Skip to content

Commit 9204eda

Browse files
per1234cmaglie
authored andcommitted
Update gRPC API of remaining commands to return status.Status instead of error
1 parent 0402f62 commit 9204eda

28 files changed

+260
-236
lines changed

Diff for: cli/compile/compile.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func run(cmd *cobra.Command, args []string) {
180180
compileErr := new(bytes.Buffer)
181181
verboseCompile := configuration.Settings.GetString("logging.level") == "debug"
182182
var compileRes *rpc.CompileResponse
183-
var err error
183+
var err *status.Status
184184
if output.OutputFormat == "json" {
185185
compileRes, err = compile.Compile(context.Background(), compileRequest, compileOut, compileErr, verboseCompile)
186186
} else {
@@ -198,7 +198,6 @@ func run(cmd *cobra.Command, args []string) {
198198
ImportDir: buildPath,
199199
Programmer: programmer,
200200
}
201-
var err *status.Status
202201
if output.OutputFormat == "json" {
203202
// TODO: do not print upload output in json mode
204203
uploadOut := new(bytes.Buffer)

Diff for: cli/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9494
}
9595

9696
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
97-
if err == core.ErrAlreadyLatest {
97+
if err.Message() == core.ErrAlreadyLatest.Error() {
9898
feedback.Printf("Platform %s is already at the latest version", platformRef)
9999
} else if err != nil {
100100
feedback.Errorf("Error during upgrade: %v", err)

Diff for: cli/debug/debug.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/fatih/color"
3434
"github.com/sirupsen/logrus"
3535
"github.com/spf13/cobra"
36-
"google.golang.org/grpc/status"
3736
)
3837

3938
var (
@@ -90,12 +89,8 @@ func run(command *cobra.Command, args []string) {
9089
if printInfo {
9190

9291
if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
93-
if status, ok := status.FromError(err); ok {
94-
feedback.Errorf("Error getting Debug info: %v", status.Message())
95-
errorcodes.ExitWithGrpcStatus(status)
96-
}
97-
feedback.Errorf("Error getting Debug info: %v", err)
98-
os.Exit(errorcodes.ErrGeneric)
92+
feedback.Errorf("Error getting Debug info: %v", err.Message())
93+
errorcodes.ExitWithGrpcStatus(err)
9994
} else {
10095
feedback.PrintResult(&debugInfoResult{res})
10196
}

Diff for: cli/instance/instance.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/configuration"
2727
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2828
"github.com/arduino/go-paths-helper"
29-
"google.golang.org/grpc/codes"
3029
"google.golang.org/grpc/status"
3130
)
3231

@@ -117,7 +116,7 @@ func FirstUpdate(instance *rpc.Instance) *status.Status {
117116
output.ProgressBar(),
118117
)
119118
if err != nil {
120-
return status.Newf(codes.FailedPrecondition, err.Error())
119+
return err
121120
}
122121
}
123122

@@ -132,7 +131,7 @@ func FirstUpdate(instance *rpc.Instance) *status.Status {
132131
output.ProgressBar(),
133132
)
134133
if err != nil {
135-
return status.Newf(codes.FailedPrecondition, err.Error())
134+
return err
136135
}
137136
}
138137

Diff for: cli/lib/args.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ func ParseLibraryReferenceArgs(args []string) ([]*LibraryReferenceArg, error) {
7575
// ParseLibraryReferenceArgAndAdjustCase parse a command line argument that reference a
7676
// library and possibly adjust the case of the name to match a library in the index
7777
func ParseLibraryReferenceArgAndAdjustCase(instance *rpc.Instance, arg string) (*LibraryReferenceArg, error) {
78-
libRef, err := ParseLibraryReferenceArg(arg)
78+
libRef, _ := ParseLibraryReferenceArg(arg)
7979
res, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
8080
Instance: instance,
8181
Query: libRef.Name,
8282
})
8383
if err != nil {
84-
return nil, err
84+
return nil, err.Err()
8585
}
8686

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

Diff for: cli/lib/check_deps.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func runDepsCommand(cmd *cobra.Command, args []string) {
5151
os.Exit(errorcodes.ErrBadArgument)
5252
}
5353

54-
deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesRequest{
54+
deps, stat := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesRequest{
5555
Instance: instance,
5656
Name: libRef.Name,
5757
Version: libRef.Version,
5858
})
59-
if err != nil {
59+
if stat != nil {
6060
feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err)
6161
}
6262

Diff for: commands/compile/compile.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ import (
4040
"github.com/pkg/errors"
4141
"github.com/segmentio/stats/v4"
4242
"github.com/sirupsen/logrus"
43+
"google.golang.org/grpc/codes"
44+
"google.golang.org/grpc/status"
4345
)
4446

4547
// Compile FIXMEDOC
46-
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResponse, e error) {
48+
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResponse, e *status.Status) {
4749

4850
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
4951
// since we want this binding to work also for the gRPC interface we must read it here in this
@@ -87,29 +89,29 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
8789

8890
pm := commands.GetPackageManager(req.GetInstance().GetId())
8991
if pm == nil {
90-
return nil, errors.New("invalid instance")
92+
return nil, status.New(codes.InvalidArgument, "invalid instance")
9193
}
9294

9395
logrus.Tracef("Compile %s for %s started", req.GetSketchPath(), req.GetFqbn())
9496
if req.GetSketchPath() == "" {
95-
return nil, fmt.Errorf("missing sketchPath")
97+
return nil, status.New(codes.InvalidArgument, "missing sketchPath")
9698
}
9799
sketchPath := paths.New(req.GetSketchPath())
98100
sk, err := sketch.New(sketchPath)
99101
if err != nil {
100-
return nil, fmt.Errorf("opening sketch: %s", err)
102+
return nil, status.Newf(codes.NotFound, "opening sketch: %s", err)
101103
}
102104

103105
fqbnIn := req.GetFqbn()
104106
if fqbnIn == "" && sk != nil && sk.Metadata != nil {
105107
fqbnIn = sk.Metadata.CPU.Fqbn
106108
}
107109
if fqbnIn == "" {
108-
return nil, fmt.Errorf("no FQBN provided")
110+
return nil, status.New(codes.InvalidArgument, "no FQBN provided")
109111
}
110112
fqbn, err := cores.ParseFQBN(fqbnIn)
111113
if err != nil {
112-
return nil, fmt.Errorf("incorrect FQBN: %s", err)
114+
return nil, status.Newf(codes.InvalidArgument, "incorrect FQBN: %s", err)
113115
}
114116

115117
targetPlatform := pm.FindPlatform(&packagemanager.PlatformReference{
@@ -122,7 +124,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
122124
// "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
123125
// version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
124126
// feedback.Error(errorMessage)
125-
return nil, fmt.Errorf("platform not installed")
127+
return nil, status.New(codes.NotFound, "platform not installed")
126128
}
127129

128130
builderCtx := &types.Context{}
@@ -145,7 +147,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
145147
builderCtx.BuildPath = paths.New(req.GetBuildPath())
146148
}
147149
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
148-
return nil, fmt.Errorf("cannot create build directory: %s", err)
150+
return nil, status.Newf(codes.PermissionDenied, "cannot create build directory: %s", err)
149151
}
150152
builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
151153
builderCtx.BuildPath.Join("compile_commands.json"),
@@ -175,7 +177,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
175177
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
176178
err = builderCtx.BuildCachePath.MkdirAll()
177179
if err != nil {
178-
return nil, fmt.Errorf("cannot create build cache directory: %s", err)
180+
return nil, status.Newf(codes.PermissionDenied, "cannot create build cache directory: %s", err)
179181
}
180182
}
181183

@@ -218,14 +220,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
218220

219221
// if --preprocess or --show-properties were passed, we can stop here
220222
if req.GetShowProperties() {
221-
return r, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
223+
return r, status.Convert(builder.RunParseHardwareAndDumpBuildProperties(builderCtx))
222224
} else if req.GetPreprocess() {
223-
return r, builder.RunPreprocess(builderCtx)
225+
return r, status.Convert(builder.RunPreprocess(builderCtx))
224226
}
225227

226228
// if it's a regular build, go on...
227229
if err := builder.RunBuilder(builderCtx); err != nil {
228-
return r, err
230+
return r, status.Convert(err)
229231
}
230232

231233
// If the export directory is set we assume you want to export the binaries
@@ -247,17 +249,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
247249
}
248250
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
249251
if err := exportPath.MkdirAll(); err != nil {
250-
return r, errors.Wrap(err, "creating output dir")
252+
return r, status.New(codes.PermissionDenied, errors.Wrap(err, "creating output dir").Error())
251253
}
252254

253255
// Copy all "sketch.ino.*" artifacts to the export directory
254256
baseName, ok := builderCtx.BuildProperties.GetOk("build.project_name") // == "sketch.ino"
255257
if !ok {
256-
return r, errors.New("missing 'build.project_name' build property")
258+
return r, status.New(codes.Internal, "missing 'build.project_name' build property")
257259
}
258260
buildFiles, err := builderCtx.BuildPath.ReadDir()
259261
if err != nil {
260-
return r, errors.Errorf("reading build directory: %s", err)
262+
return r, status.Newf(codes.PermissionDenied, "reading build directory: %s", err)
261263
}
262264
buildFiles.FilterPrefix(baseName)
263265
for _, buildFile := range buildFiles {
@@ -267,7 +269,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
267269
WithField("dest", exportedFile).
268270
Trace("Copying artifact.")
269271
if err = buildFile.CopyTo(exportedFile); err != nil {
270-
return r, errors.Wrapf(err, "copying output file %s", buildFile)
272+
return r, status.New(codes.PermissionDenied, errors.Wrapf(err, "copying output file %s", buildFile).Error())
271273
}
272274
}
273275
}
@@ -276,7 +278,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
276278
for _, lib := range builderCtx.ImportedLibraries {
277279
rpcLib, err := lib.ToRPCLibrary()
278280
if err != nil {
279-
return r, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
281+
return r, status.Newf(codes.PermissionDenied, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err).Error())
280282
}
281283
importedLibs = append(importedLibs, rpcLib)
282284
}

Diff for: commands/core/download.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ package core
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221

2322
"github.com/arduino/arduino-cli/arduino/cores"
2423
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2524
"github.com/arduino/arduino-cli/commands"
2625
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26+
"google.golang.org/grpc/codes"
27+
"google.golang.org/grpc/status"
2728
)
2829

2930
// PlatformDownload FIXMEDOC
30-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
31+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, *status.Status) {
3132
pm := commands.GetPackageManager(req.GetInstance().GetId())
3233
if pm == nil {
33-
return nil, errors.New("invalid instance")
34+
return nil, status.New(codes.InvalidArgument, "invalid instance")
3435
}
3536

3637
version, err := commands.ParseVersion(req)
3738
if err != nil {
38-
return nil, fmt.Errorf("invalid version: %s", err)
39+
return nil, status.Newf(codes.InvalidArgument, "invalid version: %s", err)
3940
}
4041

4142
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
@@ -44,18 +45,18 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
4445
PlatformVersion: version,
4546
})
4647
if err != nil {
47-
return nil, fmt.Errorf("find platform dependencies: %s", err)
48+
return nil, status.Newf(codes.InvalidArgument, "find platform dependencies: %s", err)
4849
}
4950

5051
err = downloadPlatform(pm, platform, downloadCB)
5152
if err != nil {
52-
return nil, err
53+
return nil, status.New(codes.Unavailable, err.Error())
5354
}
5455

5556
for _, tool := range tools {
5657
err := downloadTool(pm, tool, downloadCB)
5758
if err != nil {
58-
return nil, fmt.Errorf("downloading tool %s: %s", tool, err)
59+
return nil, status.Newf(codes.Unavailable, "downloading tool %s: %s", tool, err)
5960
}
6061
}
6162

Diff for: commands/core/install.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ 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-
"github.com/pkg/errors"
26+
"google.golang.org/grpc/codes"
27+
"google.golang.org/grpc/status"
2728
)
2829

2930
// PlatformInstall FIXMEDOC
3031
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
31-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
32+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, *status.Status) {
3233

3334
pm := commands.GetPackageManager(req.GetInstance().GetId())
3435
if pm == nil {
35-
return nil, errors.New("invalid instance")
36+
return nil, status.New(codes.InvalidArgument, "invalid instance")
3637
}
3738

3839
version, err := commands.ParseVersion(req)
3940
if err != nil {
40-
return nil, fmt.Errorf("invalid version: %s", err)
41+
return nil, status.Newf(codes.InvalidArgument, "invalid version: %s", err)
4142
}
4243

4344
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
@@ -46,17 +47,17 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
4647
PlatformVersion: version,
4748
})
4849
if err != nil {
49-
return nil, fmt.Errorf("finding platform dependencies: %s", err)
50+
return nil, status.Newf(codes.InvalidArgument, "finding platform dependencies: %s", err)
5051
}
5152

5253
err = installPlatform(pm, platform, tools, downloadCB, taskCB, req.GetSkipPostInstall())
5354
if err != nil {
54-
return nil, err
55+
return nil, status.Convert(err)
5556
}
5657

5758
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
5859
if status != nil {
59-
return nil, status.Err()
60+
return nil, status
6061
}
6162

6263
return &rpc.PlatformInstallResponse{}, nil

Diff for: commands/core/list.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ import (
2121

2222
"github.com/arduino/arduino-cli/commands"
2323
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24-
"github.com/pkg/errors"
24+
"google.golang.org/grpc/codes"
25+
"google.golang.org/grpc/status"
2526
)
2627

2728
// GetPlatforms returns a list of installed platforms, optionally filtered by
2829
// those requiring an update.
29-
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, error) {
30+
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, *status.Status) {
3031
instanceID := req.Instance.Id
3132
i := commands.GetInstance(instanceID)
3233
if i == nil {
33-
return nil, errors.Errorf("unable to find an instance with ID: %d", instanceID)
34+
return nil, status.Newf(codes.InvalidArgument, "unable to find an instance with ID: %d", instanceID)
3435
}
3536

3637
packageManager := i.PackageManager
3738
if packageManager == nil {
38-
return nil, errors.New("invalid instance")
39+
return nil, status.New(codes.InvalidArgument, "invalid instance")
3940
}
4041

4142
res := []*rpc.Platform{}

0 commit comments

Comments
 (0)