Skip to content

Commit fd5334c

Browse files
committed
modified return value for install and uninstall
1 parent 3e34dbd commit fd5334c

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

commands/core/install.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,46 @@ package core
22

33
import (
44
"context"
5-
"os"
5+
"fmt"
66

77
"github.com/arduino/arduino-cli/arduino/cores"
88
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
9-
"github.com/arduino/arduino-cli/cli"
9+
"github.com/arduino/arduino-cli/commands"
1010
"github.com/arduino/arduino-cli/common/formatter"
1111
"github.com/arduino/arduino-cli/rpc"
1212
semver "go.bug.st/relaxed-semver"
1313
)
1414

15-
//"packager:arch@version
1615
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.PlatformInstallResp, error) {
1716
version, err := semver.Parse(req.Version)
1817
if err != nil {
1918
formatter.PrintError(err, "version not readable")
20-
os.Exit(cli.ErrBadCall)
19+
return nil, fmt.Errorf("version not readable", err)
2120
}
2221
ref := &packagemanager.PlatformReference{
2322
Package: req.PlatformPackage,
2423
PlatformArchitecture: req.Architecture,
2524
PlatformVersion: version}
26-
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
25+
pm := commands.GetPackageManager(req)
2726
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
2827
if err != nil {
2928
formatter.PrintError(err, "Could not determine platform dependencies")
30-
os.Exit(cli.ErrBadCall)
29+
return nil, fmt.Errorf("Could not determine platform dependencies", err)
3130
}
3231

3332
installPlatform(pm, platform, tools)
3433

3534
return &rpc.PlatformInstallResp{}, nil
3635
}
3736

38-
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) {
37+
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) error {
3938
log := pm.Log.WithField("platform", platformRelease)
4039

4140
// Prerequisite checks before install
4241
if platformRelease.IsInstalled() {
4342
log.Warn("Platform already installed")
4443
formatter.Print("Platform " + platformRelease.String() + " already installed")
45-
return
44+
return fmt.Errorf("Platform " + platformRelease.String() + " already installed")
4645
}
4746
toolsToInstall := []*cores.ToolRelease{}
4847
for _, tool := range requiredTools {
@@ -80,39 +79,41 @@ func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.P
8079
if err != nil {
8180
log.WithError(err).Error("Cannot install platform")
8281
formatter.PrintError(err, "Cannot install platform")
83-
os.Exit(cli.ErrGeneric)
82+
return fmt.Errorf("Cannot install platform", err)
8483
}
8584

8685
// If upgrading remove previous release
8786
if installed != nil {
88-
err := pm.UninstallPlatform(installed)
87+
errUn := pm.UninstallPlatform(installed)
8988

9089
// In case of error try to rollback
91-
if err != nil {
92-
log.WithError(err).Error("Error updating platform.")
93-
formatter.PrintError(err, "Error updating platform")
90+
if errUn != nil {
91+
log.WithError(errUn).Error("Error updating platform.")
92+
formatter.PrintError(errUn, "Error updating platform")
9493

9594
// Rollback
9695
if err := pm.UninstallPlatform(platformRelease); err != nil {
9796
log.WithError(err).Error("Error rolling-back changes.")
9897
formatter.PrintError(err, "Error rolling-back changes.")
98+
return fmt.Errorf("Error rolling-back changes.", err)
9999
}
100-
os.Exit(cli.ErrGeneric)
100+
return fmt.Errorf("Error updating platform", errUn)
101101
}
102102
}
103103

104104
log.Info("Platform installed")
105105
formatter.Print(platformRelease.String() + " installed")
106+
return nil
106107
}
107108

108109
// InstallToolRelease installs a ToolRelease
109-
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
110+
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) error {
110111
log := pm.Log.WithField("Tool", toolRelease)
111112

112113
if toolRelease.IsInstalled() {
113114
log.Warn("Tool already installed")
114115
formatter.Print("Tool " + toolRelease.String() + " already installed")
115-
return
116+
return fmt.Errorf("Tool " + toolRelease.String() + " already installed")
116117
}
117118

118119
log.Info("Installing tool")
@@ -121,9 +122,10 @@ func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.To
121122
if err != nil {
122123
log.WithError(err).Warn("Cannot install tool")
123124
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())
124-
os.Exit(cli.ErrGeneric)
125+
return fmt.Errorf("Cannot install tool: "+toolRelease.String(), err)
125126
}
126127

127128
log.Info("Tool installed")
128129
formatter.Print(toolRelease.String() + " installed")
130+
return nil
129131
}

commands/core/uninstall.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/arduino/arduino-cli/arduino/cores"
2626
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2727
"github.com/arduino/arduino-cli/cli"
28+
"github.com/arduino/arduino-cli/commands"
2829
"github.com/arduino/arduino-cli/common/formatter"
2930
"github.com/arduino/arduino-cli/rpc"
3031
semver "go.bug.st/relaxed-semver"
@@ -35,31 +36,31 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq) (*rpc
3536
version, err := semver.Parse(req.Version)
3637
if err != nil {
3738
formatter.PrintError(err, "version not readable")
38-
os.Exit(cli.ErrBadCall)
39+
3940
}
4041
ref := &packagemanager.PlatformReference{
4142
Package: req.PlatformPackage,
4243
PlatformArchitecture: req.Architecture,
4344
PlatformVersion: version}
44-
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
45+
pm := commands.GetPackageManager(req)
4546
if ref.PlatformVersion == nil {
4647
platform := pm.FindPlatform(ref)
4748
if platform == nil {
4849
formatter.PrintErrorMessage("Platform not found " + ref.String())
49-
os.Exit(cli.ErrBadCall)
50+
5051
}
5152
platformRelease := pm.GetInstalledPlatformRelease(platform)
5253
if platformRelease == nil {
5354
formatter.PrintErrorMessage("Platform not installed " + ref.String())
54-
os.Exit(cli.ErrBadCall)
55+
5556
}
5657
ref.PlatformVersion = platformRelease.Version
5758
}
5859

5960
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
6061
if err != nil {
6162
formatter.PrintError(err, "Could not determine platform dependencies")
62-
os.Exit(cli.ErrBadCall)
63+
6364
}
6465

6566
uninstallPlatformRelease(pm, platform)

0 commit comments

Comments
 (0)