Skip to content

Fix panic when board attach args are incomplete #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import (

func initAttachCommand() *cobra.Command {
attachCommand := &cobra.Command{
Use: "attach <port>|<FQBN> [sketchPath]",
Use: "attach <port>|<FQBN> <sketchPath>",
Short: "Attaches a sketch to a board.",
Long: "Attaches a sketch to a board.",
Example: " " + os.Args[0] + " board attach serial:///dev/tty/ACM0\n" +
Example: " " + os.Args[0] + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach arduino:samd:mkr1000",
Args: cobra.RangeArgs(1, 2),
" " + os.Args[0] + " board attach arduino:samd:mkr1000 HelloWorld",
Args: cobra.ExactArgs(2),
Run: runAttachCommand,
}
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s",
Expand All @@ -52,16 +52,15 @@ var attachFlags struct {

func runAttachCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance()
var path string
if len(args) > 0 {
path = args[1]
}
path := args[1]

_, err := board.Attach(context.Background(), &rpc.BoardAttachReq{
Instance: instance,
BoardUri: args[0],
SketchPath: path,
SearchTimeout: attachFlags.searchTimeout,
}, output.TaskProgress())

if err != nil {
formatter.PrintError(err, "attach board error")
os.Exit(errorcodes.ErrGeneric)
Expand Down
9 changes: 5 additions & 4 deletions commands/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ import (

// Attach FIXMEDOC
func Attach(ctx context.Context, req *rpc.BoardAttachReq, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResp, error) {

pm := commands.GetPackageManager(req.GetInstance().GetId())
if pm == nil {
return nil, errors.New("invalid instance")
}
var sketchPath *paths.Path
if req.GetSketchPath() != "" {
sketchPath = paths.New(req.GetSketchPath())

if req.GetSketchPath() == "" {
return nil, errors.New("sketch path can't be empty")
}

sketchPath := paths.New(req.GetSketchPath())
sketch, err := sketches.NewSketchFromPath(sketchPath)
if err != nil {
return nil, fmt.Errorf("opening sketch: %s", err)
Expand Down