Skip to content

Commit 0bc3bb3

Browse files
committed
Refactoring 'sketch' commands
1 parent 6c93cea commit 0bc3bb3

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

Diff for: commands/daemon/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func (s *ArduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.Librar
438438
// ArchiveSketch FIXMEDOC
439439
func (s *ArduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
440440
resp, err := sketch.ArchiveSketch(ctx, req)
441-
return resp, err.Err()
441+
return resp, convertErrorToRPCStatus(err)
442442
}
443443

444444
//ZipLibraryInstall FIXMEDOC

Diff for: commands/sketch/archive.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ import (
2323
"strings"
2424

2525
"github.com/arduino/arduino-cli/arduino/sketch"
26+
"github.com/arduino/arduino-cli/commands"
2627
"github.com/arduino/arduino-cli/i18n"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2829
paths "github.com/arduino/go-paths-helper"
29-
"google.golang.org/grpc/codes"
30-
"google.golang.org/grpc/status"
3130
)
3231

3332
var tr = i18n.Tr
3433

3534
// ArchiveSketch FIXMEDOC
36-
func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, *status.Status) {
35+
func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
3736
// sketchName is the name of the sketch without extension, for example "MySketch"
3837
var sketchName string
3938

@@ -44,7 +43,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
4443

4544
s, err := sketch.New(sketchPath)
4645
if err != nil {
47-
return nil, status.Convert(err)
46+
return nil, &commands.SketchNotFoundError{Cause: err}
4847
}
4948

5049
sketchPath = s.FullPath
@@ -57,7 +56,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
5756

5857
archivePath, err = archivePath.Clean().Abs()
5958
if err != nil {
60-
return nil, status.Newf(codes.Unknown, tr("Error getting absolute archive path %v"), err)
59+
return nil, &commands.PermissionDeniedError{Message: tr("Error getting absolute path of sketch archive"), Cause: err}
6160
}
6261

6362
// Makes archivePath point to a zip file
@@ -68,18 +67,18 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
6867
}
6968

7069
if archivePath.Exist() {
71-
return nil, status.New(codes.InvalidArgument, tr("Archive already exists"))
70+
return nil, &commands.InvalidArgumentError{Message: tr("Archive already exists")}
7271
}
7372

7473
filesToZip, err := sketchPath.ReadDirRecursive()
7574
if err != nil {
76-
return nil, status.Newf(codes.Unknown, tr("Error retrieving sketch files: %v"), err)
75+
return nil, &commands.PermissionDeniedError{Message: tr("Error reading sketch files"), Cause: err}
7776
}
7877
filesToZip.FilterOutDirs()
7978

8079
archive, err := archivePath.Create()
8180
if err != nil {
82-
return nil, status.Newf(codes.PermissionDenied, tr("Error creating archive: %v"), err)
81+
return nil, &commands.PermissionDeniedError{Message: tr("Error creating sketch archive"), Cause: err}
8382
}
8483
defer archive.Close()
8584

@@ -91,7 +90,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
9190
if !req.IncludeBuildDir {
9291
filePath, err := sketchPath.Parent().RelTo(f)
9392
if err != nil {
94-
return nil, status.Newf(codes.Unknown, tr("Error calculating relative file path: %v"), err)
93+
return nil, &commands.PermissionDeniedError{Message: tr("Error calculating relative file path"), Cause: err}
9594
}
9695

9796
// Skips build folder
@@ -102,9 +101,8 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
102101

103102
// We get the parent path since we want the archive to unpack as a folder.
104103
// If we don't do this the archive would contain all the sketch files as top level.
105-
err = addFileToSketchArchive(zipWriter, f, sketchPath.Parent())
106-
if err != nil {
107-
return nil, status.Newf(codes.Unknown, tr("Error adding file to archive: %v"), err)
104+
if err := addFileToSketchArchive(zipWriter, f, sketchPath.Parent()); err != nil {
105+
return nil, &commands.PermissionDeniedError{Message: tr("Error adding file to sketch archive"), Cause: err}
108106
}
109107
}
110108

0 commit comments

Comments
 (0)