Skip to content

Commit ccb54c2

Browse files
committed
Refactoring 'lib' commands
1 parent beadb78 commit ccb54c2

13 files changed

+157
-98
lines changed

cli/lib/args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func ParseLibraryReferenceArgAndAdjustCase(instance *rpc.Instance, arg string) (
8181
Query: libRef.Name,
8282
})
8383
if err != nil {
84-
return nil, err.Err()
84+
return nil, err
8585
}
8686

8787
candidates := []*rpc.SearchedLibrary{}

cli/lib/search.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
5858
os.Exit(errorcodes.ErrGeneric)
5959
}
6060

61-
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
62-
Instance: inst,
63-
}, output.ProgressBar())
64-
if err != nil {
61+
if err := commands.UpdateLibrariesIndex(
62+
context.Background(),
63+
&rpc.UpdateLibrariesIndexRequest{Instance: inst},
64+
output.ProgressBar(),
65+
); err != nil {
6566
feedback.Errorf(tr("Error updating library index: %v"), err)
6667
os.Exit(errorcodes.ErrGeneric)
6768
}

commands/daemon/daemon.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type ArduinoCoreServerImpl struct {
4242
var tr = i18n.Tr
4343

4444
func convertErrorToRPCStatus(err error) error {
45+
if err == nil {
46+
return nil
47+
}
4548
if cmdErr, ok := err.(commands.CommandError); ok {
4649
return cmdErr.ToRPCStatus().Err()
4750
}
@@ -373,7 +376,7 @@ func (s *ArduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest,
373376
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryDownloadResponse{Progress: p}) },
374377
)
375378
if err != nil {
376-
return err.Err()
379+
return convertErrorToRPCStatus(err)
377380
}
378381
return stream.Send(resp)
379382
}
@@ -386,7 +389,7 @@ func (s *ArduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, s
386389
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) },
387390
)
388391
if err != nil {
389-
return err.Err()
392+
return convertErrorToRPCStatus(err)
390393
}
391394
return stream.Send(&rpc.LibraryInstallResponse{})
392395
}
@@ -397,7 +400,7 @@ func (s *ArduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallReques
397400
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) },
398401
)
399402
if err != nil {
400-
return err.Err()
403+
return convertErrorToRPCStatus(err)
401404
}
402405
return stream.Send(&rpc.LibraryUninstallResponse{})
403406
}
@@ -409,27 +412,27 @@ func (s *ArduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequ
409412
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) },
410413
)
411414
if err != nil {
412-
return err.Err()
415+
return convertErrorToRPCStatus(err)
413416
}
414417
return stream.Send(&rpc.LibraryUpgradeAllResponse{})
415418
}
416419

417420
// LibraryResolveDependencies FIXMEDOC
418421
func (s *ArduinoCoreServerImpl) LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
419422
resp, err := lib.LibraryResolveDependencies(ctx, req)
420-
return resp, err.Err()
423+
return resp, convertErrorToRPCStatus(err)
421424
}
422425

423426
// LibrarySearch FIXMEDOC
424427
func (s *ArduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
425428
resp, err := lib.LibrarySearch(ctx, req)
426-
return resp, err.Err()
429+
return resp, convertErrorToRPCStatus(err)
427430
}
428431

429432
// LibraryList FIXMEDOC
430433
func (s *ArduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
431434
resp, err := lib.LibraryList(ctx, req)
432-
return resp, err.Err()
435+
return resp, convertErrorToRPCStatus(err)
433436
}
434437

435438
// ArchiveSketch FIXMEDOC
@@ -445,7 +448,7 @@ func (s *ArduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequ
445448
func(p *rpc.TaskProgress) { stream.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) },
446449
)
447450
if err != nil {
448-
return err.Err()
451+
return convertErrorToRPCStatus(err)
449452
}
450453
return stream.Send(&rpc.ZipLibraryInstallResponse{})
451454
}
@@ -457,7 +460,7 @@ func (s *ArduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequ
457460
func(p *rpc.TaskProgress) { stream.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) },
458461
)
459462
if err != nil {
460-
return err.Err()
463+
return convertErrorToRPCStatus(err)
461464
}
462465
return stream.Send(&rpc.GitLibraryInstallResponse{})
463466
}

commands/errors.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ func (e *InvalidFQBNError) Unwrap() error {
6363
return e.Cause
6464
}
6565

66+
// InvalidLibraryError is returned when the library has syntax errors
67+
type InvalidLibraryError struct {
68+
Cause error
69+
}
70+
71+
func (e *InvalidLibraryError) Error() string {
72+
return composeErrorMsg(tr("Invalid library"), e.Cause)
73+
}
74+
75+
func (e *InvalidLibraryError) ToRPCStatus() *status.Status {
76+
return status.New(codes.InvalidArgument, e.Error())
77+
}
78+
79+
func (e *InvalidLibraryError) Unwrap() error {
80+
return e.Cause
81+
}
82+
6683
// InvalidVersionError is returned when the version has syntax errors
6784
type InvalidVersionError struct {
6885
Cause error
@@ -207,6 +224,42 @@ func (e *PlatformNotFound) Unwrap() error {
207224
return e.Cause
208225
}
209226

227+
// LibraryNotFound is returned when a platform is not found
228+
type LibraryNotFound struct {
229+
Library string
230+
Cause error
231+
}
232+
233+
func (e *LibraryNotFound) Error() string {
234+
return composeErrorMsg(tr("Library '%s' not found", e.Library), e.Cause)
235+
}
236+
237+
func (e *LibraryNotFound) ToRPCStatus() *status.Status {
238+
return status.New(codes.FailedPrecondition, e.Error())
239+
}
240+
241+
func (e *LibraryNotFound) Unwrap() error {
242+
return e.Cause
243+
}
244+
245+
// LibraryDependenciesResolutionFailedError is returned when an inconsistency is found in library dependencies
246+
// or a solution cannot be found.
247+
type LibraryDependenciesResolutionFailedError struct {
248+
Cause error
249+
}
250+
251+
func (e *LibraryDependenciesResolutionFailedError) Error() string {
252+
return composeErrorMsg(tr("No valid dependencies solution found"), e.Cause)
253+
}
254+
255+
func (e *LibraryDependenciesResolutionFailedError) ToRPCStatus() *status.Status {
256+
return status.New(codes.FailedPrecondition, e.Error())
257+
}
258+
259+
func (e *LibraryDependenciesResolutionFailedError) Unwrap() error {
260+
return e.Cause
261+
}
262+
210263
// PlatformAlreadyAtTheLatestVersionError is returned when a platform is up to date
211264
type PlatformAlreadyAtTheLatestVersionError struct {
212265
Platform string
@@ -269,6 +322,23 @@ func (e *FailedInstallError) ToRPCStatus() *status.Status {
269322
return status.New(codes.Internal, e.Error())
270323
}
271324

325+
// FailedLibraryInstallError is returned if a library install operation fails
326+
type FailedLibraryInstallError struct {
327+
Cause error
328+
}
329+
330+
func (e *FailedLibraryInstallError) Error() string {
331+
return composeErrorMsg(tr("Library install failed"), e.Cause)
332+
}
333+
334+
func (e *FailedLibraryInstallError) Unwrap() error {
335+
return e.Cause
336+
}
337+
338+
func (e *FailedLibraryInstallError) ToRPCStatus() *status.Status {
339+
return status.New(codes.Internal, e.Error())
340+
}
341+
272342
// FailedUninstallError is returned if an uninstall operation fails
273343
type FailedUninstallError struct {
274344
Message string

commands/lib/download.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@ import (
2525
"github.com/arduino/arduino-cli/i18n"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2727
"github.com/sirupsen/logrus"
28-
"google.golang.org/grpc/codes"
29-
"google.golang.org/grpc/status"
3028
)
3129

3230
var tr = i18n.Tr
3331

3432
// LibraryDownload FIXMEDOC
35-
func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.LibraryDownloadResponse, *status.Status) {
33+
func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.LibraryDownloadResponse, error) {
3634
logrus.Info("Executing `arduino lib download`")
3735

3836
lm := commands.GetLibraryManager(req.GetInstance().GetId())
37+
if lm == nil {
38+
return nil, &commands.InvalidInstanceError{}
39+
}
3940

4041
logrus.Info("Preparing download")
4142

4243
lib, err := findLibraryIndexRelease(lm, req)
4344
if err != nil {
44-
return nil, status.Newf(codes.InvalidArgument, tr("Error looking for library: %s"), err)
45+
return nil, err
4546
}
4647

4748
if err := downloadLibrary(lm, lib, downloadCB, func(*rpc.TaskProgress) {}); err != nil {
48-
return nil, status.Convert(err)
49+
return nil, err
4950
}
5051

5152
return &rpc.LibraryDownloadResponse{}, nil
@@ -57,12 +58,12 @@ func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librarie
5758
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Downloading %s"), libRelease)})
5859
config, err := commands.GetDownloaderConfig()
5960
if err != nil {
60-
return err
61+
return &commands.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6162
}
6263
if d, err := libRelease.Resource.Download(lm.DownloadsDir, config); err != nil {
63-
return err
64+
return &commands.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6465
} else if err := commands.Download(d, libRelease.String(), downloadCB); err != nil {
65-
return err
66+
return &commands.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6667
}
6768
taskCB(&rpc.TaskProgress{Completed: true})
6869

commands/lib/install.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@ package lib
1717

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

2222
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2323
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2424
"github.com/arduino/arduino-cli/commands"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
"github.com/sirupsen/logrus"
27-
"google.golang.org/grpc/codes"
28-
"google.golang.org/grpc/status"
2927
)
3028

3129
// LibraryInstall FIXMEDOC
3230
func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest,
33-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) *status.Status {
31+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
3432

3533
lm := commands.GetLibraryManager(req.GetInstance().GetId())
34+
if lm == nil {
35+
return &commands.InvalidInstanceError{}
36+
}
3637

3738
toInstall := map[string]*rpc.LibraryDependencyStatus{}
3839
if req.NoDeps {
@@ -47,14 +48,16 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest,
4748
Version: req.Version,
4849
})
4950
if err != nil {
50-
return status.Newf(err.Code(), tr("Error resolving dependencies for %[1]s@%[2]s: %[3]s", req.Name, req.Version, err.Message()))
51+
return err
5152
}
5253

5354
for _, dep := range res.Dependencies {
5455
if existingDep, has := toInstall[dep.Name]; has {
5556
if existingDep.VersionRequired != dep.VersionRequired {
56-
return status.Newf(codes.FailedPrecondition, tr("Two different versions of the library %[1]s are required: %[2]s and %[3]s"),
57-
dep.Name, dep.VersionRequired, existingDep.VersionRequired)
57+
err := errors.New(
58+
tr("two different versions of the library %[1]s are required: %[2]s and %[3]s",
59+
dep.Name, dep.VersionRequired, existingDep.VersionRequired))
60+
return &commands.LibraryDependenciesResolutionFailedError{Cause: err}
5861
}
5962
}
6063
toInstall[dep.Name] = dep
@@ -67,21 +70,20 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest,
6770
Version: lib.VersionRequired,
6871
})
6972
if err != nil {
70-
return status.Newf(codes.InvalidArgument, tr("Error looking for library: %s", err))
73+
return err
7174
}
7275

7376
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
74-
return status.Newf(codes.Unknown, tr("Error downloading library: %s", err))
77+
return err
7578
}
7679

7780
if err := installLibrary(lm, libRelease, taskCB); err != nil {
78-
return status.New(codes.PermissionDenied, err.Error())
81+
return err
7982
}
8083
}
8184

82-
stat := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
83-
if stat != nil {
84-
return status.Newf(stat.Code(), tr("Error rescanning libraries: %s", stat.Err()))
85+
if err := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil); err != nil {
86+
return err
8587
}
8688
return nil
8789
}
@@ -96,36 +98,36 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
9698
}
9799

98100
if err != nil {
99-
return fmt.Errorf(tr("checking lib install prerequisites: %s"), err)
101+
return &commands.FailedInstallError{Message: tr("Checking lib install prerequisites"), Cause: err}
100102
}
101103

102104
if libReplaced != nil {
103105
taskCB(&rpc.TaskProgress{Message: tr("Replacing %[1]s with %[2]s", libReplaced, libRelease)})
104106
}
105107

106108
if err := lm.Install(libRelease, libPath); err != nil {
107-
return err
109+
return &commands.FailedLibraryInstallError{Cause: err}
108110
}
109111

110112
taskCB(&rpc.TaskProgress{Message: tr("Installed %s", libRelease), Completed: true})
111113
return nil
112114
}
113115

114116
//ZipLibraryInstall FIXMEDOC
115-
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, taskCB commands.TaskProgressCB) *status.Status {
117+
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, taskCB commands.TaskProgressCB) error {
116118
lm := commands.GetLibraryManager(req.GetInstance().GetId())
117119
if err := lm.InstallZipLib(ctx, req.Path, req.Overwrite); err != nil {
118-
return status.New(codes.InvalidArgument, err.Error())
120+
return &commands.FailedLibraryInstallError{Cause: err}
119121
}
120122
taskCB(&rpc.TaskProgress{Message: tr("Library installed"), Completed: true})
121123
return nil
122124
}
123125

124126
//GitLibraryInstall FIXMEDOC
125-
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallRequest, taskCB commands.TaskProgressCB) *status.Status {
127+
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallRequest, taskCB commands.TaskProgressCB) error {
126128
lm := commands.GetLibraryManager(req.GetInstance().GetId())
127129
if err := lm.InstallGitLib(req.Url, req.Overwrite); err != nil {
128-
return status.New(codes.InvalidArgument, err.Error())
130+
return &commands.FailedLibraryInstallError{Cause: err}
129131
}
130132
taskCB(&rpc.TaskProgress{Message: tr("Library installed"), Completed: true})
131133
return nil

0 commit comments

Comments
 (0)