Skip to content

Commit 60abc6a

Browse files
cli: board list use feedback result structs
1 parent cdc80bf commit 60abc6a

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

internal/cli/board/list.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/commands/board"
2828
"github.com/arduino/arduino-cli/internal/cli/arguments"
2929
"github.com/arduino/arduino-cli/internal/cli/feedback"
30+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
3031
"github.com/arduino/arduino-cli/internal/cli/instance"
3132
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3233
"github.com/arduino/arduino-cli/table"
@@ -81,7 +82,8 @@ func runListCommand(watch bool, timeout int64, fqbn string) {
8182
for _, err := range discoveryErrors {
8283
feedback.Warning(tr("Error starting discovery: %v", err))
8384
}
84-
feedback.PrintResult(result{ports})
85+
86+
feedback.PrintResult(listResult{result.NewDetectedPorts(ports)})
8587
}
8688

8789
func watchList(inst *rpc.Instance) {
@@ -98,58 +100,61 @@ func watchList(inst *rpc.Instance) {
98100
}
99101

100102
for event := range eventsChan {
101-
feedback.PrintResult(watchEvent{
103+
feedback.PrintResult(watchEventResult{
102104
Type: event.EventType,
103-
Boards: event.Port.MatchingBoards,
104-
Port: event.Port.Port,
105+
Boards: result.NewBoardListItems(event.Port.MatchingBoards),
106+
Port: result.NewPort(event.Port.Port),
105107
Error: event.Error,
106108
})
107109
}
108110
}
109111

110112
// output from this command requires special formatting, let's create a dedicated
111113
// feedback.Result implementation
112-
type result struct {
113-
ports []*rpc.DetectedPort
114+
type listResult struct {
115+
ports []*result.DetectedPort
114116
}
115117

116-
func (dr result) Data() interface{} {
118+
func (dr listResult) Data() interface{} {
117119
return dr.ports
118120
}
119121

120-
func (dr result) String() string {
122+
func (dr listResult) String() string {
121123
if len(dr.ports) == 0 {
122124
return tr("No boards found.")
123125
}
124126

125127
sort.Slice(dr.ports, func(i, j int) bool {
126128
x, y := dr.ports[i].Port, dr.ports[j].Port
127-
return x.GetProtocol() < y.GetProtocol() ||
128-
(x.GetProtocol() == y.GetProtocol() && x.GetAddress() < y.GetAddress())
129+
return x.Protocol < y.Protocol ||
130+
(x.Protocol == y.Protocol && x.Address < y.Address)
129131
})
130132

131133
t := table.New()
132134
t.SetHeader(tr("Port"), tr("Protocol"), tr("Type"), tr("Board Name"), tr("FQBN"), tr("Core"))
133135
for _, detectedPort := range dr.ports {
136+
if detectedPort == nil {
137+
continue
138+
}
134139
port := detectedPort.Port
135-
protocol := port.GetProtocol()
136-
address := port.GetAddress()
137-
if port.GetProtocol() == "serial" {
138-
address = port.GetAddress()
140+
protocol := port.Protocol
141+
address := port.Address
142+
if port.Protocol == "serial" {
143+
address = port.Address
139144
}
140-
protocolLabel := port.GetProtocolLabel()
141-
if boards := detectedPort.GetMatchingBoards(); len(boards) > 0 {
145+
protocolLabel := port.ProtocolLabel
146+
if boards := detectedPort.MatchingBoards; len(boards) > 0 {
142147
sort.Slice(boards, func(i, j int) bool {
143148
x, y := boards[i], boards[j]
144-
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
149+
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
145150
})
146151
for _, b := range boards {
147-
board := b.GetName()
152+
board := b.Name
148153

149154
// to improve the user experience, show on a dedicated column
150155
// the name of the core supporting the board detected
151156
var coreName = ""
152-
fqbn, err := cores.ParseFQBN(b.GetFqbn())
157+
fqbn, err := cores.ParseFQBN(b.Fqbn)
153158
if err == nil {
154159
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
155160
}
@@ -170,18 +175,18 @@ func (dr result) String() string {
170175
return t.Render()
171176
}
172177

173-
type watchEvent struct {
174-
Type string `json:"eventType"`
175-
Boards []*rpc.BoardListItem `json:"matching_boards,omitempty"`
176-
Port *rpc.Port `json:"port,omitempty"`
177-
Error string `json:"error,omitempty"`
178+
type watchEventResult struct {
179+
Type string `json:"eventType"`
180+
Boards []*result.BoardListItem `json:"matching_boards,omitempty"`
181+
Port *result.Port `json:"port,omitempty"`
182+
Error string `json:"error,omitempty"`
178183
}
179184

180-
func (dr watchEvent) Data() interface{} {
185+
func (dr watchEventResult) Data() interface{} {
181186
return dr
182187
}
183188

184-
func (dr watchEvent) String() string {
189+
func (dr watchEventResult) String() string {
185190
t := table.New()
186191

187192
event := map[string]string{
@@ -197,15 +202,15 @@ func (dr watchEvent) String() string {
197202
if boards := dr.Boards; len(boards) > 0 {
198203
sort.Slice(boards, func(i, j int) bool {
199204
x, y := boards[i], boards[j]
200-
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
205+
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
201206
})
202207
for _, b := range boards {
203-
board := b.GetName()
208+
board := b.Name
204209

205210
// to improve the user experience, show on a dedicated column
206211
// the name of the core supporting the board detected
207212
var coreName = ""
208-
fqbn, err := cores.ParseFQBN(b.GetFqbn())
213+
fqbn, err := cores.ParseFQBN(b.Fqbn)
209214
if err == nil {
210215
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
211216
}

0 commit comments

Comments
 (0)