@@ -30,7 +30,7 @@ func initAttachCommand() *cobra.Command {
30
30
attachCommand := & cobra.Command {
31
31
Use : fmt .Sprintf ("attach [-p <%s>] [-b <%s>] [%s]" , tr ("port" ), tr ("FQBN" ), tr ("sketchPath" )),
32
32
Short : tr ("Attaches a sketch to a board." ),
33
- Long : tr ("Attaches a sketch to a board ." ),
33
+ Long : tr ("Sets the default values for port and FQBN. If no port or FQBN are specified, the current default port and FQBN are displayed ." ),
34
34
Example : " " + os .Args [0 ] + " board attach -p /dev/ttyACM0\n " +
35
35
" " + os .Args [0 ] + " board attach -p /dev/ttyACM0 HelloWorld\n " +
36
36
" " + os .Args [0 ] + " board attach -b arduino:samd:mkr1000" ,
@@ -53,24 +53,66 @@ func runAttachCommand(path string, port *arguments.Port, fqbn string) {
53
53
sketchPath := arguments .InitSketchPath (path )
54
54
sk := arguments .NewSketch (sketchPath )
55
55
56
+ var currentPort * boardAttachPortResult
57
+ if currentAddress , currentProtocol := sk .GetDefaultPortAddressAndProtocol (); currentAddress != "" {
58
+ currentPort = & boardAttachPortResult {
59
+ Address : currentAddress ,
60
+ Protocol : currentProtocol ,
61
+ }
62
+ }
63
+ current := & boradAttachResult {
64
+ Port : currentPort ,
65
+ Fqbn : sk .GetDefaultFQBN (),
66
+ }
56
67
address , protocol , _ := port .GetPortAddressAndProtocol (nil , sk )
57
68
if address != "" {
58
69
if err := sk .SetDefaultPort (address , protocol ); err != nil {
59
70
feedback .Errorf ("%s: %s" , tr ("Error saving sketch metadata" ), err )
60
71
os .Exit (errorcodes .ErrGeneric )
61
72
}
62
- msg := fmt . Sprintf ( "%s: %s" , tr ( "Default port set to" ), address )
63
- if protocol != "" {
64
- msg += " (" + protocol + ")"
73
+ current . Port = & boardAttachPortResult {
74
+ Address : address ,
75
+ Protocol : protocol ,
65
76
}
66
- feedback .Print (msg )
67
77
}
68
78
if fqbn != "" {
69
79
if err := sk .SetDefaultFQBN (fqbn ); err != nil {
70
80
feedback .Errorf ("%s: %s" , tr ("Error saving sketch metadata" ), err )
71
81
os .Exit (errorcodes .ErrGeneric )
72
82
}
73
- msg := fmt .Sprintf ("%s: %s" , tr ("Default FQBN set to" ), fqbn )
74
- feedback .Print (msg )
83
+ current .Fqbn = fqbn
84
+ }
85
+
86
+ feedback .PrintResult (current )
87
+ }
88
+
89
+ type boardAttachPortResult struct {
90
+ Address string `json:"address,omitempty"`
91
+ Protocol string `json:"protocol,omitempty"`
92
+ }
93
+
94
+ func (b * boardAttachPortResult ) String () string {
95
+ port := b .Address
96
+ if b .Protocol != "" {
97
+ port += " (" + b .Protocol + ")"
98
+ }
99
+ return port
100
+ }
101
+
102
+ type boradAttachResult struct {
103
+ Fqbn string `json:"fqbn,omitempty"`
104
+ Port * boardAttachPortResult `json:"port,omitempty"`
105
+ }
106
+
107
+ func (b * boradAttachResult ) Data () interface {} {
108
+ return b
109
+ }
110
+
111
+ func (b * boradAttachResult ) String () string {
112
+ if b .Port == nil && b .Fqbn == "" {
113
+ return tr ("No default port or FQBN set" )
75
114
}
115
+ res := fmt .Sprintf ("%s: %s\n " , tr ("Default port set to" ), b .Port )
116
+ res += fmt .Sprintf ("%s: %s\n " , tr ("Default FQBN set to" ), b .Fqbn )
117
+ return res
76
118
}
0 commit comments