Commit f8e52e2e authored by Cristian Maglie's avatar Cristian Maglie

Re-enabled json output for 'core list' command

Fix #39
parent 5f2d04f6
......@@ -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})
}
}
......@@ -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})
}
}
......@@ -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)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment