@@ -27,43 +27,56 @@ import (
27
27
"github.com/arduino/arduino-cli/cli/output"
28
28
"github.com/arduino/arduino-cli/commands/board"
29
29
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
30
+ "github.com/sirupsen/logrus"
30
31
"github.com/spf13/cobra"
31
32
)
32
33
34
+ var (
35
+ port arguments.Port
36
+ )
37
+
33
38
func initAttachCommand () * cobra.Command {
34
39
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" )),
36
41
Short : tr ("Attaches a sketch to a board." ),
37
42
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 ),
42
47
Run : runAttachCommand ,
43
48
}
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 )
48
51
49
- var attachFlags struct {
50
- searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
52
+ return attachCommand
51
53
}
52
54
53
55
func runAttachCommand (cmd * cobra.Command , args []string ) {
54
56
instance := instance .CreateAndInit ()
55
57
58
+ logrus .Info ("Executing `arduino-cli board attach`" )
59
+
56
60
path := ""
57
- if len (args ) > 1 {
58
- path = args [1 ]
61
+ if len (args ) > 0 {
62
+ path = args [0 ]
59
63
}
60
64
sketchPath := arguments .InitSketchPath (path )
61
65
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
+ }
62
75
if _ , err := board .Attach (context .Background (), & rpc.BoardAttachRequest {
63
76
Instance : instance ,
64
- BoardUri : args [ 0 ] ,
77
+ BoardUri : boardURI ,
65
78
SketchPath : sketchPath .String (),
66
- SearchTimeout : attachFlags . searchTimeout ,
79
+ SearchTimeout : port . GetSearchTimeout (). String () ,
67
80
}, output .TaskProgress ()); err != nil {
68
81
feedback .Errorf (tr ("Attach board error: %v" ), err )
69
82
os .Exit (errorcodes .ErrGeneric )
0 commit comments