Skip to content

Fixed 'core list' json output #48

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

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]")
require.Zero(t, exitCode, "exit code")
Expand All @@ -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:[email protected]")
require.NotZero(t, exitCode, "exit code")
Expand Down
18 changes: 14 additions & 4 deletions commands/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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})
}
}
18 changes: 14 additions & 4 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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})
}
}
68 changes: 37 additions & 31 deletions common/formatter/output/core_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}