@@ -17,13 +17,11 @@ package sketch
1717
1818import (
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
5250func 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