@@ -27,43 +27,56 @@ 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/cc/arduino/cli/commands/v1"
30+ "github.com/sirupsen/logrus"
3031 "github.com/spf13/cobra"
3132)
3233
34+ var (
35+ port arguments.Port
36+ )
37+
3338func initAttachCommand () * cobra.Command {
3439 attachCommand := & cobra.Command {
35- Use : fmt .Sprintf ("attach <%s>|<%s> [%s]" , tr ("port" ), tr ("FQBN" ), tr ("sketchPath" )),
40+ Use : fmt .Sprintf ("attach -p <%s>|-b <%s> [%s]" , tr ("port" ), tr ("FQBN" ), tr ("sketchPath" )),
3641 Short : tr ("Attaches a sketch to a board." ),
3742 Long : tr ("Attaches a sketch to a board." ),
38- Example : " " + os .Args [0 ] + " board attach serial:// /dev/ttyACM0\n " +
39- " " + os .Args [0 ] + " board attach serial:// /dev/ttyACM0 HelloWorld\n " +
40- " " + os .Args [0 ] + " board attach arduino:samd:mkr1000" ,
41- Args : cobra .RangeArgs ( 1 , 2 ),
43+ Example : " " + os .Args [0 ] + " board attach -p /dev/ttyACM0\n " +
44+ " " + os .Args [0 ] + " board attach -p /dev/ttyACM0 HelloWorld\n " +
45+ " " + os .Args [0 ] + " board attach -b arduino:samd:mkr1000" ,
46+ Args : cobra .MaximumNArgs ( 1 ),
4247 Run : runAttachCommand ,
4348 }
44- attachCommand .Flags ().StringVar (& attachFlags .searchTimeout , "timeout" , "5s" ,
45- tr ("The connected devices search timeout, raise it if your board doesn't show up (e.g. to %s)." , "10s" ))
46- return attachCommand
47- }
49+ fqbn .AddToCommand (attachCommand )
50+ port .AddToCommand (attachCommand )
4851
49- var attachFlags struct {
50- searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
52+ return attachCommand
5153}
5254
5355func runAttachCommand (cmd * cobra.Command , args []string ) {
5456 instance := instance .CreateAndInit ()
5557
58+ logrus .Info ("Executing `arduino-cli board attach`" )
59+
5660 path := ""
57- if len (args ) > 1 {
58- path = args [1 ]
61+ if len (args ) > 0 {
62+ path = args [0 ]
5963 }
6064 sketchPath := arguments .InitSketchPath (path )
6165
66+ // ugly hack to allow user to specify fqbn and port as flags (consistency)
67+ // a more meaningful fix would be to fix board.Attach
68+ var boardURI string
69+ discoveryPort , _ := port .GetPort (instance , nil )
70+ if fqbn .String () != "" {
71+ boardURI = fqbn .String ()
72+ } else if discoveryPort != nil {
73+ boardURI = discoveryPort .Address
74+ }
6275 if _ , err := board .Attach (context .Background (), & rpc.BoardAttachRequest {
6376 Instance : instance ,
64- BoardUri : args [ 0 ] ,
77+ BoardUri : boardURI ,
6578 SketchPath : sketchPath .String (),
66- SearchTimeout : attachFlags . searchTimeout ,
79+ SearchTimeout : port . GetSearchTimeout (). String () ,
6780 }, output .TaskProgress ()); err != nil {
6881 feedback .Errorf (tr ("Attach board error: %v" ), err )
6982 os .Exit (errorcodes .ErrGeneric )
0 commit comments