Skip to content

Commit f8e52e2

Browse files
committed
Re-enabled json output for 'core list' command
Fix #39
1 parent 5f2d04f commit f8e52e2

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

Diff for: commands/core/list.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/common/formatter/output"
2424
"github.com/sirupsen/logrus"
2525
"github.com/spf13/cobra"
26+
semver "go.bug.st/relaxed-semver"
2627
)
2728

2829
func initListCommand() *cobra.Command {
@@ -47,7 +48,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
4748

4849
pm := commands.InitPackageManager()
4950

50-
res := output.InstalledPlatformReleases{}
51+
installed := []*output.InstalledPlatform{}
5152
for _, targetPackage := range pm.GetPackages().Packages {
5253
for _, platform := range targetPackage.Platforms {
5354
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
@@ -56,12 +57,21 @@ func runListCommand(cmd *cobra.Command, args []string) {
5657
continue
5758
}
5859
}
59-
res = append(res, platformRelease)
60+
var latestVersion *semver.Version
61+
if latest := platformRelease.Platform.GetLatestRelease(); latest != nil {
62+
latestVersion = latest.Version
63+
}
64+
installed = append(installed, &output.InstalledPlatform{
65+
ID: platformRelease.String(),
66+
Installed: platformRelease.Version,
67+
Latest: latestVersion,
68+
Name: platformRelease.Platform.Name,
69+
})
6070
}
6171
}
6272
}
6373

64-
if len(res) > 0 {
65-
formatter.Print(res)
74+
if len(installed) > 0 {
75+
formatter.Print(output.InstalledPlatforms{Platforms: installed})
6676
}
6777
}

Diff for: commands/core/search.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"regexp"
2222
"strings"
2323

24+
"github.com/arduino/arduino-cli/common/formatter/output"
25+
26+
"github.com/arduino/arduino-cli/arduino/cores"
2427
"github.com/arduino/arduino-cli/commands"
2528
"github.com/arduino/arduino-cli/common/formatter"
26-
"github.com/arduino/arduino-cli/common/formatter/output"
2729
"github.com/spf13/cobra"
2830
)
2931

@@ -40,13 +42,13 @@ func initSearchCommand() *cobra.Command {
4042
}
4143

4244
func runSearchCommand(cmd *cobra.Command, args []string) {
43-
pm := commands.InitPackageManager()
45+
pm := commands.InitPackageManagerWithoutBundles()
4446

4547
search := strings.ToLower(strings.Join(args, " "))
4648
formatter.Print("Searching for platforms matching '" + search + "'")
4749
formatter.Print("")
4850

49-
res := output.PlatformReleases{}
51+
res := []*cores.PlatformRelease{}
5052
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", search); isUsb {
5153
vid, pid := search[:4], search[5:]
5254
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
@@ -77,6 +79,14 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
7779
if len(res) == 0 {
7880
formatter.Print("No platforms matching your search")
7981
} else {
80-
formatter.Print(res)
82+
out := []*output.SearchedPlatform{}
83+
for _, platformRelease := range res {
84+
out = append(out, &output.SearchedPlatform{
85+
ID: platformRelease.Platform.String(),
86+
Name: platformRelease.Platform.Name,
87+
Version: platformRelease.Version,
88+
})
89+
}
90+
formatter.Print(output.SearchedPlatforms{Platforms: out})
8191
}
8292
}

Diff for: common/formatter/output/core_structs.go

+37-31
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,65 @@ import (
2121
"fmt"
2222
"sort"
2323

24-
"github.com/arduino/arduino-cli/arduino/cores"
2524
"github.com/gosuri/uitable"
2625
semver "go.bug.st/relaxed-semver"
2726
)
2827

29-
// InstalledPlatformReleases represents an output set of installed platforms.
30-
type InstalledPlatformReleases []*cores.PlatformRelease
31-
32-
func (is InstalledPlatformReleases) Len() int { return len(is) }
33-
func (is InstalledPlatformReleases) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
34-
func (is InstalledPlatformReleases) Less(i, j int) bool {
35-
return is[i].Platform.String() < is[j].Platform.String()
28+
// InstalledPlatforms represents an output of a set of installed platforms.
29+
type InstalledPlatforms struct {
30+
Platforms []*InstalledPlatform
3631
}
3732

38-
// PlatformReleases represents an output set of tools of platforms.
39-
type PlatformReleases []*cores.PlatformRelease
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+
}
4040

41-
func (is PlatformReleases) Len() int { return len(is) }
42-
func (is PlatformReleases) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
43-
func (is PlatformReleases) Less(i, j int) bool {
44-
return is[i].Platform.String() < is[j].Platform.String()
41+
func (is InstalledPlatforms) less(i, j int) bool {
42+
return is.Platforms[i].ID < is.Platforms[j].ID
4543
}
4644

47-
func (is InstalledPlatformReleases) String() string {
45+
func (is InstalledPlatforms) String() string {
4846
table := uitable.New()
4947
table.MaxColWidth = 100
5048
table.Wrap = true
5149

5250
table.AddRow("ID", "Installed", "Latest", "Name")
53-
sort.Sort(is)
54-
for _, item := range is {
55-
var latestVersion *semver.Version
56-
if latest := item.Platform.GetLatestRelease(); latest != nil {
57-
latestVersion = latest.Version
58-
}
59-
table.AddRow(item.Platform, item.Version, latestVersion, item.Platform.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)
6054
}
6155
return fmt.Sprintln(table)
6256
}
6357

64-
func (is PlatformReleases) String() string {
58+
// SearchedPlatforms represents an output of a set of searched platforms
59+
type SearchedPlatforms struct {
60+
Platforms []*SearchedPlatform
61+
}
62+
63+
func (is SearchedPlatforms) less(i, j int) bool {
64+
return is.Platforms[i].ID < is.Platforms[j].ID
65+
}
66+
67+
// SearchedPlatform represents an output of a searched platform
68+
type SearchedPlatform struct {
69+
ID string
70+
Version *semver.Version
71+
Name string
72+
}
73+
74+
func (is SearchedPlatforms) String() string {
6575
table := uitable.New()
6676
table.MaxColWidth = 100
6777
table.Wrap = true
6878

69-
table.AddRow("ID", "Version", "Installed", "Name")
70-
sort.Sort(is)
71-
for _, item := range is {
72-
installed := "No"
73-
if item.InstallDir != nil {
74-
installed = "Yes"
75-
}
76-
table.AddRow(item.Platform.String(), item.Version, installed, item.Platform.Name)
79+
sort.Slice(is.Platforms, is.less)
80+
table.AddRow("ID", "Version", "Name")
81+
for _, item := range is.Platforms {
82+
table.AddRow(item.ID, item.Version, item.Name)
7783
}
7884
return fmt.Sprintln(table)
7985
}

0 commit comments

Comments
 (0)