Skip to content

Commit 96c16b2

Browse files
committed
Fixed 'board attach' command output
1 parent 2aadae5 commit 96c16b2

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

Diff for: cli/board/attach.go

+49-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func initAttachCommand() *cobra.Command {
3030
attachCommand := &cobra.Command{
3131
Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
3232
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."),
3434
Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" +
3535
" " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" +
3636
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000",
@@ -53,24 +53,66 @@ func runAttachCommand(path string, port *arguments.Port, fqbn string) {
5353
sketchPath := arguments.InitSketchPath(path)
5454
sk := arguments.NewSketch(sketchPath)
5555

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+
}
5667
address, protocol, _ := port.GetPortAddressAndProtocol(nil, sk)
5768
if address != "" {
5869
if err := sk.SetDefaultPort(address, protocol); err != nil {
5970
feedback.Errorf("%s: %s", tr("Error saving sketch metadata"), err)
6071
os.Exit(errorcodes.ErrGeneric)
6172
}
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,
6576
}
66-
feedback.Print(msg)
6777
}
6878
if fqbn != "" {
6979
if err := sk.SetDefaultFQBN(fqbn); err != nil {
7080
feedback.Errorf("%s: %s", tr("Error saving sketch metadata"), err)
7181
os.Exit(errorcodes.ErrGeneric)
7282
}
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")
75114
}
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
76118
}

0 commit comments

Comments
 (0)