Skip to content

Commit 98547d9

Browse files
committed
#1456 - Use go-paths-helper
1 parent 5e61200 commit 98547d9

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

Diff for: cli/sketch/new.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ package sketch
1717

1818
import (
1919
"os"
20-
"path/filepath"
2120
"strings"
2221

2322
"github.com/arduino/arduino-cli/arduino/globals"
2423
"github.com/arduino/arduino-cli/cli/errorcodes"
2524
"github.com/arduino/arduino-cli/cli/feedback"
25+
paths "github.com/arduino/go-paths-helper"
2626
sk "github.com/arduino/arduino-cli/commands/sketch"
2727
"github.com/spf13/cobra"
2828
)
@@ -43,16 +43,16 @@ func runNewCommand(cmd *cobra.Command, args []string) {
4343
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
4444
sketchName := args[0]
4545
trimmedSketchName := strings.TrimSuffix(sketchName, globals.MainFileValidExtension)
46-
sketchDir, err := filepath.Abs(trimmedSketchName)
46+
sketchDirPath, err := paths.New(trimmedSketchName).Abs()
4747
if err != nil {
4848
feedback.Errorf(tr("Error creating sketch: %v"), err)
4949
os.Exit(errorcodes.ErrGeneric)
5050
}
51-
_, err = sk.CreateSketch(sketchDir, sketchName)
51+
_, err = sk.CreateSketch(sketchDirPath)
5252
if err != nil {
5353
feedback.Errorf(tr("Error creating sketch: %v"), err)
5454
os.Exit(errorcodes.ErrGeneric)
5555
}
5656

57-
feedback.Print(tr("Sketch created in: %s", sketchDir))
57+
feedback.Print(tr("Sketch created in: %s", sketchDirPath.String()))
5858
}

Diff for: commands/sketch/new.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ package sketch
1717

1818
import (
1919
"context"
20-
"io/ioutil"
21-
"os"
22-
"path/filepath"
2320

2421
"github.com/arduino/arduino-cli/arduino/globals"
2522
"github.com/arduino/arduino-cli/commands"
2623
"github.com/arduino/arduino-cli/configuration"
24+
paths "github.com/arduino/go-paths-helper"
2725
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2826
)
2927

@@ -36,31 +34,31 @@ void loop() {
3634
`)
3735

3836
// CreateSketch creates a new sketch
39-
func CreateSketch(sketchDir string, sketchName string) (string, error) {
40-
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
41-
return "", err
37+
func CreateSketch(sketchDirPath *paths.Path) (*paths.Path, error) {
38+
if err := sketchDirPath.MkdirAll(); err != nil {
39+
return nil, err
4240
}
43-
baseSketchName := filepath.Base(sketchDir)
44-
sketchFile := filepath.Join(sketchDir, baseSketchName+globals.MainFileValidExtension)
45-
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
46-
return "", err
41+
baseSketchName := sketchDirPath.Base()
42+
sketchFilePath := sketchDirPath.Join(baseSketchName+globals.MainFileValidExtension)
43+
if err := sketchFilePath.WriteFile(emptySketch); err != nil {
44+
return nil, err
4745
}
48-
return sketchFile, nil
46+
return sketchFilePath, nil
4947
}
5048

51-
// NewSketch FIXMEDOC
49+
// NewSketch creates a new sketch via gRPC
5250
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
5351
var sketchesDir string
5452
if len(req.SketchDir) > 0 {
5553
sketchesDir = req.SketchDir
5654
} else {
5755
sketchesDir = configuration.Settings.GetString("directories.User")
5856
}
59-
sketchDir := filepath.Join(sketchesDir, req.SketchName)
60-
sketchFile, err := CreateSketch(sketchDir, req.SketchName)
57+
sketchDirPath := paths.New(sketchesDir).Join(req.SketchName)
58+
sketchFilePath, err := CreateSketch(sketchDirPath)
6159
if err != nil {
6260
return nil, &commands.CantCreateSketchError{Cause: err}
6361
}
6462

65-
return &rpc.NewSketchResponse{MainFile: sketchFile}, nil
63+
return &rpc.NewSketchResponse{MainFile: sketchFilePath.String()}, nil
6664
}

0 commit comments

Comments
 (0)