Skip to content

Commit caaec9d

Browse files
committed
Removed the massive code duplication in the 'upgrade' command
1 parent 979dac6 commit caaec9d

File tree

4 files changed

+73
-187
lines changed

4 files changed

+73
-187
lines changed

cli/upgrade/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/cli/feedback"
2424
"github.com/arduino/arduino-cli/cli/instance"
2525
"github.com/arduino/arduino-cli/cli/output"
26-
"github.com/arduino/arduino-cli/commands"
26+
"github.com/arduino/arduino-cli/commands/upgrade"
2727
"github.com/arduino/arduino-cli/i18n"
2828
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2929
"github.com/sirupsen/logrus"
@@ -54,7 +54,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
5454
inst := instance.CreateAndInit()
5555
logrus.Info("Executing `arduino-cli upgrade`")
5656

57-
err := commands.Upgrade(context.Background(), &rpc.UpgradeRequest{
57+
err := upgrade.Upgrade(context.Background(), &rpc.UpgradeRequest{
5858
Instance: inst,
5959
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
6060
}, output.NewDownloadProgressBarCB(), output.TaskProgress())

commands/daemon/daemon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/arduino/arduino-cli/commands/lib"
3131
"github.com/arduino/arduino-cli/commands/monitor"
3232
"github.com/arduino/arduino-cli/commands/sketch"
33+
"github.com/arduino/arduino-cli/commands/upgrade"
3334
"github.com/arduino/arduino-cli/commands/upload"
3435
"github.com/arduino/arduino-cli/i18n"
3536
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -208,7 +209,7 @@ func (s *ArduinoCoreServerImpl) Outdated(ctx context.Context, req *rpc.OutdatedR
208209

209210
// Upgrade FIXMEDOC
210211
func (s *ArduinoCoreServerImpl) Upgrade(req *rpc.UpgradeRequest, stream rpc.ArduinoCoreService_UpgradeServer) error {
211-
err := commands.Upgrade(stream.Context(), req,
212+
err := upgrade.Upgrade(stream.Context(), req,
212213
func(p *rpc.DownloadProgress) {
213214
stream.Send(&rpc.UpgradeResponse{
214215
Progress: p,

commands/instances.go

Lines changed: 0 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package commands
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"net/url"
2322
"os"
@@ -28,7 +27,6 @@ import (
2827
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2928
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
3029
"github.com/arduino/arduino-cli/arduino/globals"
31-
"github.com/arduino/arduino-cli/arduino/httpclient"
3230
"github.com/arduino/arduino-cli/arduino/libraries"
3331
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
3432
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -642,188 +640,6 @@ func getOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease {
642640
return &rpc.LibraryRelease{}
643641
}
644642

645-
// Upgrade downloads and installs outdated Cores and Libraries
646-
func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
647-
downloaderConfig, err := httpclient.GetDownloaderConfig()
648-
if err != nil {
649-
return err
650-
}
651-
652-
lm := GetLibraryManager(req.Instance.Id)
653-
if lm == nil {
654-
return &arduino.InvalidInstanceError{}
655-
}
656-
657-
for _, libAlternatives := range lm.Libraries {
658-
for _, library := range libAlternatives.Alternatives {
659-
if library.Location != libraries.User {
660-
continue
661-
}
662-
available := lm.Index.FindLibraryUpdate(library)
663-
if available == nil {
664-
continue
665-
}
666-
667-
// Downloads latest library release
668-
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", available)})
669-
if err := available.Resource.Download(lm.DownloadsDir, downloaderConfig, available.String(), downloadCB); err != nil {
670-
return &arduino.FailedDownloadError{Message: tr("Error downloading library"), Cause: err}
671-
}
672-
673-
// Installs downloaded library
674-
taskCB(&rpc.TaskProgress{Name: tr("Installing %s", available)})
675-
libPath, libReplaced, err := lm.InstallPrerequisiteCheck(available)
676-
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
677-
taskCB(&rpc.TaskProgress{Message: tr("Already installed %s", available), Completed: true})
678-
continue
679-
} else if err != nil {
680-
return &arduino.FailedLibraryInstallError{Cause: err}
681-
}
682-
683-
if libReplaced != nil {
684-
taskCB(&rpc.TaskProgress{Message: tr("Replacing %[1]s with %[2]s", libReplaced, available)})
685-
}
686-
687-
if err := lm.Install(available, libPath); err != nil {
688-
return &arduino.FailedLibraryInstallError{Cause: err}
689-
}
690-
691-
taskCB(&rpc.TaskProgress{Message: tr("Installed %s", available), Completed: true})
692-
}
693-
}
694-
695-
pm := GetPackageManager(req.Instance.Id)
696-
if pm == nil {
697-
return &arduino.InvalidInstanceError{}
698-
}
699-
700-
for _, targetPackage := range pm.Packages {
701-
for _, installed := range targetPackage.Platforms {
702-
if installedRelease := pm.GetInstalledPlatformRelease(installed); installedRelease != nil {
703-
latest := installed.GetLatestRelease()
704-
if latest == nil || latest == installedRelease {
705-
continue
706-
}
707-
708-
ref := &packagemanager.PlatformReference{
709-
Package: installedRelease.Platform.Package.Name,
710-
PlatformArchitecture: installedRelease.Platform.Architecture,
711-
PlatformVersion: installedRelease.Version,
712-
}
713-
// Get list of installed tools needed by the currently installed version
714-
_, installedTools, err := pm.FindPlatformReleaseDependencies(ref)
715-
if err != nil {
716-
return &arduino.NotFoundError{Message: tr("Can't find dependencies for platform %s", ref), Cause: err}
717-
}
718-
719-
ref = &packagemanager.PlatformReference{
720-
Package: latest.Platform.Package.Name,
721-
PlatformArchitecture: latest.Platform.Architecture,
722-
PlatformVersion: latest.Version,
723-
}
724-
725-
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", latest)})
726-
_, tools, err := pm.FindPlatformReleaseDependencies(ref)
727-
if err != nil {
728-
return &arduino.NotFoundError{Message: tr("Can't find dependencies for platform %s", ref), Cause: err}
729-
}
730-
731-
toolsToInstall := []*cores.ToolRelease{}
732-
for _, tool := range tools {
733-
if tool.IsInstalled() {
734-
logrus.WithField("tool", tool).Warn("Tool already installed")
735-
taskCB(&rpc.TaskProgress{Name: tr("Tool %s already installed", tool), Completed: true})
736-
} else {
737-
toolsToInstall = append(toolsToInstall, tool)
738-
}
739-
}
740-
741-
// Downloads platform tools
742-
for _, tool := range toolsToInstall {
743-
if err := pm.DownloadToolRelease(tool, nil, downloadCB); err != nil {
744-
taskCB(&rpc.TaskProgress{Message: tr("Error downloading tool %s", tool)})
745-
return &arduino.FailedDownloadError{Message: tr("Error downloading tool %s", tool), Cause: err}
746-
}
747-
}
748-
749-
// Downloads platform
750-
if err := pm.DownloadPlatformRelease(latest, downloaderConfig, downloadCB); err != nil {
751-
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", latest), Cause: err}
752-
}
753-
754-
logrus.Info("Updating platform " + installed.String())
755-
taskCB(&rpc.TaskProgress{Name: tr("Updating platform %s", latest)})
756-
757-
// Installs tools
758-
for _, tool := range toolsToInstall {
759-
if err := pm.InstallTool(tool, taskCB); err != nil {
760-
msg := tr("Error installing tool %s", tool)
761-
taskCB(&rpc.TaskProgress{Message: msg})
762-
return &arduino.FailedInstallError{Message: msg, Cause: err}
763-
}
764-
}
765-
766-
// Installs platform
767-
err = pm.InstallPlatform(latest)
768-
if err != nil {
769-
logrus.WithError(err).Error("Cannot install platform")
770-
msg := tr("Error installing platform %s", latest)
771-
taskCB(&rpc.TaskProgress{Message: msg})
772-
return &arduino.FailedInstallError{Message: msg, Cause: err}
773-
}
774-
775-
// Uninstall previously installed release
776-
err = pm.UninstallPlatform(installedRelease, taskCB)
777-
778-
// In case uninstall fails tries to rollback
779-
if err != nil {
780-
logrus.WithError(err).Error("Error updating platform.")
781-
taskCB(&rpc.TaskProgress{Message: tr("Error upgrading platform: %s", err)})
782-
783-
// Rollback
784-
if err := pm.UninstallPlatform(latest, taskCB); err != nil {
785-
logrus.WithError(err).Error("Error rolling-back changes.")
786-
msg := tr("Error rolling-back changes")
787-
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf("%s: %s", msg, err)})
788-
return &arduino.FailedInstallError{Message: msg, Cause: err}
789-
}
790-
}
791-
792-
// Uninstall unused tools
793-
for _, toolRelease := range installedTools {
794-
if !pm.IsToolRequired(toolRelease) {
795-
log := pm.Log.WithField("Tool", toolRelease)
796-
797-
log.Info("Uninstalling tool")
798-
taskCB(&rpc.TaskProgress{Name: tr("Uninstalling %s: tool is no more required", toolRelease)})
799-
if err := pm.UninstallTool(toolRelease, taskCB); err != nil {
800-
log.WithError(err).Error("Error uninstalling")
801-
return &arduino.FailedInstallError{Message: tr("Error uninstalling tool %s", toolRelease), Cause: err}
802-
}
803-
804-
log.Info("Tool uninstalled")
805-
taskCB(&rpc.TaskProgress{Message: tr("%s uninstalled", toolRelease), Completed: true})
806-
}
807-
}
808-
809-
// Perform post install
810-
if !req.SkipPostInstall {
811-
logrus.Info("Running post_install script")
812-
taskCB(&rpc.TaskProgress{Message: tr("Configuring platform")})
813-
if err := pm.RunPostInstallScript(latest); err != nil {
814-
taskCB(&rpc.TaskProgress{Message: tr("WARNING: cannot run post install: %s", err)})
815-
}
816-
} else {
817-
logrus.Info("Skipping platform configuration (post_install run).")
818-
taskCB(&rpc.TaskProgress{Message: tr("Skipping platform configuration")})
819-
}
820-
}
821-
}
822-
}
823-
824-
return nil
825-
}
826-
827643
// LoadSketch collects and returns all files composing a sketch
828644
func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
829645
// TODO: This should be a ToRpc function for the Sketch struct

commands/upgrade/upgrade.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 upgrade
17+
18+
import (
19+
"context"
20+
"strings"
21+
22+
"github.com/arduino/arduino-cli/commands/core"
23+
"github.com/arduino/arduino-cli/commands/lib"
24+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
25+
)
26+
27+
// Upgrade downloads and installs outdated Cores and Libraries
28+
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+
})
33+
if err != nil {
34+
return err
35+
}
36+
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() {
46+
err := lib.LibraryInstall(ctx, &rpc.LibraryInstallRequest{
47+
Instance: req.GetInstance(),
48+
Name: libToUpgrade.GetLibrary().GetName(),
49+
}, downloadCB, taskCB)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
55+
for _, platformToUpgrade := range getPlatformsResp {
56+
split := strings.Split(platformToUpgrade.GetId(), ":")
57+
_, err := core.PlatformUpgrade(ctx, &rpc.PlatformUpgradeRequest{
58+
Instance: req.GetInstance(),
59+
PlatformPackage: split[0],
60+
Architecture: split[1],
61+
SkipPostInstall: req.GetSkipPostInstall(),
62+
}, downloadCB, taskCB)
63+
if err != nil {
64+
return err
65+
}
66+
}
67+
68+
return nil
69+
}

0 commit comments

Comments
 (0)