Skip to content

Commit 21ae1e0

Browse files
committed
Removed the massive code duplication in the 'upgrade' command
1 parent 148a423 commit 21ae1e0

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"
@@ -641,188 +639,6 @@ func getOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease {
641639
return &rpc.LibraryRelease{}
642640
}
643641

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