Skip to content

Commit 207f5ef

Browse files
committed
Removed the massive code duplication in the 'outdated' command
1 parent caaec9d commit 207f5ef

File tree

6 files changed

+58
-136
lines changed

6 files changed

+58
-136
lines changed

cli/outdated/outdated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/arduino/arduino-cli/cli/feedback"
2323
"github.com/arduino/arduino-cli/cli/instance"
24-
"github.com/arduino/arduino-cli/commands"
24+
"github.com/arduino/arduino-cli/commands/outdated"
2525
"github.com/arduino/arduino-cli/i18n"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2727
"github.com/arduino/arduino-cli/table"
@@ -50,7 +50,7 @@ func runOutdatedCommand(cmd *cobra.Command, args []string) {
5050
inst := instance.CreateAndInit()
5151
logrus.Info("Executing `arduino-cli outdated`")
5252

53-
outdatedResp, err := commands.Outdated(context.Background(), &rpc.OutdatedRequest{
53+
outdatedResp, err := outdated.Outdated(context.Background(), &rpc.OutdatedRequest{
5454
Instance: inst,
5555
})
5656
if err != nil {

cli/update/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/arduino/arduino-cli/cli/instance"
2525
"github.com/arduino/arduino-cli/cli/output"
2626
"github.com/arduino/arduino-cli/commands"
27+
"github.com/arduino/arduino-cli/commands/outdated"
2728
"github.com/arduino/arduino-cli/i18n"
2829
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2930
"github.com/arduino/arduino-cli/table"
@@ -70,7 +71,7 @@ func runUpdateCommand(cmd *cobra.Command, args []string) {
7071
feedback.Errorf(tr("Error initializing instance: %v"), err)
7172
}
7273

73-
outdatedResp, err := commands.Outdated(context.Background(), &rpc.OutdatedRequest{
74+
outdatedResp, err := outdated.Outdated(context.Background(), &rpc.OutdatedRequest{
7475
Instance: inst,
7576
})
7677
if err != nil {

commands/daemon/daemon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/arduino/arduino-cli/commands/core"
3030
"github.com/arduino/arduino-cli/commands/lib"
3131
"github.com/arduino/arduino-cli/commands/monitor"
32+
"github.com/arduino/arduino-cli/commands/outdated"
3233
"github.com/arduino/arduino-cli/commands/sketch"
3334
"github.com/arduino/arduino-cli/commands/upgrade"
3435
"github.com/arduino/arduino-cli/commands/upload"
@@ -203,7 +204,7 @@ func (s *ArduinoCoreServerImpl) UpdateCoreLibrariesIndex(req *rpc.UpdateCoreLibr
203204

204205
// Outdated FIXMEDOC
205206
func (s *ArduinoCoreServerImpl) Outdated(ctx context.Context, req *rpc.OutdatedRequest) (*rpc.OutdatedResponse, error) {
206-
resp, err := commands.Outdated(ctx, req)
207+
resp, err := outdated.Outdated(ctx, req)
207208
return resp, convertErrorToRPCStatus(err)
208209
}
209210

commands/instances.go

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -522,124 +522,6 @@ func UpdateCoreLibrariesIndex(ctx context.Context, req *rpc.UpdateCoreLibrariesI
522522
return nil
523523
}
524524

525-
// Outdated returns a list struct containing both Core and Libraries that can be updated
526-
func Outdated(ctx context.Context, req *rpc.OutdatedRequest) (*rpc.OutdatedResponse, error) {
527-
id := req.GetInstance().GetId()
528-
529-
lm := GetLibraryManager(id)
530-
if lm == nil {
531-
return nil, &arduino.InvalidInstanceError{}
532-
}
533-
534-
outdatedLibraries := []*rpc.InstalledLibrary{}
535-
for _, libAlternatives := range lm.Libraries {
536-
for _, library := range libAlternatives.Alternatives {
537-
if library.Location != libraries.User {
538-
continue
539-
}
540-
available := lm.Index.FindLibraryUpdate(library)
541-
if available == nil {
542-
continue
543-
}
544-
545-
outdatedLibraries = append(outdatedLibraries, &rpc.InstalledLibrary{
546-
Library: getOutputLibrary(library),
547-
Release: getOutputRelease(available),
548-
})
549-
}
550-
}
551-
552-
pm := GetPackageManager(id)
553-
if pm == nil {
554-
return nil, &arduino.InvalidInstanceError{}
555-
}
556-
557-
outdatedPlatforms := []*rpc.Platform{}
558-
for _, targetPackage := range pm.Packages {
559-
for _, installed := range targetPackage.Platforms {
560-
if installedRelease := pm.GetInstalledPlatformRelease(installed); installedRelease != nil {
561-
latest := installed.GetLatestRelease()
562-
if latest == nil || latest == installedRelease {
563-
continue
564-
}
565-
rpcPlatform := PlatformReleaseToRPC(latest)
566-
rpcPlatform.Installed = installedRelease.Version.String()
567-
568-
outdatedPlatforms = append(
569-
outdatedPlatforms,
570-
rpcPlatform,
571-
)
572-
}
573-
}
574-
}
575-
576-
return &rpc.OutdatedResponse{
577-
OutdatedLibraries: outdatedLibraries,
578-
OutdatedPlatforms: outdatedPlatforms,
579-
}, nil
580-
}
581-
582-
func getOutputLibrary(lib *libraries.Library) *rpc.Library {
583-
insdir := ""
584-
if lib.InstallDir != nil {
585-
insdir = lib.InstallDir.String()
586-
}
587-
srcdir := ""
588-
if lib.SourceDir != nil {
589-
srcdir = lib.SourceDir.String()
590-
}
591-
utldir := ""
592-
if lib.UtilityDir != nil {
593-
utldir = lib.UtilityDir.String()
594-
}
595-
cntplat := ""
596-
if lib.ContainerPlatform != nil {
597-
cntplat = lib.ContainerPlatform.String()
598-
}
599-
600-
return &rpc.Library{
601-
Name: lib.Name,
602-
Author: lib.Author,
603-
Maintainer: lib.Maintainer,
604-
Sentence: lib.Sentence,
605-
Paragraph: lib.Paragraph,
606-
Website: lib.Website,
607-
Category: lib.Category,
608-
Architectures: lib.Architectures,
609-
Types: lib.Types,
610-
InstallDir: insdir,
611-
SourceDir: srcdir,
612-
UtilityDir: utldir,
613-
Location: lib.Location.ToRPCLibraryLocation(),
614-
ContainerPlatform: cntplat,
615-
Layout: lib.Layout.ToRPCLibraryLayout(),
616-
RealName: lib.RealName,
617-
DotALinkage: lib.DotALinkage,
618-
Precompiled: lib.Precompiled,
619-
LdFlags: lib.LDflags,
620-
IsLegacy: lib.IsLegacy,
621-
Version: lib.Version.String(),
622-
License: lib.License,
623-
}
624-
}
625-
626-
func getOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease {
627-
if lib != nil {
628-
return &rpc.LibraryRelease{
629-
Author: lib.Author,
630-
Version: lib.Version.String(),
631-
Maintainer: lib.Maintainer,
632-
Sentence: lib.Sentence,
633-
Paragraph: lib.Paragraph,
634-
Website: lib.Website,
635-
Category: lib.Category,
636-
Architectures: lib.Architectures,
637-
Types: lib.Types,
638-
}
639-
}
640-
return &rpc.LibraryRelease{}
641-
}
642-
643525
// LoadSketch collects and returns all files composing a sketch
644526
func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
645527
// TODO: This should be a ToRpc function for the Sketch struct

commands/outdated/outdated.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package outdated
17+
18+
import (
19+
"context"
20+
21+
"github.com/arduino/arduino-cli/commands/core"
22+
"github.com/arduino/arduino-cli/commands/lib"
23+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24+
)
25+
26+
// Outdated returns a list struct containing both Core and Libraries that can be updated
27+
func Outdated(ctx context.Context, req *rpc.OutdatedRequest) (*rpc.OutdatedResponse, error) {
28+
libraryListResponse, err := lib.LibraryList(ctx, &rpc.LibraryListRequest{
29+
Instance: req.GetInstance(),
30+
Updatable: true,
31+
})
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
getPlatformsResp, err := core.GetPlatforms(&rpc.PlatformListRequest{
37+
Instance: req.GetInstance(),
38+
UpdatableOnly: true,
39+
})
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return &rpc.OutdatedResponse{
45+
OutdatedLibraries: libraryListResponse.GetInstalledLibraries(),
46+
OutdatedPlatforms: getPlatformsResp,
47+
}, nil
48+
}

commands/upgrade/upgrade.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,18 @@ import (
2121

2222
"github.com/arduino/arduino-cli/commands/core"
2323
"github.com/arduino/arduino-cli/commands/lib"
24+
"github.com/arduino/arduino-cli/commands/outdated"
2425
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2526
)
2627

2728
// Upgrade downloads and installs outdated Cores and Libraries
2829
func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
29-
libraryListResponse, err := lib.LibraryList(ctx, &rpc.LibraryListRequest{
30-
Instance: req.GetInstance(),
31-
Updatable: true,
32-
})
30+
outdatedResp, err := outdated.Outdated(ctx, &rpc.OutdatedRequest{Instance: req.GetInstance()})
3331
if err != nil {
3432
return err
3533
}
3634

37-
getPlatformsResp, err := core.GetPlatforms(&rpc.PlatformListRequest{
38-
Instance: req.GetInstance(),
39-
UpdatableOnly: true,
40-
})
41-
if err != nil {
42-
return err
43-
}
44-
45-
for _, libToUpgrade := range libraryListResponse.GetInstalledLibraries() {
35+
for _, libToUpgrade := range outdatedResp.GetOutdatedLibraries() {
4636
err := lib.LibraryInstall(ctx, &rpc.LibraryInstallRequest{
4737
Instance: req.GetInstance(),
4838
Name: libToUpgrade.GetLibrary().GetName(),
@@ -52,7 +42,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB rpc.Downlo
5242
}
5343
}
5444

55-
for _, platformToUpgrade := range getPlatformsResp {
45+
for _, platformToUpgrade := range outdatedResp.GetOutdatedPlatforms() {
5646
split := strings.Split(platformToUpgrade.GetId(), ":")
5747
_, err := core.PlatformUpgrade(ctx, &rpc.PlatformUpgradeRequest{
5848
Instance: req.GetInstance(),

0 commit comments

Comments
 (0)