From 012515f93c079d49abf9246aab8f6816c5ccbf1b Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Tue, 1 Oct 2019 10:52:23 +0200 Subject: [PATCH] fix panic on missing sketch path --- cli/board/attach.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/cli/board/attach.go b/cli/board/attach.go index 80b1e68bd9f..76b611d1183 100644 --- a/cli/board/attach.go +++ b/cli/board/attach.go @@ -27,6 +27,8 @@ import ( "github.com/arduino/arduino-cli/cli/output" "github.com/arduino/arduino-cli/commands/board" rpc "github.com/arduino/arduino-cli/rpc/commands" + "github.com/arduino/go-paths-helper" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -52,14 +54,18 @@ var attachFlags struct { func runAttachCommand(cmd *cobra.Command, args []string) { instance := instance.CreateInstance() - var path string - if len(args) > 0 { - path = args[1] + + var path *paths.Path + if len(args) > 1 { + path = paths.New(args[1]) + } else { + path = initSketchPath(path) } + _, err := board.Attach(context.Background(), &rpc.BoardAttachReq{ Instance: instance, BoardUri: args[0], - SketchPath: path, + SketchPath: path.String(), SearchTimeout: attachFlags.searchTimeout, }, output.TaskProgress()) if err != nil { @@ -67,3 +73,18 @@ func runAttachCommand(cmd *cobra.Command, args []string) { os.Exit(errorcodes.ErrGeneric) } } + +// initSketchPath returns the current working directory +func initSketchPath(sketchPath *paths.Path) *paths.Path { + if sketchPath != nil { + return sketchPath + } + + wd, err := paths.Getwd() + if err != nil { + feedback.Errorf("Couldn't get current working directory: %v", err) + os.Exit(errorcodes.ErrGeneric) + } + logrus.Infof("Reading sketch from dir: %s", wd) + return wd +}