Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use feedback/result structs in cli commands instead of rpc ones #2389

Merged
merged 24 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a33072e
added more mapper in feedback result pkg
alessio-perugini Oct 25, 2023
e639e5b
cli: outdated command use feedback result structs
alessio-perugini Oct 25, 2023
cbe3b0d
cli: upload use feedback result structs
alessio-perugini Oct 25, 2023
3ae9dee
cli: board details use feedback result structs
alessio-perugini Oct 25, 2023
da7541a
rename result Platform in PlatformSummary
alessio-perugini Oct 25, 2023
3aee471
fixup mapper BoardListAllRespnse
alessio-perugini Oct 25, 2023
dcf7f86
cli: board listall use feedback result structs
alessio-perugini Oct 25, 2023
cdc80bf
cli: board search use feedback result structs
alessio-perugini Oct 25, 2023
60abc6a
cli: board list use feedback result structs
alessio-perugini Oct 25, 2023
1534ab9
cli: lib deps use feedback result structs
alessio-perugini Oct 25, 2023
5fd4539
cli: lib search use feedback result structs
alessio-perugini Oct 25, 2023
c93f4bc
cli: lib examples use feedback result structs
alessio-perugini Oct 25, 2023
e18ec5f
cli: lib list use feedback result structs
alessio-perugini Oct 25, 2023
ce31a9b
cli: monitor use feedback result structs
alessio-perugini Oct 25, 2023
429f525
cli: compile use feedback result structs
alessio-perugini Oct 25, 2023
a16ba4d
add tests
alessio-perugini Oct 26, 2023
e619093
fix typo and import name
alessio-perugini Nov 6, 2023
2e16184
remove redundant nil checks
alessio-perugini Nov 6, 2023
0702b77
fix printing empty string instead of empty table
alessio-perugini Nov 6, 2023
90cb43a
fix: make anonymous struct explicit
alessio-perugini Nov 6, 2023
2d77b39
fix: use rpc enum String method
alessio-perugini Nov 6, 2023
a126e93
add BoardListWatchResponse mapper
alessio-perugini Nov 6, 2023
0aabc1b
fix: remap rpc enums
alessio-perugini Nov 6, 2023
1d855a0
fix: cli sketch new - not showing any json output
alessio-perugini Nov 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions internal/cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/table"
Expand Down Expand Up @@ -74,7 +75,7 @@ func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool, showP
}

feedback.PrintResult(detailsResult{
details: res,
details: result.NewBoardDetailsResponse(res),
listProgrammers: listProgrammers,
showFullDetails: showFullDetails,
showProperties: showPropertiesMode != arguments.ShowPropertiesDisabled,
Expand All @@ -84,7 +85,7 @@ func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool, showP
// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type detailsResult struct {
details *rpc.BoardDetailsResponse
details *result.BoardDetailsResponse
listProgrammers bool
showFullDetails bool
showProperties bool
Expand All @@ -99,7 +100,7 @@ func (dr detailsResult) String() string {

if dr.showProperties {
res := ""
for _, prop := range details.GetBuildProperties() {
for _, prop := range details.BuildProperties {
res += fmt.Sprintln(prop)
}
return res
Expand All @@ -109,7 +110,7 @@ func (dr detailsResult) String() string {
t := table.New()
t.AddRow(tr("Id"), tr("Programmer name"))
for _, programmer := range details.Programmers {
t.AddRow(programmer.GetId(), programmer.GetName())
t.AddRow(programmer.Id, programmer.Name)
}
return t.Render()
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func (dr detailsResult) String() string {
t.AddRow(tr("Board name:"), details.Name)
t.AddRow(tr("FQBN:"), details.Fqbn)
addIfNotEmpty(tr("Board version:"), details.Version)
if details.GetDebuggingSupported() {
if details.DebuggingSupported {
t.AddRow(tr("Debugging supported:"), table.NewCell("✔", color.New(color.FgGreen)))
}

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

for _, idp := range details.GetIdentificationProperties() {
for _, idp := range details.IdentificationProperties {
if idp.Properties == nil {
continue
}
t.AddRow() // get some space from above
header := tr("Identification properties:")
for k, v := range idp.GetProperties() {
t.AddRow(header, k+"="+v)
keys := idp.Properties.Keys()
for _, k := range keys {
t.AddRow(header, k+"="+idp.Properties.Get(k))
header = ""
}
}
Expand Down Expand Up @@ -213,7 +218,7 @@ func (dr detailsResult) String() string {

tab.AddRow(tr("Programmers:"), tr("ID"), tr("Name"))
for _, programmer := range details.Programmers {
tab.AddRow("", programmer.GetId(), programmer.GetName())
tab.AddRow("", programmer.Id, programmer.Name)
}

return t.Render() + tab.Render()
Expand Down
68 changes: 36 additions & 32 deletions internal/cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/table"
Expand Down Expand Up @@ -81,7 +82,8 @@ func runListCommand(watch bool, timeout int64, fqbn string) {
for _, err := range discoveryErrors {
feedback.Warning(tr("Error starting discovery: %v", err))
}
feedback.PrintResult(result{ports})

feedback.PrintResult(listResult{result.NewDetectedPorts(ports)})
}

func watchList(inst *rpc.Instance) {
Expand All @@ -98,58 +100,60 @@ func watchList(inst *rpc.Instance) {
}

for event := range eventsChan {
feedback.PrintResult(watchEvent{
Type: event.EventType,
Boards: event.Port.MatchingBoards,
Port: event.Port.Port,
Error: event.Error,
})
if res := result.NewBoardListWatchResponse(event); res != nil {
feedback.PrintResult(watchEventResult{
Type: res.EventType,
Boards: res.Port.MatchingBoards,
Port: res.Port.Port,
Error: res.Error,
})
}
}
}

// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type result struct {
ports []*rpc.DetectedPort
type listResult struct {
ports []*result.DetectedPort
}

func (dr result) Data() interface{} {
func (dr listResult) Data() interface{} {
return dr.ports
}

func (dr result) String() string {
func (dr listResult) String() string {
if len(dr.ports) == 0 {
return tr("No boards found.")
}

sort.Slice(dr.ports, func(i, j int) bool {
x, y := dr.ports[i].Port, dr.ports[j].Port
return x.GetProtocol() < y.GetProtocol() ||
(x.GetProtocol() == y.GetProtocol() && x.GetAddress() < y.GetAddress())
return x.Protocol < y.Protocol ||
(x.Protocol == y.Protocol && x.Address < y.Address)
})

t := table.New()
t.SetHeader(tr("Port"), tr("Protocol"), tr("Type"), tr("Board Name"), tr("FQBN"), tr("Core"))
for _, detectedPort := range dr.ports {
port := detectedPort.Port
protocol := port.GetProtocol()
address := port.GetAddress()
if port.GetProtocol() == "serial" {
address = port.GetAddress()
protocol := port.Protocol
address := port.Address
if port.Protocol == "serial" {
address = port.Address
}
protocolLabel := port.GetProtocolLabel()
if boards := detectedPort.GetMatchingBoards(); len(boards) > 0 {
protocolLabel := port.ProtocolLabel
if boards := detectedPort.MatchingBoards; len(boards) > 0 {
sort.Slice(boards, func(i, j int) bool {
x, y := boards[i], boards[j]
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
})
for _, b := range boards {
board := b.GetName()
board := b.Name

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

type watchEvent struct {
Type string `json:"eventType"`
Boards []*rpc.BoardListItem `json:"matching_boards,omitempty"`
Port *rpc.Port `json:"port,omitempty"`
Error string `json:"error,omitempty"`
type watchEventResult struct {
Type string `json:"eventType"`
Boards []*result.BoardListItem `json:"matching_boards,omitempty"`
Port *result.Port `json:"port,omitempty"`
Error string `json:"error,omitempty"`
}

func (dr watchEvent) Data() interface{} {
func (dr watchEventResult) Data() interface{} {
return dr
}

func (dr watchEvent) String() string {
func (dr watchEventResult) String() string {
t := table.New()

event := map[string]string{
Expand All @@ -197,15 +201,15 @@ func (dr watchEvent) String() string {
if boards := dr.Boards; len(boards) > 0 {
sort.Slice(boards, func(i, j int) bool {
x, y := boards[i], boards[j]
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFqbn() < y.GetFqbn())
return x.Name < y.Name || (x.Name == y.Name && x.Fqbn < y.Fqbn)
})
for _, b := range boards {
board := b.GetName()
board := b.Name

// to improve the user experience, show on a dedicated column
// the name of the core supporting the board detected
var coreName = ""
fqbn, err := cores.ParseFQBN(b.GetFqbn())
fqbn, err := cores.ParseFQBN(b.Fqbn)
if err == nil {
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
}
Expand Down
20 changes: 13 additions & 7 deletions internal/cli/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

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

feedback.PrintResult(resultAll{list})
feedback.PrintResult(resultAll{fResult.NewBoardListAllResponse(list)})
}

// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type resultAll struct {
list *rpc.BoardListAllResponse
list *fResult.BoardListAllResponse
}

func (dr resultAll) Data() interface{} {
return dr.list
}

func (dr resultAll) String() string {
t := table.New()
t.SetHeader(tr("Board Name"), tr("FQBN"), "")

if dr.list == nil || len(dr.list.Boards) == 0 {
return t.Render()
}

sort.Slice(dr.list.Boards, func(i, j int) bool {
return dr.list.Boards[i].GetName() < dr.list.Boards[j].GetName()
return dr.list.Boards[i].Name < dr.list.Boards[j].Name
})

t := table.New()
t.SetHeader(tr("Board Name"), tr("FQBN"), "")
for _, item := range dr.list.GetBoards() {
for _, item := range dr.list.Boards {
hidden := ""
if item.IsHidden {
hidden = tr("(hidden)")
}
t.AddRow(item.GetName(), item.GetFqbn(), hidden)
t.AddRow(item.Name, item.Fqbn, hidden)
}
return t.Render()
}
18 changes: 12 additions & 6 deletions internal/cli/board/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/internal/cli/feedback"
fResult "github.com/arduino/arduino-cli/internal/cli/feedback/result"
alessio-perugini marked this conversation as resolved.
Show resolved Hide resolved
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/table"
Expand Down Expand Up @@ -60,32 +61,37 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
feedback.Fatal(tr("Error searching boards: %v", err), feedback.ErrGeneric)
}

feedback.PrintResult(searchResults{res.Boards})
feedback.PrintResult(searchResults{fResult.NewBoardListItems(res.Boards)})
}

// output from this command requires special formatting so we create a dedicated
// feedback.Result implementation
type searchResults struct {
boards []*rpc.BoardListItem
boards []*fResult.BoardListItem
}

func (r searchResults) Data() interface{} {
return r.boards
}

func (r searchResults) String() string {
sort.Slice(r.boards, func(i, j int) bool {
return r.boards[i].GetName() < r.boards[j].GetName()
})
if len(r.boards) == 0 {
return ""
}

t := table.New()
t.SetHeader(tr("Board Name"), tr("FQBN"), tr("Platform ID"), "")

sort.Slice(r.boards, func(i, j int) bool {
return r.boards[i].Name < r.boards[j].Name
})

for _, item := range r.boards {
hidden := ""
if item.IsHidden {
hidden = tr("(hidden)")
}
t.AddRow(item.GetName(), item.GetFqbn(), item.Platform.Metadata.Id, hidden)
t.AddRow(item.Name, item.Fqbn, item.Platform.Metadata.Id, hidden)
}
return t.Render()
}
Loading
Loading