Skip to content

Commit 554160c

Browse files
committed
Added output to new 'core list' command
1 parent 890546e commit 554160c

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

cli/core/list.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ package core
1919

2020
import (
2121
"context"
22+
"fmt"
23+
"os"
24+
"sort"
2225

2326
"github.com/arduino/arduino-cli/cli"
2427
"github.com/arduino/arduino-cli/commands/core"
28+
"github.com/arduino/arduino-cli/common/formatter"
2529
"github.com/arduino/arduino-cli/output"
2630
"github.com/arduino/arduino-cli/rpc"
2731
"github.com/spf13/cobra"
@@ -46,8 +50,30 @@ var listFlags struct {
4650

4751
func runListCommand(cmd *cobra.Command, args []string) {
4852
instance := cli.CreateInstance()
49-
core.PlatformList(context.Background(), &rpc.PlatformListReq{
53+
resp, err := core.PlatformList(context.Background(), &rpc.PlatformListReq{
5054
Instance: instance,
5155
UpdatableOnly: listFlags.updatableOnly,
5256
}, output.NewTaskProgressCB())
57+
if err != nil {
58+
formatter.PrintError(err, "Error saerching for platforms")
59+
os.Exit(cli.ErrGeneric)
60+
}
61+
installed := resp.GetInstalledPlatform()
62+
if installed != nil && len(installed) > 0 {
63+
if cli.OutputJSONOrElse(installed) {
64+
outputInstalledCores(installed)
65+
}
66+
}
67+
}
68+
69+
func outputInstalledCores(cores []*rpc.InstalledPlatform) {
70+
table := output.NewTable()
71+
table.AddRow("ID", "Installed", "Latest", "Name")
72+
sort.Slice(cores, func(i, j int) bool {
73+
return cores[i].ID < cores[j].ID
74+
})
75+
for _, item := range cores {
76+
table.AddRow(item.GetID(), item.GetInstalled(), item.GetLatest(), item.GetName())
77+
}
78+
fmt.Print(table.Render())
5379
}

commands/core/list.go

-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,5 @@ func PlatformList(ctx context.Context, req *rpc.PlatformListReq, taskCB commands
5353
}
5454
}
5555

56-
// if len(installed) > 0 {
57-
// //formatter.Print(output.InstalledPlatforms{Platforms: installed})
58-
// }
5956
return &rpc.PlatformListResp{InstalledPlatform: installed}, nil
6057
}

common/formatter/output/core_structs.go

-30
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,6 @@ import (
2525
semver "go.bug.st/relaxed-semver"
2626
)
2727

28-
// InstalledPlatforms represents an output of a set of installed platforms.
29-
type InstalledPlatforms struct {
30-
Platforms []*InstalledPlatform
31-
}
32-
33-
// InstalledPlatform represents an output of an installed plaform.
34-
type InstalledPlatform struct {
35-
ID string
36-
Installed *semver.Version
37-
Latest *semver.Version
38-
Name string
39-
}
40-
41-
func (is InstalledPlatforms) less(i, j int) bool {
42-
return is.Platforms[i].ID < is.Platforms[j].ID
43-
}
44-
45-
func (is InstalledPlatforms) String() string {
46-
table := uitable.New()
47-
table.MaxColWidth = 100
48-
table.Wrap = true
49-
50-
table.AddRow("ID", "Installed", "Latest", "Name")
51-
sort.Slice(is.Platforms, is.less)
52-
for _, item := range is.Platforms {
53-
table.AddRow(item.ID, item.Installed, item.Latest, item.Name)
54-
}
55-
return fmt.Sprintln(table)
56-
}
57-
5828
// SearchedPlatforms represents an output of a set of searched platforms
5929
type SearchedPlatforms struct {
6030
Platforms []*SearchedPlatform

0 commit comments

Comments
 (0)