Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c93cea

Browse files
committedAug 27, 2021
Refactoring 'lib' commands
1 parent 567b977 commit 6c93cea

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

‎commands/lib/list.go

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

3230
type installedLib struct {
@@ -35,15 +33,15 @@ type installedLib struct {
3533
}
3634

3735
// LibraryList FIXMEDOC
38-
func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, *status.Status) {
36+
func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
3937
pm := commands.GetPackageManager(req.GetInstance().GetId())
4038
if pm == nil {
41-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
39+
return nil, &commands.InvalidInstanceError{}
4240
}
4341

4442
lm := commands.GetLibraryManager(req.GetInstance().GetId())
4543
if lm == nil {
46-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
44+
return nil, &commands.InvalidInstanceError{}
4745
}
4846

4947
nameFilter := strings.ToLower(req.GetName())
@@ -53,11 +51,11 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.Library
5351
if f := req.GetFqbn(); f != "" {
5452
fqbn, err := cores.ParseFQBN(req.GetFqbn())
5553
if err != nil {
56-
return nil, status.Newf(codes.InvalidArgument, tr("Error parsing FQBN: %s", err))
54+
return nil, &commands.InvalidFQBNError{Cause: err}
5755
}
5856
_, boardPlatform, _, _, refBoardPlatform, err := pm.ResolveFQBN(fqbn)
5957
if err != nil {
60-
return nil, status.Newf(codes.InvalidArgument, tr("Error loading board data: %s", err))
58+
return nil, &commands.UnknownFQBNError{Cause: err}
6159
}
6260

6361
filteredRes := map[string]*installedLib{}
@@ -105,7 +103,7 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.Library
105103
}
106104
rpcLib, err := lib.Library.ToRPCLibrary()
107105
if err != nil {
108-
return nil, status.Newf(codes.PermissionDenied, tr("Error converting library %[1]s to rpc struct: %[2]s", lib.Library.Name, err))
106+
return nil, &commands.PermissionDeniedError{Message: tr("Error getting information for library %s", lib.Library.Name), Cause: err}
109107
}
110108
instaledLibs = append(instaledLibs, &rpc.InstalledLibrary{
111109
Library: rpcLib,

‎commands/lib/resolve_deps.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ package lib
1717

1818
import (
1919
"context"
20+
"errors"
2021

2122
"github.com/arduino/arduino-cli/arduino/libraries"
2223
"github.com/arduino/arduino-cli/commands"
2324
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24-
"google.golang.org/grpc/codes"
25-
"google.golang.org/grpc/status"
2625
)
2726

2827
// LibraryResolveDependencies FIXMEDOC
29-
func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, *status.Status) {
28+
func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
3029
lm := commands.GetLibraryManager(req.GetInstance().GetId())
30+
if lm == nil {
31+
return nil, &commands.InvalidInstanceError{}
32+
}
3133

3234
// Search the requested lib
3335
reqLibRelease, err := findLibraryIndexRelease(lm, req)
3436
if err != nil {
35-
return nil, status.Newf(codes.InvalidArgument, tr("Error looking for library: %s", err))
37+
return nil, err
3638
}
3739

3840
// Extract all installed libraries
@@ -49,12 +51,13 @@ func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDepe
4951
// Check if there is a problem with the first level deps
5052
for _, directDep := range reqLibRelease.GetDependencies() {
5153
if _, ok := lm.Index.Libraries[directDep.GetName()]; !ok {
52-
return nil, status.Newf(codes.FailedPrecondition, tr("Dependency '%s' is not available", directDep.GetName()))
54+
err := errors.New(tr("dependency '%s' is not available", directDep.GetName()))
55+
return nil, &commands.LibraryDependenciesResolutionFailedError{Cause: err}
5356
}
5457
}
5558

5659
// Otherwise there is no possible solution, the depends field has an invalid formula
57-
return nil, status.New(codes.FailedPrecondition, tr("No valid dependencies solution found"))
60+
return nil, &commands.LibraryDependenciesResolutionFailedError{}
5861
}
5962

6063
res := []*rpc.LibraryDependencyStatus{}

‎commands/lib/search.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,18 @@ import (
2424
"github.com/arduino/arduino-cli/commands"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
semver "go.bug.st/relaxed-semver"
27-
"google.golang.org/grpc/codes"
28-
"google.golang.org/grpc/status"
2927
)
3028

3129
// LibrarySearch FIXMEDOC
32-
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, *status.Status) {
30+
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
3331
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3432
if lm == nil {
35-
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
33+
return nil, &commands.InvalidInstanceError{}
3634
}
37-
38-
resp, err := searchLibrary(req, lm)
39-
return resp, status.Convert(err)
35+
return searchLibrary(req, lm), nil
4036
}
4137

42-
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) (*rpc.LibrarySearchResponse, error) {
38+
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse {
4339
res := []*rpc.SearchedLibrary{}
4440
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
4541

@@ -51,7 +47,7 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
5147
res = append(res, indexLibraryToRPCSearchLibrary(lib))
5248
}
5349

54-
return &rpc.LibrarySearchResponse{Libraries: res, Status: status}, nil
50+
return &rpc.LibrarySearchResponse{Libraries: res, Status: status}
5551
}
5652

5753
// indexLibraryToRPCSearchLibrary converts a librariindex.Library to rpc.SearchLibrary

‎commands/lib/search_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ func TestSearchLibrary(t *testing.T) {
2121
Query: "test",
2222
}
2323

24-
resp, err := searchLibrary(req, lm)
25-
if err != nil {
26-
t.Fatal(err)
27-
}
28-
24+
resp := searchLibrary(req, lm)
2925
assert := assert.New(t)
3026
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS)
3127
assert.Equal(len(resp.GetLibraries()), 2)
@@ -42,11 +38,7 @@ func TestSearchLibrarySimilar(t *testing.T) {
4238
Query: "arduino",
4339
}
4440

45-
resp, err := searchLibrary(req, lm)
46-
if err != nil {
47-
t.Fatal(err)
48-
}
49-
41+
resp := searchLibrary(req, lm)
5042
assert := assert.New(t)
5143
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS)
5244
assert.Equal(len(resp.GetLibraries()), 2)

‎commands/lib/uninstall.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ import (
2121

2222
"github.com/arduino/arduino-cli/commands"
2323
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24-
"google.golang.org/grpc/codes"
25-
"google.golang.org/grpc/status"
2624
)
2725

2826
// LibraryUninstall FIXMEDOC
29-
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, taskCB commands.TaskProgressCB) *status.Status {
27+
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, taskCB commands.TaskProgressCB) error {
3028
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3129
ref, err := createLibIndexReference(lm, req)
3230
if err != nil {
33-
return status.New(codes.InvalidArgument, err.Error())
31+
return &commands.InvalidLibraryError{Cause: err}
3432
}
3533

3634
lib := lm.FindByReference(ref)

‎commands/lib/upgrade.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,42 @@ import (
1919
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2020
"github.com/arduino/arduino-cli/commands"
2121
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
22-
"google.golang.org/grpc/status"
2322
)
2423

2524
// LibraryUpgradeAll upgrades all the available libraries
26-
func LibraryUpgradeAll(instanceID int32, downloadCB commands.DownloadProgressCB,
27-
taskCB commands.TaskProgressCB) *status.Status {
28-
// get the library manager
25+
func LibraryUpgradeAll(instanceID int32, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
2926
lm := commands.GetLibraryManager(instanceID)
27+
if lm == nil {
28+
return &commands.InvalidInstanceError{}
29+
}
3030

3131
if err := upgrade(lm, listLibraries(lm, true, true), downloadCB, taskCB); err != nil {
32-
return status.Convert(err)
32+
return err
3333
}
3434

35-
stat := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: instanceID}}, nil)
36-
if stat != nil {
37-
return status.Newf(stat.Code(), tr("Error rescanning libraries: %s", stat.Err()))
35+
if err := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: instanceID}}, nil); err != nil {
36+
return err
3837
}
3938

4039
return nil
4140
}
4241

4342
// LibraryUpgrade upgrades only the given libraries
44-
func LibraryUpgrade(instanceID int32, libraryNames []string, downloadCB commands.DownloadProgressCB,
45-
taskCB commands.TaskProgressCB) *status.Status {
46-
// get the library manager
43+
func LibraryUpgrade(instanceID int32, libraryNames []string, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
4744
lm := commands.GetLibraryManager(instanceID)
45+
if lm == nil {
46+
return &commands.InvalidInstanceError{}
47+
}
4848

4949
// get the libs to upgrade
5050
libs := filterByName(listLibraries(lm, true, true), libraryNames)
5151

5252
// do it
53-
return status.Convert(upgrade(lm, libs, downloadCB, taskCB))
53+
return upgrade(lm, libs, downloadCB, taskCB)
5454
}
5555

56-
func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB commands.DownloadProgressCB,
57-
taskCB commands.TaskProgressCB) error {
58-
56+
func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
5957
// Go through the list and download them
60-
6158
for _, lib := range libs {
6259
if err := downloadLibrary(lm, lib.Available, downloadCB, taskCB); err != nil {
6360
return err

‎commands/lib/utils.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package lib
1717

1818
import (
19-
"fmt"
20-
2119
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2220
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2321
"github.com/arduino/arduino-cli/commands"
@@ -31,7 +29,7 @@ type libraryReferencer interface {
3129
func createLibIndexReference(lm *librariesmanager.LibrariesManager, req libraryReferencer) (*librariesindex.Reference, error) {
3230
version, err := commands.ParseVersion(req)
3331
if err != nil {
34-
return nil, fmt.Errorf(tr("invalid version: %s"), err)
32+
return nil, &commands.InvalidVersionError{Cause: err}
3533
}
3634

3735
return &librariesindex.Reference{Name: req.GetName(), Version: version}, nil
@@ -44,7 +42,7 @@ func findLibraryIndexRelease(lm *librariesmanager.LibrariesManager, req libraryR
4442
}
4543
lib := lm.Index.FindRelease(ref)
4644
if lib == nil {
47-
return nil, fmt.Errorf(tr("library %s not found"), ref)
45+
return nil, &commands.LibraryNotFound{Library: ref.String()}
4846
}
4947
return lib, nil
5048
}

0 commit comments

Comments
 (0)
Please sign in to comment.