diff --git a/commands/commands_test.go b/commands/commands_test.go index 977c7c9801c..003d3d74120 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -422,6 +422,11 @@ func TestCoreCommands(t *testing.T) { require.Contains(t, string(d), "arduino:avr") require.Contains(t, string(d), "1.6.16") + exitCode, d = executeWithArgs(t, "core", "list", "--format", "json") + require.Zero(t, exitCode, "exit code") + require.Contains(t, string(d), "arduino:avr") + require.Contains(t, string(d), "1.6.16") + // Replace avr with 1.6.17 exitCode, d = executeWithArgs(t, "core", "install", "arduino:avr@1.6.17") require.Zero(t, exitCode, "exit code") @@ -438,6 +443,10 @@ func TestCoreCommands(t *testing.T) { require.Zero(t, exitCode, "exit code") require.Contains(t, string(d), "arduino:avr") + exitCode, d = executeWithArgs(t, "core", "list", "--updatable", "--format", "json") + require.Zero(t, exitCode, "exit code") + require.Contains(t, string(d), "arduino:avr") + // Upgrade platform exitCode, d = executeWithArgs(t, "core", "upgrade", "arduino:avr@1.6.18") require.NotZero(t, exitCode, "exit code") diff --git a/commands/core/list.go b/commands/core/list.go index 10a3570deff..64d719bd7ca 100644 --- a/commands/core/list.go +++ b/commands/core/list.go @@ -23,6 +23,7 @@ import ( "github.com/arduino/arduino-cli/common/formatter/output" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + semver "go.bug.st/relaxed-semver" ) func initListCommand() *cobra.Command { @@ -47,7 +48,7 @@ func runListCommand(cmd *cobra.Command, args []string) { pm := commands.InitPackageManager() - res := output.InstalledPlatformReleases{} + installed := []*output.InstalledPlatform{} for _, targetPackage := range pm.GetPackages().Packages { for _, platform := range targetPackage.Platforms { if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil { @@ -56,12 +57,21 @@ func runListCommand(cmd *cobra.Command, args []string) { continue } } - res = append(res, platformRelease) + var latestVersion *semver.Version + if latest := platformRelease.Platform.GetLatestRelease(); latest != nil { + latestVersion = latest.Version + } + installed = append(installed, &output.InstalledPlatform{ + ID: platformRelease.String(), + Installed: platformRelease.Version, + Latest: latestVersion, + Name: platformRelease.Platform.Name, + }) } } } - if len(res) > 0 { - formatter.Print(res) + if len(installed) > 0 { + formatter.Print(output.InstalledPlatforms{Platforms: installed}) } } diff --git a/commands/core/search.go b/commands/core/search.go index a9c74124738..5acac8c8c3f 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -21,9 +21,11 @@ import ( "regexp" "strings" + "github.com/arduino/arduino-cli/common/formatter/output" + + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/common/formatter" - "github.com/arduino/arduino-cli/common/formatter/output" "github.com/spf13/cobra" ) @@ -40,13 +42,13 @@ func initSearchCommand() *cobra.Command { } func runSearchCommand(cmd *cobra.Command, args []string) { - pm := commands.InitPackageManager() + pm := commands.InitPackageManagerWithoutBundles() search := strings.ToLower(strings.Join(args, " ")) formatter.Print("Searching for platforms matching '" + search + "'") formatter.Print("") - res := output.PlatformReleases{} + res := []*cores.PlatformRelease{} if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", search); isUsb { vid, pid := search[:4], search[5:] res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid) @@ -77,6 +79,14 @@ func runSearchCommand(cmd *cobra.Command, args []string) { if len(res) == 0 { formatter.Print("No platforms matching your search") } else { - formatter.Print(res) + out := []*output.SearchedPlatform{} + for _, platformRelease := range res { + out = append(out, &output.SearchedPlatform{ + ID: platformRelease.Platform.String(), + Name: platformRelease.Platform.Name, + Version: platformRelease.Version, + }) + } + formatter.Print(output.SearchedPlatforms{Platforms: out}) } } diff --git a/common/formatter/output/core_structs.go b/common/formatter/output/core_structs.go index 2af8286a940..854efd19370 100644 --- a/common/formatter/output/core_structs.go +++ b/common/formatter/output/core_structs.go @@ -21,59 +21,65 @@ import ( "fmt" "sort" - "github.com/arduino/arduino-cli/arduino/cores" "github.com/gosuri/uitable" semver "go.bug.st/relaxed-semver" ) -// InstalledPlatformReleases represents an output set of installed platforms. -type InstalledPlatformReleases []*cores.PlatformRelease - -func (is InstalledPlatformReleases) Len() int { return len(is) } -func (is InstalledPlatformReleases) Swap(i, j int) { is[i], is[j] = is[j], is[i] } -func (is InstalledPlatformReleases) Less(i, j int) bool { - return is[i].Platform.String() < is[j].Platform.String() +// InstalledPlatforms represents an output of a set of installed platforms. +type InstalledPlatforms struct { + Platforms []*InstalledPlatform } -// PlatformReleases represents an output set of tools of platforms. -type PlatformReleases []*cores.PlatformRelease +// InstalledPlatform represents an output of an installed plaform. +type InstalledPlatform struct { + ID string + Installed *semver.Version + Latest *semver.Version + Name string +} -func (is PlatformReleases) Len() int { return len(is) } -func (is PlatformReleases) Swap(i, j int) { is[i], is[j] = is[j], is[i] } -func (is PlatformReleases) Less(i, j int) bool { - return is[i].Platform.String() < is[j].Platform.String() +func (is InstalledPlatforms) less(i, j int) bool { + return is.Platforms[i].ID < is.Platforms[j].ID } -func (is InstalledPlatformReleases) String() string { +func (is InstalledPlatforms) String() string { table := uitable.New() table.MaxColWidth = 100 table.Wrap = true table.AddRow("ID", "Installed", "Latest", "Name") - sort.Sort(is) - for _, item := range is { - var latestVersion *semver.Version - if latest := item.Platform.GetLatestRelease(); latest != nil { - latestVersion = latest.Version - } - table.AddRow(item.Platform, item.Version, latestVersion, item.Platform.Name) + sort.Slice(is.Platforms, is.less) + for _, item := range is.Platforms { + table.AddRow(item.ID, item.Installed, item.Latest, item.Name) } return fmt.Sprintln(table) } -func (is PlatformReleases) String() string { +// SearchedPlatforms represents an output of a set of searched platforms +type SearchedPlatforms struct { + Platforms []*SearchedPlatform +} + +func (is SearchedPlatforms) less(i, j int) bool { + return is.Platforms[i].ID < is.Platforms[j].ID +} + +// SearchedPlatform represents an output of a searched platform +type SearchedPlatform struct { + ID string + Version *semver.Version + Name string +} + +func (is SearchedPlatforms) String() string { table := uitable.New() table.MaxColWidth = 100 table.Wrap = true - table.AddRow("ID", "Version", "Installed", "Name") - sort.Sort(is) - for _, item := range is { - installed := "No" - if item.InstallDir != nil { - installed = "Yes" - } - table.AddRow(item.Platform.String(), item.Version, installed, item.Platform.Name) + sort.Slice(is.Platforms, is.less) + table.AddRow("ID", "Version", "Name") + for _, item := range is.Platforms { + table.AddRow(item.ID, item.Version, item.Name) } return fmt.Sprintln(table) }