Skip to content

Commit 3384213

Browse files
committed
arduino#1456 - Add sketches directory to the Settings (to be used when called via gRPC)
1 parent 920a3e7 commit 3384213

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

Diff for: cli/config/validate.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var validMap = map[string]reflect.Kind{
2626
"directories.data": reflect.String,
2727
"directories.downloads": reflect.String,
2828
"directories.user": reflect.String,
29+
"directories.sketches": reflect.String,
2930
"library.enable_unsafe_install": reflect.Bool,
3031
"logging.file": reflect.String,
3132
"logging.format": reflect.String,

Diff for: cli/sketch/new.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package sketch
1717

1818
import (
1919
"os"
20+
"strings"
21+
"path/filepath"
2022

2123
"github.com/arduino/arduino-cli/cli/errorcodes"
2224
"github.com/arduino/arduino-cli/cli/feedback"
@@ -37,9 +39,17 @@ func initNewCommand() *cobra.Command {
3739
}
3840

3941
func runNewCommand(cmd *cobra.Command, args []string) {
40-
sketchDir, _, err := sk.CreateSketch(args[0])
42+
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
43+
sketchName := args[0];
44+
trimmedSketchName := strings.TrimSuffix(sketchName, ".ino")
45+
sketchDir, err := filepath.Abs(trimmedSketchName)
4146
if err != nil {
42-
feedback.Errorf("Error creating sketch: %v", err) // TODO: use `tr()`
47+
feedback.Errorf(tr("Error creating sketch: %v"), err)
48+
os.Exit(errorcodes.ErrGeneric)
49+
}
50+
_, err = sk.CreateSketch(sketchDir, sketchName)
51+
if err != nil {
52+
feedback.Errorf(tr("Error creating sketch: %v"), err)
4353
os.Exit(errorcodes.ErrGeneric)
4454
}
4555

Diff for: commands/sketch/new.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"io/ioutil"
2121
"os"
2222
"path/filepath"
23-
"strings"
2423

2524
"github.com/arduino/arduino-cli/commands"
25+
"github.com/arduino/arduino-cli/configuration"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2727
)
2828

@@ -35,27 +35,22 @@ void loop() {
3535
`)
3636

3737
// CreateSketch creates a new sketch
38-
func CreateSketch(sketchName string) (string, string, error) {
39-
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
40-
trimmedSketchName := strings.TrimSuffix(sketchName, ".ino")
41-
sketchDir, err := filepath.Abs(trimmedSketchName)
42-
if err != nil {
43-
return "", "", err
44-
}
38+
func CreateSketch(sketchDir string, sketchName string) (string, error) {
4539
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
46-
return "", "", err
40+
return "", err
4741
}
4842
baseSketchName := filepath.Base(sketchDir)
4943
sketchFile := filepath.Join(sketchDir, baseSketchName+".ino")
5044
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
51-
return "", "", err
45+
return "", err
5246
}
53-
return sketchDir, sketchFile, nil
47+
return sketchFile, nil
5448
}
5549

5650
// NewSketch FIXMEDOC
5751
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
58-
_, sketchFile, err := CreateSketch(req.SketchName)
52+
sketchDir := configuration.Settings.GetString("directories.Sketches")
53+
sketchFile, err := CreateSketch(sketchDir, req.SketchName)
5954
if err != nil {
6055
return nil, &commands.CantCreateSketchError{Cause: err}
6156
}

Diff for: configuration/configuration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func TestInit(t *testing.T) {
9393
require.NotEmpty(t, settings.GetString("directories.Data"))
9494
require.NotEmpty(t, settings.GetString("directories.Downloads"))
9595
require.NotEmpty(t, settings.GetString("directories.User"))
96+
require.NotEmpty(t, settings.GetString("directories.Sketches"))
9697

9798
require.Equal(t, "50051", settings.GetString("daemon.port"))
9899

Diff for: configuration/defaults.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func SetDefaults(settings *viper.Viper) {
3838
settings.SetDefault("directories.Data", getDefaultArduinoDataDir())
3939
settings.SetDefault("directories.Downloads", filepath.Join(getDefaultArduinoDataDir(), "staging"))
4040
settings.SetDefault("directories.User", getDefaultUserDir())
41+
settings.SetDefault("directories.Sketches",
42+
filepath.Join(getDefaultUserDir(), "sketches"))
4143

4244
// Sketch compilation
4345
settings.SetDefault("sketch.always_export_binaries", false)

Diff for: docs/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `downloads` - directory used to stage downloaded archives during Boards/Library Manager installations.
1010
- `user` - the equivalent of the Arduino IDE's ["sketchbook" directory][sketchbook directory]. Library Manager
1111
installations are made to the `libraries` subdirectory of the user directory.
12+
- `sketches` - directory used to store user sketches if called via gRPC.
1213
- `library` - configuration options relating to Arduino libraries.
1314
- `enable_unsafe_install` - set to `true` to enable the use of the `--git-url` and `--zip-file` flags with
1415
[`arduino-cli lib install`][arduino cli lib install]. These are considered "unsafe" installation methods because

0 commit comments

Comments
 (0)