Skip to content

Commit e6290c5

Browse files
core search handles compatible version
1 parent b4a06dd commit e6290c5

File tree

4 files changed

+131
-106
lines changed

4 files changed

+131
-106
lines changed

commands/core/search.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,34 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
8585
out := []*rpc.PlatformSummary{}
8686
for _, platform := range res {
8787
rpcPlatformSummary := &rpc.PlatformSummary{
88+
Metadata: commands.PlatformToRPCPlatformMetadata(platform),
8889
Releases: map[string]*rpc.PlatformRelease{},
8990
}
90-
91-
rpcPlatformSummary.Metadata = commands.PlatformToRPCPlatformMetadata(platform)
92-
93-
installed := pme.GetInstalledPlatformRelease(platform)
94-
latest := platform.GetLatestRelease()
95-
if installed != nil {
91+
if installed := pme.GetInstalledPlatformRelease(platform); installed != nil {
9692
rpcPlatformSummary.InstalledVersion = installed.Version.String()
93+
rpcPlatformSummary.Releases[installed.Version.String()] = commands.PlatformReleaseToRPC(installed)
9794
}
98-
if latest != nil {
95+
if latest := platform.GetLatestRelease(); latest != nil {
9996
rpcPlatformSummary.LatestVersion = latest.Version.String()
97+
rpcPlatformSummary.Releases[latest.Version.String()] = commands.PlatformReleaseToRPC(latest)
98+
}
99+
if latestCompatible := platform.GetLatestCompatibleRelease(); latestCompatible != nil {
100+
rpcPlatformSummary.LatestCompatibleVersion = latestCompatible.Version.String()
101+
rpcPlatformSummary.Releases[latestCompatible.Version.String()] = commands.PlatformReleaseToRPC(latestCompatible)
100102
}
101103
if req.AllVersions {
102104
for _, platformRelease := range platform.GetAllReleases() {
103105
rpcPlatformRelease := commands.PlatformReleaseToRPC(platformRelease)
104106
rpcPlatformSummary.Releases[rpcPlatformRelease.Version] = rpcPlatformRelease
105107
}
106-
} else {
107-
if installed != nil {
108-
rpcPlatformRelease := commands.PlatformReleaseToRPC(installed)
109-
rpcPlatformSummary.Releases[installed.Version.String()] = rpcPlatformRelease
110-
}
111-
if latest != nil {
112-
rpcPlatformRelease := commands.PlatformReleaseToRPC(latest)
113-
rpcPlatformSummary.Releases[latest.Version.String()] = rpcPlatformRelease
114-
}
115108
}
116109
out = append(out, rpcPlatformSummary)
117110
}
118111

119112
// Sort result alphabetically and put deprecated platforms at the bottom
120113
sort.Slice(out, func(i, j int) bool {
121-
return strings.ToLower(out[i].GetReleases()[out[i].GetLatestVersion()].Name) <
122-
strings.ToLower(out[j].GetReleases()[out[j].GetLatestVersion()].Name)
114+
return strings.ToLower(out[i].GetLatestRelease().Name) <
115+
strings.ToLower(out[j].GetLatestRelease().Name)
123116
})
124117
sort.SliceStable(out, func(i, j int) bool {
125118
return !out[i].GetMetadata().Deprecated && out[j].GetMetadata().Deprecated

commands/core/search_test.go

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,22 @@ func TestPlatformSearch(t *testing.T) {
6161
},
6262
Releases: map[string]*rpc.PlatformRelease{
6363
"1.0.5": {
64-
Name: "RK002",
65-
Type: []string{"Contributed"},
66-
Installed: false,
67-
Version: "1.0.5",
68-
Boards: []*rpc.Board{{Name: "RK002"}},
69-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
64+
Name: "RK002",
65+
Type: []string{"Contributed"},
66+
Installed: false,
67+
Version: "1.0.5",
68+
Boards: []*rpc.Board{{Name: "RK002"}},
69+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
70+
Incompatible: true,
7071
},
7172
"1.0.6": {
72-
Name: "RK002",
73-
Type: []string{"Contributed"},
74-
Installed: false,
75-
Version: "1.0.6",
76-
Boards: []*rpc.Board{{Name: "RK002"}},
77-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
73+
Name: "RK002",
74+
Type: []string{"Contributed"},
75+
Installed: false,
76+
Version: "1.0.6",
77+
Boards: []*rpc.Board{{Name: "RK002"}},
78+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
79+
Incompatible: true,
7880
},
7981
},
8082
InstalledVersion: "",
@@ -101,12 +103,13 @@ func TestPlatformSearch(t *testing.T) {
101103
},
102104
Releases: map[string]*rpc.PlatformRelease{
103105
"1.0.6": {
104-
Name: "RK002",
105-
Type: []string{"Contributed"},
106-
Installed: false,
107-
Version: "1.0.6",
108-
Boards: []*rpc.Board{{Name: "RK002"}},
109-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
106+
Name: "RK002",
107+
Type: []string{"Contributed"},
108+
Installed: false,
109+
Version: "1.0.6",
110+
Boards: []*rpc.Board{{Name: "RK002"}},
111+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
112+
Incompatible: true,
110113
},
111114
},
112115
InstalledVersion: "",
@@ -133,20 +136,22 @@ func TestPlatformSearch(t *testing.T) {
133136
},
134137
Releases: map[string]*rpc.PlatformRelease{
135138
"1.0.5": {
136-
Name: "RK002",
137-
Type: []string{"Contributed"},
138-
Installed: false,
139-
Version: "1.0.5",
140-
Boards: []*rpc.Board{{Name: "RK002"}},
141-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
139+
Name: "RK002",
140+
Type: []string{"Contributed"},
141+
Installed: false,
142+
Version: "1.0.5",
143+
Boards: []*rpc.Board{{Name: "RK002"}},
144+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
145+
Incompatible: true,
142146
},
143147
"1.0.6": {
144-
Name: "RK002",
145-
Type: []string{"Contributed"},
146-
Installed: false,
147-
Version: "1.0.6",
148-
Boards: []*rpc.Board{{Name: "RK002"}},
149-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
148+
Name: "RK002",
149+
Type: []string{"Contributed"},
150+
Installed: false,
151+
Version: "1.0.6",
152+
Boards: []*rpc.Board{{Name: "RK002"}},
153+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
154+
Incompatible: true,
150155
},
151156
},
152157
InstalledVersion: "",
@@ -173,20 +178,22 @@ func TestPlatformSearch(t *testing.T) {
173178
},
174179
Releases: map[string]*rpc.PlatformRelease{
175180
"1.0.5": {
176-
Name: "RK002",
177-
Type: []string{"Contributed"},
178-
Installed: false,
179-
Version: "1.0.5",
180-
Boards: []*rpc.Board{{Name: "RK002"}},
181-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
181+
Name: "RK002",
182+
Type: []string{"Contributed"},
183+
Installed: false,
184+
Version: "1.0.5",
185+
Boards: []*rpc.Board{{Name: "RK002"}},
186+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
187+
Incompatible: true,
182188
},
183189
"1.0.6": {
184-
Name: "RK002",
185-
Type: []string{"Contributed"},
186-
Installed: false,
187-
Version: "1.0.6",
188-
Boards: []*rpc.Board{{Name: "RK002"}},
189-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
190+
Name: "RK002",
191+
Type: []string{"Contributed"},
192+
Installed: false,
193+
Version: "1.0.6",
194+
Boards: []*rpc.Board{{Name: "RK002"}},
195+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
196+
Incompatible: true,
190197
},
191198
},
192199
InstalledVersion: "",
@@ -213,20 +220,22 @@ func TestPlatformSearch(t *testing.T) {
213220
},
214221
Releases: map[string]*rpc.PlatformRelease{
215222
"1.0.5": {
216-
Name: "RK002",
217-
Type: []string{"Contributed"},
218-
Installed: false,
219-
Version: "1.0.5",
220-
Boards: []*rpc.Board{{Name: "RK002"}},
221-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
223+
Name: "RK002",
224+
Type: []string{"Contributed"},
225+
Installed: false,
226+
Version: "1.0.5",
227+
Boards: []*rpc.Board{{Name: "RK002"}},
228+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
229+
Incompatible: true,
222230
},
223231
"1.0.6": {
224-
Name: "RK002",
225-
Type: []string{"Contributed"},
226-
Installed: false,
227-
Version: "1.0.6",
228-
Boards: []*rpc.Board{{Name: "RK002"}},
229-
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
232+
Name: "RK002",
233+
Type: []string{"Contributed"},
234+
Installed: false,
235+
Version: "1.0.6",
236+
Boards: []*rpc.Board{{Name: "RK002"}},
237+
Help: &rpc.HelpResources{Online: "https://www.retrokits.com/rk002/arduino"},
238+
Incompatible: true,
230239
},
231240
},
232241
InstalledVersion: "",
@@ -285,7 +294,8 @@ func TestPlatformSearch(t *testing.T) {
285294
{Name: "Arduino Industrial 101"},
286295
{Name: "Linino One"},
287296
},
288-
Help: &rpc.HelpResources{Online: "http://www.arduino.cc/en/Reference/HomePage"},
297+
Help: &rpc.HelpResources{Online: "http://www.arduino.cc/en/Reference/HomePage"},
298+
Incompatible: true,
289299
},
290300
},
291301
InstalledVersion: "",
@@ -344,7 +354,8 @@ func TestPlatformSearch(t *testing.T) {
344354
{Name: "Arduino Industrial 101"},
345355
{Name: "Linino One"},
346356
},
347-
Help: &rpc.HelpResources{Online: "http://www.arduino.cc/en/Reference/HomePage"},
357+
Help: &rpc.HelpResources{Online: "http://www.arduino.cc/en/Reference/HomePage"},
358+
Incompatible: true,
348359
},
349360
},
350361
InstalledVersion: "",

internal/cli/core/search.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ import (
3737
"github.com/spf13/cobra"
3838
)
3939

40-
var (
41-
allVersions bool
42-
)
43-
4440
func initSearchCommand() *cobra.Command {
41+
var allVersions bool
4542
searchCommand := &cobra.Command{
4643
Use: fmt.Sprintf("search <%s...>", tr("keywords")),
4744
Short: tr("Search for a core in Boards Manager."),
4845
Long: tr("Search for a core in Boards Manager using the specified keywords."),
4946
Example: " " + os.Args[0] + " core search MKRZero -a -v",
5047
Args: cobra.ArbitraryArgs,
51-
Run: runSearchCommand,
48+
Run: func(cmd *cobra.Command, args []string) {
49+
runSearchCommand(cmd, args, allVersions)
50+
},
5251
}
5352
searchCommand.Flags().BoolVarP(&allVersions, "all", "a", false, tr("Show all available core versions."))
5453

@@ -58,7 +57,7 @@ func initSearchCommand() *cobra.Command {
5857
// indexUpdateInterval specifies the time threshold over which indexes are updated
5958
const indexUpdateInterval = "24h"
6059

61-
func runSearchCommand(cmd *cobra.Command, args []string) {
60+
func runSearchCommand(cmd *cobra.Command, args []string, allVersions bool) {
6261
inst := instance.CreateAndInit()
6362

6463
if indexesNeedUpdating(indexUpdateInterval) {
@@ -82,19 +81,23 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
8281
}
8382

8483
coreslist := resp.GetSearchOutput()
85-
feedback.PrintResult(newSearchResult(coreslist))
84+
feedback.PrintResult(newSearchResult(coreslist, allVersions))
8685
}
8786

8887
// output from this command requires special formatting, let's create a dedicated
8988
// feedback.Result implementation
9089
type searchResults struct {
91-
platforms []*result.Platform
90+
platforms []*result.Platform
91+
allVersions bool
9292
}
9393

94-
func newSearchResult(in []*rpc.PlatformSummary) *searchResults {
95-
res := &searchResults{}
96-
for _, platformSummary := range in {
97-
res.platforms = append(res.platforms, result.NewPlatformResult(platformSummary))
94+
func newSearchResult(in []*rpc.PlatformSummary, allVersions bool) *searchResults {
95+
res := &searchResults{
96+
platforms: make([]*result.Platform, len(in)),
97+
allVersions: allVersions,
98+
}
99+
for i, platformSummary := range in {
100+
res.platforms[i] = result.NewPlatformResult(platformSummary)
98101
}
99102
return res
100103
}
@@ -108,15 +111,24 @@ func (sr searchResults) String() string {
108111
t := table.New()
109112
t.SetHeader(tr("ID"), tr("Version"), tr("Name"))
110113
for _, platform := range sr.platforms {
111-
name := ""
112-
if latest := platform.GetLatestRelease(); latest != nil {
113-
name = latest.Name
114-
}
115-
if platform.Deprecated {
116-
name = fmt.Sprintf("[%s] %s", tr("DEPRECATED"), name)
114+
// When allVersions is not requested we only show the latest compatible version
115+
if !sr.allVersions {
116+
if latestCompatible := platform.GetLatestCompatibleRelease(); latestCompatible != nil {
117+
name := latestCompatible.Name
118+
if latestCompatible.Deprecated {
119+
name = fmt.Sprintf("[%s] %s", tr("DEPRECATED"), latestCompatible.Name)
120+
}
121+
t.AddRow(platform.Id, latestCompatible.Version, name)
122+
}
123+
continue
117124
}
118-
for _, version := range platform.Releases.Keys() {
119-
t.AddRow(platform.Id, version, name)
125+
126+
for _, release := range platform.Releases.Values() {
127+
name := release.Name
128+
if platform.Deprecated {
129+
name = fmt.Sprintf("[%s] %s", tr("DEPRECATED"), release.Name)
130+
}
131+
t.AddRow(platform.Id, release.Version, name)
120132
}
121133
}
122134
return t.Render()

internal/cli/feedback/result/rpc.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ func NewPlatformResult(in *rpc.PlatformSummary) *Platform {
3030
releases.SortKeys((*semver.Version).CompareTo)
3131

3232
return &Platform{
33-
Id: in.Metadata.Id,
34-
Maintainer: in.Metadata.Maintainer,
35-
Website: in.Metadata.Website,
36-
Email: in.Metadata.Email,
37-
ManuallyInstalled: in.Metadata.ManuallyInstalled,
38-
Deprecated: in.Metadata.Deprecated,
39-
Indexed: in.Metadata.Indexed,
40-
Releases: releases,
41-
InstalledVersion: semver.MustParse(in.InstalledVersion),
42-
LatestVersion: semver.MustParse(in.LatestVersion),
33+
Id: in.Metadata.Id,
34+
Maintainer: in.Metadata.Maintainer,
35+
Website: in.Metadata.Website,
36+
Email: in.Metadata.Email,
37+
ManuallyInstalled: in.Metadata.ManuallyInstalled,
38+
Deprecated: in.Metadata.Deprecated,
39+
Indexed: in.Metadata.Indexed,
40+
Releases: releases,
41+
InstalledVersion: semver.MustParse(in.InstalledVersion),
42+
LatestVersion: semver.MustParse(in.LatestVersion),
43+
LatestCompatibleVersion: semver.MustParse(in.LatestCompatibleVersion),
4344
}
4445
}
4546

@@ -55,15 +56,21 @@ type Platform struct {
5556

5657
Releases orderedmap.Map[*semver.Version, *PlatformRelease] `json:"releases,omitempty"`
5758

58-
InstalledVersion *semver.Version `json:"installed_version,omitempty"`
59-
LatestVersion *semver.Version `json:"latest_version,omitempty"`
59+
InstalledVersion *semver.Version `json:"installed_version,omitempty"`
60+
LatestVersion *semver.Version `json:"latest_version,omitempty"`
61+
LatestCompatibleVersion *semver.Version `json:"latest_compatible_version,omitempty"`
6062
}
6163

6264
// GetLatestRelease returns the latest relase of this platform or nil if none available.
6365
func (p *Platform) GetLatestRelease() *PlatformRelease {
6466
return p.Releases.Get(p.LatestVersion)
6567
}
6668

69+
// GetLatestCompatibleRelease returns the latest relase of this platform or nil if none available.
70+
func (p *Platform) GetLatestCompatibleRelease() *PlatformRelease {
71+
return p.Releases.Get(p.LatestCompatibleVersion)
72+
}
73+
6774
// GetInstalledRelease returns the installed relase of this platform or nil if none available.
6875
func (p *Platform) GetInstalledRelease() *PlatformRelease {
6976
return p.Releases.Get(p.InstalledVersion)
@@ -93,6 +100,7 @@ func NewPlatformReleaseResult(in *rpc.PlatformRelease) *PlatformRelease {
93100
Help: help,
94101
MissingMetadata: in.MissingMetadata,
95102
Deprecated: in.Deprecated,
103+
Incompatible: in.Incompatible,
96104
}
97105
return res
98106
}
@@ -107,6 +115,7 @@ type PlatformRelease struct {
107115
Help *HelpResource `json:"help,omitempty"`
108116
MissingMetadata bool `json:"missing_metadata,omitempty"`
109117
Deprecated bool `json:"deprecated,omitempty"`
118+
Incompatible bool `json:"incompatible,omitempty"`
110119
}
111120

112121
// Board maps a rpc.Board

0 commit comments

Comments
 (0)