Skip to content

Commit 3dcbb9b

Browse files
author
Massimiliano Pippi
authored
fix panic on missing sketch path (#432)
1 parent 18f9272 commit 3dcbb9b

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Diff for: cli/board/attach.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/arduino/arduino-cli/cli/output"
2828
"github.com/arduino/arduino-cli/commands/board"
2929
rpc "github.com/arduino/arduino-cli/rpc/commands"
30+
"github.com/arduino/go-paths-helper"
31+
"github.com/sirupsen/logrus"
3032
"github.com/spf13/cobra"
3133
)
3234

@@ -52,18 +54,37 @@ var attachFlags struct {
5254

5355
func runAttachCommand(cmd *cobra.Command, args []string) {
5456
instance := instance.CreateInstance()
55-
var path string
56-
if len(args) > 0 {
57-
path = args[1]
57+
58+
var path *paths.Path
59+
if len(args) > 1 {
60+
path = paths.New(args[1])
61+
} else {
62+
path = initSketchPath(path)
5863
}
64+
5965
_, err := board.Attach(context.Background(), &rpc.BoardAttachReq{
6066
Instance: instance,
6167
BoardUri: args[0],
62-
SketchPath: path,
68+
SketchPath: path.String(),
6369
SearchTimeout: attachFlags.searchTimeout,
6470
}, output.TaskProgress())
6571
if err != nil {
6672
feedback.Errorf("Attach board error: %v", err)
6773
os.Exit(errorcodes.ErrGeneric)
6874
}
6975
}
76+
77+
// initSketchPath returns the current working directory
78+
func initSketchPath(sketchPath *paths.Path) *paths.Path {
79+
if sketchPath != nil {
80+
return sketchPath
81+
}
82+
83+
wd, err := paths.Getwd()
84+
if err != nil {
85+
feedback.Errorf("Couldn't get current working directory: %v", err)
86+
os.Exit(errorcodes.ErrGeneric)
87+
}
88+
logrus.Infof("Reading sketch from dir: %s", wd)
89+
return wd
90+
}

0 commit comments

Comments
 (0)