Skip to content

Commit 826c554

Browse files
committed
Refactoring 'core' commands
1 parent 875cb3c commit 826c554

File tree

10 files changed

+187
-126
lines changed

10 files changed

+187
-126
lines changed

cli/core/upgrade.go

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

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"os"
2223

@@ -25,6 +26,7 @@ import (
2526
"github.com/arduino/arduino-cli/cli/feedback"
2627
"github.com/arduino/arduino-cli/cli/instance"
2728
"github.com/arduino/arduino-cli/cli/output"
29+
"github.com/arduino/arduino-cli/commands"
2830
"github.com/arduino/arduino-cli/commands/core"
2931
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3032
"github.com/sirupsen/logrus"
@@ -94,13 +96,10 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9496
SkipPostInstall: DetectSkipPostInstallValue(),
9597
}
9698

97-
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
98-
if err != nil {
99-
if d := err.Details(); len(d) > 0 {
100-
if _, ok := d[0].(*rpc.AlreadyAtLatestVersionError); ok {
101-
feedback.Printf(tr("Platform %s is already at the latest version", platformRef))
102-
continue
103-
}
99+
if _, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress()); err != nil {
100+
if errors.Is(err, &commands.PlatformAlreadyAtTheLatestVersionError{}) {
101+
feedback.Print(err.Error())
102+
continue
104103
}
105104

106105
feedback.Errorf(tr("Error during upgrade: %v", err))

commands/bundled_tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.To
5151
err := pm.InstallTool(toolRelease)
5252
if err != nil {
5353
log.WithError(err).Warn("Cannot install tool")
54-
return fmt.Errorf(tr("installing tool %[1]s: %[2]s"), toolRelease, err)
54+
return &FailedInstallError{Message: tr("Cannot install tool %s", toolRelease), Cause: err}
5555
}
5656
log.Info("Tool installed")
5757
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("%s installed"), toolRelease), Completed: true})

commands/core/download.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,46 @@ package core
1717

1818
import (
1919
"context"
20-
"fmt"
20+
"errors"
2121

2222
"github.com/arduino/arduino-cli/arduino/cores"
2323
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2424
"github.com/arduino/arduino-cli/commands"
2525
"github.com/arduino/arduino-cli/i18n"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27-
"google.golang.org/grpc/codes"
28-
"google.golang.org/grpc/status"
2927
)
3028

3129
var tr = i18n.Tr
3230

3331
// PlatformDownload FIXMEDOC
34-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, *status.Status) {
32+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
3533
pm := commands.GetPackageManager(req.GetInstance().GetId())
3634
if pm == nil {
37-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
35+
return nil, &commands.InvalidInstanceError{}
3836
}
3937

4038
version, err := commands.ParseVersion(req)
4139
if err != nil {
42-
return nil, status.Newf(codes.InvalidArgument, tr("Invalid version: %s"), err)
40+
return nil, &commands.InvalidVersionError{Cause: err}
4341
}
4442

45-
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
43+
ref := &packagemanager.PlatformReference{
4644
Package: req.PlatformPackage,
4745
PlatformArchitecture: req.Architecture,
4846
PlatformVersion: version,
49-
})
47+
}
48+
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
5049
if err != nil {
51-
return nil, status.Newf(codes.InvalidArgument, tr("Error finding platform dependencies: %s"), err)
50+
return nil, &commands.PlatformNotFound{Platform: ref.String(), Cause: err}
5251
}
5352

54-
err = downloadPlatform(pm, platform, downloadCB)
55-
if err != nil {
56-
return nil, status.New(codes.Unavailable, err.Error())
53+
if err := downloadPlatform(pm, platform, downloadCB); err != nil {
54+
return nil, err
5755
}
5856

5957
for _, tool := range tools {
60-
err := downloadTool(pm, tool, downloadCB)
61-
if err != nil {
62-
return nil, status.Newf(codes.Unavailable, tr("Error downloading tool %[1]s: %[2]s"), tool, err)
58+
if err := downloadTool(pm, tool, downloadCB); err != nil {
59+
return nil, err
6360
}
6461
}
6562

@@ -70,20 +67,26 @@ func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.
7067
// Download platform
7168
config, err := commands.GetDownloaderConfig()
7269
if err != nil {
73-
return err
70+
return &commands.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7471
}
7572
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
7673
if err != nil {
77-
return err
74+
return &commands.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7875
}
7976
return commands.Download(resp, platformRelease.String(), downloadCB)
8077
}
8178

8279
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {
8380
// Check if tool has a flavor available for the current OS
8481
if tool.GetCompatibleFlavour() == nil {
85-
return fmt.Errorf(tr("tool %s not available for the current OS"), tool)
82+
return &commands.FailedDownloadError{
83+
Message: tr("Error downloading tool %s", tool),
84+
Cause: errors.New(tr("no versions available for the current OS", tool))}
85+
}
86+
87+
if err := commands.DownloadToolRelease(pm, tool, downloadCB); err != nil {
88+
return &commands.FailedDownloadError{Message: tr("Error downloading tool %s", tool), Cause: err}
8689
}
8790

88-
return commands.DownloadToolRelease(pm, tool, downloadCB)
91+
return nil
8992
}

commands/core/install.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,40 @@ import (
2323
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2424
"github.com/arduino/arduino-cli/commands"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26-
"google.golang.org/grpc/codes"
27-
"google.golang.org/grpc/status"
2826
)
2927

3028
// PlatformInstall FIXMEDOC
3129
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
32-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, *status.Status) {
30+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
3331

3432
pm := commands.GetPackageManager(req.GetInstance().GetId())
3533
if pm == nil {
36-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
34+
return nil, &commands.InvalidInstanceError{}
3735
}
3836

3937
version, err := commands.ParseVersion(req)
4038
if err != nil {
41-
return nil, status.Newf(codes.InvalidArgument, tr("Invalid version: %s"), err)
39+
return nil, &commands.InvalidVersionError{Cause: err}
4240
}
4341

44-
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
42+
ref := &packagemanager.PlatformReference{
4543
Package: req.PlatformPackage,
4644
PlatformArchitecture: req.Architecture,
4745
PlatformVersion: version,
48-
})
46+
}
47+
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
4948
if err != nil {
50-
return nil, status.Newf(codes.InvalidArgument, tr("Error finding platform dependencies: %s"), err)
49+
return nil, &commands.PlatformNotFound{Platform: ref.String(), Cause: err}
5150
}
5251

5352
err = installPlatform(pm, platform, tools, downloadCB, taskCB, req.GetSkipPostInstall())
5453
if err != nil {
55-
return nil, status.Convert(err)
54+
return nil, err
5655
}
5756

5857
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
5958
if status != nil {
60-
return nil, status
59+
return nil, status.Err()
6160
}
6261

6362
return &rpc.PlatformInstallResponse{}, nil
@@ -92,16 +91,14 @@ func installPlatform(pm *packagemanager.PackageManager,
9291
return err
9392
}
9493
}
95-
err := downloadPlatform(pm, platformRelease, downloadCB)
96-
if err != nil {
94+
if err := downloadPlatform(pm, platformRelease, downloadCB); err != nil {
9795
return err
9896
}
9997
taskCB(&rpc.TaskProgress{Completed: true})
10098

10199
// Install tools first
102100
for _, tool := range toolsToInstall {
103-
err := commands.InstallToolRelease(pm, tool, taskCB)
104-
if err != nil {
101+
if err := commands.InstallToolRelease(pm, tool, taskCB); err != nil {
105102
return err
106103
}
107104
}
@@ -111,11 +108,11 @@ func installPlatform(pm *packagemanager.PackageManager,
111108
if installed == nil {
112109
// No version of this platform is installed
113110
log.Info("Installing platform")
114-
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Installing %s"), platformRelease)})
111+
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Installing platform %s"), platformRelease)})
115112
} else {
116113
// A platform with a different version is already installed
117114
log.Info("Upgrading platform " + installed.String())
118-
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Upgrading %[1]s with %[2]s"), installed, platformRelease)})
115+
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Upgrading platform %[1]s with %[2]s"), installed, platformRelease)})
119116
platformRef := &packagemanager.PlatformReference{
120117
Package: platformRelease.Platform.Package.Name,
121118
PlatformArchitecture: platformRelease.Platform.Architecture,
@@ -128,33 +125,32 @@ func installPlatform(pm *packagemanager.PackageManager,
128125
var err error
129126
_, installedTools, err = pm.FindPlatformReleaseDependencies(platformRef)
130127
if err != nil {
131-
return fmt.Errorf(tr("can't find dependencies for platform %[1]s: %[2]w"), platformRef, err)
128+
return &commands.NotFoundError{Message: tr("Can't find dependencies for platform %s", platformRef), Cause: err}
132129
}
133130
}
134131

135132
// Install
136-
err = pm.InstallPlatform(platformRelease)
137-
if err != nil {
133+
if err := pm.InstallPlatform(platformRelease); err != nil {
138134
log.WithError(err).Error("Cannot install platform")
139-
return err
135+
return &commands.FailedInstallError{Message: tr("Cannot install platform"), Cause: err}
140136
}
141137

142138
// If upgrading remove previous release
143139
if installed != nil {
144-
errUn := pm.UninstallPlatform(installed)
140+
uninstallErr := pm.UninstallPlatform(installed)
145141

146142
// In case of error try to rollback
147-
if errUn != nil {
148-
log.WithError(errUn).Error("Error upgrading platform.")
149-
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("Error upgrading platform: %s"), errUn)})
143+
if uninstallErr != nil {
144+
log.WithError(uninstallErr).Error("Error upgrading platform.")
145+
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("Error upgrading platform: %s"), uninstallErr)})
150146

151147
// Rollback
152148
if err := pm.UninstallPlatform(platformRelease); err != nil {
153149
log.WithError(err).Error("Error rolling-back changes.")
154150
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("Error rolling-back changes: %s"), err)})
155151
}
156152

157-
return fmt.Errorf(tr("upgrading platform: %s"), errUn)
153+
return &commands.FailedInstallError{Message: tr("Cannot upgrade platform"), Cause: uninstallErr}
158154
}
159155

160156
// Uninstall unused tools
@@ -169,16 +165,16 @@ func installPlatform(pm *packagemanager.PackageManager,
169165
// Perform post install
170166
if !skipPostInstall {
171167
log.Info("Running post_install script")
172-
taskCB(&rpc.TaskProgress{Message: tr("Configuring platform")})
168+
taskCB(&rpc.TaskProgress{Message: tr("Configuring platform.")})
173169
if err := pm.RunPostInstallScript(platformRelease); err != nil {
174-
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("WARNING: cannot run post install: %s"), err)})
170+
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("WARNING cannot configure platform: %s"), err)})
175171
}
176172
} else {
177-
log.Info("Skipping platform configuration (post_install run).")
178-
taskCB(&rpc.TaskProgress{Message: tr("Skipping platform configuration")})
173+
log.Info("Skipping platform configuration.")
174+
taskCB(&rpc.TaskProgress{Message: tr("Skipping platform configuration.")})
179175
}
180176

181177
log.Info("Platform installed")
182-
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("%s installed"), platformRelease), Completed: true})
178+
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf(tr("Platform %s installed"), platformRelease), Completed: true})
183179
return nil
184180
}

commands/core/list.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,20 @@
1616
package core
1717

1818
import (
19-
"fmt"
19+
"errors"
2020
"sort"
2121
"strings"
2222

2323
"github.com/arduino/arduino-cli/commands"
2424
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
25-
"google.golang.org/grpc/codes"
26-
"google.golang.org/grpc/status"
2725
)
2826

2927
// GetPlatforms returns a list of installed platforms, optionally filtered by
3028
// those requiring an update.
31-
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, *status.Status) {
32-
instanceID := req.Instance.Id
33-
i := commands.GetInstance(instanceID)
34-
if i == nil {
35-
return nil, status.Newf(codes.InvalidArgument, tr("Invalid instance"))
36-
}
37-
38-
packageManager := i.PackageManager
29+
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, error) {
30+
packageManager := commands.GetPackageManager(req.GetInstance().Id)
3931
if packageManager == nil {
40-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
32+
return nil, &commands.InvalidInstanceError{}
4133
}
4234

4335
res := []*rpc.Platform{}
@@ -62,7 +54,7 @@ func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, *status.Status
6254
if platformRelease != nil {
6355
latest := platform.GetLatestRelease()
6456
if latest == nil {
65-
return nil, fmt.Errorf(tr("can't find latest release of core %s", platform))
57+
return nil, &commands.PlatformNotFound{Platform: platform.String(), Cause: errors.New(tr("the platform has no releases"))}
6658
}
6759

6860
if req.UpdatableOnly {

commands/core/search.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ import (
2424
"github.com/arduino/arduino-cli/arduino/utils"
2525
"github.com/arduino/arduino-cli/commands"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27-
"google.golang.org/grpc/codes"
28-
"google.golang.org/grpc/status"
2927
)
3028

31-
// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
32-
// This value is completely arbitrary and picked randomly.
33-
const maximumSearchDistance = 20
34-
3529
// PlatformSearch FIXMEDOC
36-
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, *status.Status) {
30+
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
3731
searchArgs := strings.TrimSpace(req.SearchArgs)
3832
allVersions := req.AllVersions
3933
pm := commands.GetPackageManager(req.Instance.Id)
4034
if pm == nil {
41-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
35+
return nil, &commands.InvalidInstanceError{}
4236
}
4337

4438
res := []*cores.PlatformRelease{}

0 commit comments

Comments
 (0)