Skip to content

Commit 4ad18ff

Browse files
Use feedback/result structs in cli commands instead of rpc ones (#2389)
1 parent aeac305 commit 4ad18ff

File tree

17 files changed

+1322
-166
lines changed

17 files changed

+1322
-166
lines changed

internal/cli/board/details.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/commands/board"
2424
"github.com/arduino/arduino-cli/internal/cli/arguments"
2525
"github.com/arduino/arduino-cli/internal/cli/feedback"
26+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
2627
"github.com/arduino/arduino-cli/internal/cli/instance"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2829
"github.com/arduino/arduino-cli/table"
@@ -74,7 +75,7 @@ func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool, showP
7475
}
7576

7677
feedback.PrintResult(detailsResult{
77-
details: res,
78+
details: result.NewBoardDetailsResponse(res),
7879
listProgrammers: listProgrammers,
7980
showFullDetails: showFullDetails,
8081
showProperties: showPropertiesMode != arguments.ShowPropertiesDisabled,
@@ -84,7 +85,7 @@ func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool, showP
8485
// output from this command requires special formatting, let's create a dedicated
8586
// feedback.Result implementation
8687
type detailsResult struct {
87-
details *rpc.BoardDetailsResponse
88+
details *result.BoardDetailsResponse
8889
listProgrammers bool
8990
showFullDetails bool
9091
showProperties bool
@@ -99,7 +100,7 @@ func (dr detailsResult) String() string {
99100

100101
if dr.showProperties {
101102
res := ""
102-
for _, prop := range details.GetBuildProperties() {
103+
for _, prop := range details.BuildProperties {
103104
res += fmt.Sprintln(prop)
104105
}
105106
return res
@@ -109,7 +110,7 @@ func (dr detailsResult) String() string {
109110
t := table.New()
110111
t.AddRow(tr("Id"), tr("Programmer name"))
111112
for _, programmer := range details.Programmers {
112-
t.AddRow(programmer.GetId(), programmer.GetName())
113+
t.AddRow(programmer.Id, programmer.Name)
113114
}
114115
return t.Render()
115116
}
@@ -138,7 +139,7 @@ func (dr detailsResult) String() string {
138139
t.AddRow(tr("Board name:"), details.Name)
139140
t.AddRow(tr("FQBN:"), details.Fqbn)
140141
addIfNotEmpty(tr("Board version:"), details.Version)
141-
if details.GetDebuggingSupported() {
142+
if details.DebuggingSupported {
142143
t.AddRow(tr("Debugging supported:"), table.NewCell("✔", color.New(color.FgGreen)))
143144
}
144145

@@ -148,11 +149,15 @@ func (dr detailsResult) String() string {
148149
table.NewCell("✔", color.New(color.FgGreen)))
149150
}
150151

151-
for _, idp := range details.GetIdentificationProperties() {
152+
for _, idp := range details.IdentificationProperties {
153+
if idp.Properties == nil {
154+
continue
155+
}
152156
t.AddRow() // get some space from above
153157
header := tr("Identification properties:")
154-
for k, v := range idp.GetProperties() {
155-
t.AddRow(header, k+"="+v)
158+
keys := idp.Properties.Keys()
159+
for _, k := range keys {
160+
t.AddRow(header, k+"="+idp.Properties.Get(k))
156161
header = ""
157162
}
158163
}
@@ -213,7 +218,7 @@ func (dr detailsResult) String() string {
213218

214219
tab.AddRow(tr("Programmers:"), tr("ID"), tr("Name"))
215220
for _, programmer := range details.Programmers {
216-
tab.AddRow("", programmer.GetId(), programmer.GetName())
221+
tab.AddRow("", programmer.Id, programmer.Name)
217222
}
218223

219224
return t.Render() + tab.Render()

internal/cli/board/list.go

Lines changed: 36 additions & 32 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,60 @@ func watchList(inst *rpc.Instance) {
98100
}
99101

100102
for event := range eventsChan {
101-
feedback.PrintResult(watchEvent{
102-
Type: event.EventType,
103-
Boards: event.Port.MatchingBoards,
104-
Port: event.Port.Port,
105-
Error: event.Error,
106-
})
103+
if res := result.NewBoardListWatchResponse(event); res != nil {
104+
feedback.PrintResult(watchEventResult{
105+
Type: res.EventType,
106+
Boards: res.Port.MatchingBoards,
107+
Port: res.Port.Port,
108+
Error: res.Error,
109+
})
110+
}
107111
}
108112
}
109113

110114
// output from this command requires special formatting, let's create a dedicated
111115
// feedback.Result implementation
112-
type result struct {
113-
ports []*rpc.DetectedPort
116+
type listResult struct {
117+
ports []*result.DetectedPort
114118
}
115119

116-
func (dr result) Data() interface{} {
120+
func (dr listResult) Data() interface{} {
117121
return dr.ports
118122
}
119123

120-
func (dr result) String() string {
124+
func (dr listResult) String() string {
121125
if len(dr.ports) == 0 {
122126
return tr("No boards found.")
123127
}
124128

125129
sort.Slice(dr.ports, func(i, j int) bool {
126130
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())
131+
return x.Protocol < y.Protocol ||
132+
(x.Protocol == y.Protocol && x.Address < y.Address)
129133
})
130134

131135
t := table.New()
132136
t.SetHeader(tr("Port"), tr("Protocol"), tr("Type"), tr("Board Name"), tr("FQBN"), tr("Core"))
133137
for _, detectedPort := range dr.ports {
134138
port := detectedPort.Port
135-
protocol := port.GetProtocol()
136-
address := port.GetAddress()
137-
if port.GetProtocol() == "serial" {
138-
address = port.GetAddress()
139+
protocol := port.Protocol
140+
address := port.Address
141+
if port.Protocol == "serial" {
142+
address = port.Address
139143
}
140-
protocolLabel := port.GetProtocolLabel()
141-
if boards := detectedPort.GetMatchingBoards(); len(boards) > 0 {
144+
protocolLabel := port.ProtocolLabel
145+
if boards := detectedPort.MatchingBoards; len(boards) > 0 {
142146
sort.Slice(boards, func(i, j int) bool {
143147
x, y := boards[i], boards[j]
144-
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
148+
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
145149
})
146150
for _, b := range boards {
147-
board := b.GetName()
151+
board := b.Name
148152

149153
// to improve the user experience, show on a dedicated column
150154
// the name of the core supporting the board detected
151155
var coreName = ""
152-
fqbn, err := cores.ParseFQBN(b.GetFqbn())
156+
fqbn, err := cores.ParseFQBN(b.Fqbn)
153157
if err == nil {
154158
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
155159
}
@@ -170,18 +174,18 @@ func (dr result) String() string {
170174
return t.Render()
171175
}
172176

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"`
177+
type watchEventResult struct {
178+
Type string `json:"eventType"`
179+
Boards []*result.BoardListItem `json:"matching_boards,omitempty"`
180+
Port *result.Port `json:"port,omitempty"`
181+
Error string `json:"error,omitempty"`
178182
}
179183

180-
func (dr watchEvent) Data() interface{} {
184+
func (dr watchEventResult) Data() interface{} {
181185
return dr
182186
}
183187

184-
func (dr watchEvent) String() string {
188+
func (dr watchEventResult) String() string {
185189
t := table.New()
186190

187191
event := map[string]string{
@@ -197,15 +201,15 @@ func (dr watchEvent) String() string {
197201
if boards := dr.Boards; len(boards) > 0 {
198202
sort.Slice(boards, func(i, j int) bool {
199203
x, y := boards[i], boards[j]
200-
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
204+
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
201205
})
202206
for _, b := range boards {
203-
board := b.GetName()
207+
board := b.Name
204208

205209
// to improve the user experience, show on a dedicated column
206210
// the name of the core supporting the board detected
207211
var coreName = ""
208-
fqbn, err := cores.ParseFQBN(b.GetFqbn())
212+
fqbn, err := cores.ParseFQBN(b.Fqbn)
209213
if err == nil {
210214
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
211215
}

internal/cli/board/listall.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/arduino/arduino-cli/commands/board"
2525
"github.com/arduino/arduino-cli/internal/cli/feedback"
26+
fResult "github.com/arduino/arduino-cli/internal/cli/feedback/result"
2627
"github.com/arduino/arduino-cli/internal/cli/instance"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2829
"github.com/arduino/arduino-cli/table"
@@ -63,32 +64,37 @@ func runListAllCommand(cmd *cobra.Command, args []string) {
6364
feedback.Fatal(tr("Error listing boards: %v", err), feedback.ErrGeneric)
6465
}
6566

66-
feedback.PrintResult(resultAll{list})
67+
feedback.PrintResult(resultAll{fResult.NewBoardListAllResponse(list)})
6768
}
6869

6970
// output from this command requires special formatting, let's create a dedicated
7071
// feedback.Result implementation
7172
type resultAll struct {
72-
list *rpc.BoardListAllResponse
73+
list *fResult.BoardListAllResponse
7374
}
7475

7576
func (dr resultAll) Data() interface{} {
7677
return dr.list
7778
}
7879

7980
func (dr resultAll) String() string {
81+
t := table.New()
82+
t.SetHeader(tr("Board Name"), tr("FQBN"), "")
83+
84+
if dr.list == nil || len(dr.list.Boards) == 0 {
85+
return t.Render()
86+
}
87+
8088
sort.Slice(dr.list.Boards, func(i, j int) bool {
81-
return dr.list.Boards[i].GetName() < dr.list.Boards[j].GetName()
89+
return dr.list.Boards[i].Name < dr.list.Boards[j].Name
8290
})
8391

84-
t := table.New()
85-
t.SetHeader(tr("Board Name"), tr("FQBN"), "")
86-
for _, item := range dr.list.GetBoards() {
92+
for _, item := range dr.list.Boards {
8793
hidden := ""
8894
if item.IsHidden {
8995
hidden = tr("(hidden)")
9096
}
91-
t.AddRow(item.GetName(), item.GetFqbn(), hidden)
97+
t.AddRow(item.Name, item.Fqbn, hidden)
9298
}
9399
return t.Render()
94100
}

internal/cli/board/search.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/arduino/arduino-cli/commands/board"
2626
"github.com/arduino/arduino-cli/internal/cli/feedback"
27+
fResult "github.com/arduino/arduino-cli/internal/cli/feedback/result"
2728
"github.com/arduino/arduino-cli/internal/cli/instance"
2829
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2930
"github.com/arduino/arduino-cli/table"
@@ -60,32 +61,37 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
6061
feedback.Fatal(tr("Error searching boards: %v", err), feedback.ErrGeneric)
6162
}
6263

63-
feedback.PrintResult(searchResults{res.Boards})
64+
feedback.PrintResult(searchResults{fResult.NewBoardListItems(res.Boards)})
6465
}
6566

6667
// output from this command requires special formatting so we create a dedicated
6768
// feedback.Result implementation
6869
type searchResults struct {
69-
boards []*rpc.BoardListItem
70+
boards []*fResult.BoardListItem
7071
}
7172

7273
func (r searchResults) Data() interface{} {
7374
return r.boards
7475
}
7576

7677
func (r searchResults) String() string {
77-
sort.Slice(r.boards, func(i, j int) bool {
78-
return r.boards[i].GetName() < r.boards[j].GetName()
79-
})
78+
if len(r.boards) == 0 {
79+
return ""
80+
}
8081

8182
t := table.New()
8283
t.SetHeader(tr("Board Name"), tr("FQBN"), tr("Platform ID"), "")
84+
85+
sort.Slice(r.boards, func(i, j int) bool {
86+
return r.boards[i].Name < r.boards[j].Name
87+
})
88+
8389
for _, item := range r.boards {
8490
hidden := ""
8591
if item.IsHidden {
8692
hidden = tr("(hidden)")
8793
}
88-
t.AddRow(item.GetName(), item.GetFqbn(), item.Platform.Metadata.Id, hidden)
94+
t.AddRow(item.Name, item.Fqbn, item.Platform.Metadata.Id, hidden)
8995
}
9096
return t.Render()
9197
}

0 commit comments

Comments
 (0)