Skip to content

Commit 2f920b6

Browse files
committed
arduino#1456 - Extract sketch creating into command and use from CLI and gRPC
1 parent c0a95d7 commit 2f920b6

File tree

4 files changed

+88
-28
lines changed

4 files changed

+88
-28
lines changed

cli/sketch/new.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
package sketch
1717

1818
import (
19-
"io/ioutil"
2019
"os"
21-
"path/filepath"
22-
"strings"
23-
20+
2421
"github.com/arduino/arduino-cli/cli/errorcodes"
2522
"github.com/arduino/arduino-cli/cli/feedback"
23+
sk "github.com/arduino/arduino-cli/commands/sketch"
2624
"github.com/spf13/cobra"
2725
)
2826

@@ -38,32 +36,12 @@ func initNewCommand() *cobra.Command {
3836
return newCommand
3937
}
4038

41-
var emptySketch = []byte(`
42-
void setup() {
43-
}
44-
45-
void loop() {
46-
}
47-
`)
48-
4939
func runNewCommand(cmd *cobra.Command, args []string) {
50-
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
51-
trimmedSketchName := strings.TrimSuffix(args[0], ".ino")
52-
sketchDir, err := filepath.Abs(trimmedSketchName)
53-
if err != nil {
54-
feedback.Errorf(tr("Error creating sketch: %v"), err)
40+
sketchDir, _, err := sk.CreateSketch(args[0]);
41+
if err != nil {
42+
feedback.Errorf(tr("Error creating sketch: %v"), err)
5543
os.Exit(errorcodes.ErrGeneric)
56-
}
57-
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
58-
feedback.Errorf(tr("Could not create sketch directory: %v"), err)
59-
os.Exit(errorcodes.ErrGeneric)
60-
}
61-
sketchName := filepath.Base(sketchDir)
62-
sketchFile := filepath.Join(sketchDir, sketchName+".ino")
63-
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
64-
feedback.Errorf(tr("Error creating sketch: %v"), err)
65-
os.Exit(errorcodes.ErrGeneric)
66-
}
44+
}
6745

6846
feedback.Print(tr("Sketch created in: %s", sketchDir))
6947
}

commands/daemon/daemon.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ func (s *ArduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq
239239
return &rpc.VersionResponse{Version: s.VersionString}, nil
240240
}
241241

242+
// NewSketch FIXMEDOC
243+
func (s *ArduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
244+
resp, err := sketch.NewSketch(ctx, req)
245+
return resp, convertErrorToRPCStatus(err)
246+
}
247+
242248
// LoadSketch FIXMEDOC
243249
func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
244250
resp, err := commands.LoadSketch(ctx, req)

commands/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ func (e *MissingSketchPathError) ToRPCStatus() *status.Status {
323323
return status.New(codes.InvalidArgument, e.Error())
324324
}
325325

326+
// CantCreateSketchError is returned when the sketch cannot be created
327+
type CantCreateSketchError struct {
328+
Cause error
329+
}
330+
331+
func (e *CantCreateSketchError) Error() string {
332+
return composeErrorMsg(tr("Can't create sketch"), e.Cause)
333+
}
334+
335+
func (e *CantCreateSketchError) Unwrap() error {
336+
return e.Cause
337+
}
338+
326339
// CantOpenSketchError is returned when the sketch is not found or cannot be opened
327340
type CantOpenSketchError struct {
328341
Cause error

commands/sketch/new.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package sketch
17+
18+
import (
19+
"context"
20+
"os"
21+
"path/filepath"
22+
"strings"
23+
"io/ioutil"
24+
25+
"github.com/arduino/arduino-cli/commands"
26+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27+
)
28+
29+
var emptySketch = []byte(`
30+
void setup() {
31+
}
32+
33+
void loop() {
34+
}
35+
`)
36+
37+
func CreateSketch(sketchName string) (string, string, error) {
38+
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
39+
trimmedSketchName := strings.TrimSuffix(sketchName, ".ino")
40+
sketchDir, err := filepath.Abs(trimmedSketchName)
41+
if err != nil {
42+
return "", "", err
43+
}
44+
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
45+
return "", "", err
46+
}
47+
baseSketchName := filepath.Base(sketchDir)
48+
sketchFile := filepath.Join(sketchDir, baseSketchName+".ino")
49+
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
50+
return "", "", err
51+
}
52+
return sketchDir, sketchFile, nil
53+
}
54+
55+
// NewSketch FIXMEDOC
56+
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
57+
_, sketchFile, err := CreateSketch(req.SketchName);
58+
if err != nil {
59+
return nil, &commands.CantCreateSketchError{Cause: err}
60+
}
61+
62+
return &rpc.NewSketchResponse{MainFile: sketchFile}, nil
63+
}

0 commit comments

Comments
 (0)