Skip to content

Commit

Permalink
[breaking] replace board attach <port|fqbn> <sketch-path> with `boa…
Browse files Browse the repository at this point in the history
…rd attach -b <fqbn> | -p <port> <sketh-path>`
  • Loading branch information
umbynos committed Nov 19, 2021
1 parent 633c177 commit f85513c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
}
}

// GetSearchTimeout returns the timeout
func (p *Port) GetSearchTimeout() time.Duration {
return p.timeout
}

// GetDiscoveryPort is a helper function useful to get the port and handle possible errors
func (p *Port) GetDiscoveryPort(instance *rpc.Instance, sk *sketch.Sketch) *discovery.Port {
discoveryPort, err := p.GetPort(instance, sk)
Expand Down
36 changes: 24 additions & 12 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ import (
"github.com/spf13/cobra"
)

var searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
var (
port arguments.Port
)

func initAttachCommand() *cobra.Command {
attachCommand := &cobra.Command{
Use: fmt.Sprintf("attach <%s>|<%s> [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Use: fmt.Sprintf("attach -p <%s>|-b <%s> [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Short: tr("Attaches a sketch to a board."),
Long: tr("Attaches a sketch to a board."),
Example: " " + os.Args[0] + " board attach serial:///dev/ttyACM0\n" +
" " + os.Args[0] + " board attach serial:///dev/ttyACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach arduino:samd:mkr1000",
Args: cobra.RangeArgs(1, 2),
Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" +
" " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000",
Args: cobra.MaximumNArgs(1),
Run: runAttachCommand,
}
attachCommand.Flags().StringVar(&searchTimeout, "timeout", "5s",
tr("The connected devices search timeout, raise it if your board doesn't show up (e.g. to %s).", "10s"))
fqbn.AddToCommand(attachCommand)
port.AddToCommand(attachCommand)

return attachCommand
}

Expand All @@ -55,16 +58,25 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino-cli board attach`")

path := ""
if len(args) > 1 {
path = args[1]
if len(args) > 0 {
path = args[0]
}
sketchPath := arguments.InitSketchPath(path)

// ugly hack to allow user to specify fqbn and port as flags (consistency)
// a more meaningful fix would be to fix board.Attach
var boardURI string
discoveryPort, _ := port.GetPort(instance, nil)
if fqbn.String() != "" {
boardURI = fqbn.String()
} else if discoveryPort != nil {
boardURI = discoveryPort.Address
}
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
Instance: instance,
BoardUri: args[0],
BoardUri: boardURI,
SketchPath: sketchPath.String(),
SearchTimeout: searchTimeout,
SearchTimeout: port.GetSearchTimeout().String(),
}, output.TaskProgress()); err != nil {
feedback.Errorf(tr("Attach board error: %v"), err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
2 changes: 1 addition & 1 deletion test/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ def test_board_attach_without_sketch_json(run_command, data_dir):
# Create a test sketch
assert run_command(["sketch", "new", sketch_path])

assert run_command(["board", "attach", fqbn, sketch_path])
assert run_command(["board", "attach", "-b", fqbn, sketch_path])


def test_board_search_with_outdated_core(run_command):
Expand Down

0 comments on commit f85513c

Please sign in to comment.