Skip to content

Commit 5fd4539

Browse files
cli: lib search use feedback result structs
1 parent 1534ab9 commit 5fd4539

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

internal/cli/feedback/result/rpc.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -766,23 +766,21 @@ func NewLibrarySearchResponse(l *rpc.LibrarySearchResponse) *LibrarySearchRespon
766766
}
767767

768768
type SearchedLibrary struct {
769-
Name string `json:"name,omitempty"`
770-
Releases orderedmap.Map[string, *LibraryRelease] `json:"releases,omitempty"`
771-
Latest *LibraryRelease `json:"latest,omitempty"`
772-
AvailableVersions []string `json:"available_versions,omitempty"`
769+
Name string `json:"name,omitempty"`
770+
Releases orderedmap.Map[*semver.Version, *LibraryRelease] `json:"releases,omitempty"`
771+
Latest *LibraryRelease `json:"latest,omitempty"`
772+
AvailableVersions []string `json:"available_versions,omitempty"`
773773
}
774774

775775
func NewSearchedLibrary(l *rpc.SearchedLibrary) *SearchedLibrary {
776776
if l == nil {
777777
return nil
778778
}
779-
// TODO understand if we want semver keys
780-
releasesMap := orderedmap.New[string, *LibraryRelease]()
779+
releasesMap := orderedmap.NewWithConversionFunc[*semver.Version, *LibraryRelease, string]((*semver.Version).String)
781780
for k, v := range l.GetReleases() {
782-
releasesMap.Set(k, NewLibraryRelease(v))
781+
releasesMap.Set(semver.MustParse(k), NewLibraryRelease(v))
783782
}
784-
releasesMap.SortStableKeys(cmp.Compare)
785-
783+
releasesMap.SortKeys((*semver.Version).CompareTo)
786784
return &SearchedLibrary{
787785
Name: l.GetName(),
788786
Releases: releasesMap,

internal/cli/lib/search.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-cli/commands/lib"
2727
"github.com/arduino/arduino-cli/configuration"
2828
"github.com/arduino/arduino-cli/internal/cli/feedback"
29+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
2930
"github.com/arduino/arduino-cli/internal/cli/instance"
3031
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3132
"github.com/arduino/go-paths-helper"
@@ -128,8 +129,8 @@ func runSearchCommand(args []string, namesOnly bool, omitReleasesDetails bool) {
128129
feedback.Fatal(tr("Error searching for Libraries: %v", err), feedback.ErrGeneric)
129130
}
130131

131-
feedback.PrintResult(result{
132-
results: searchResp,
132+
feedback.PrintResult(librarySearchResult{
133+
results: result.NewLibrarySearchResponse(searchResp),
133134
namesOnly: namesOnly,
134135
})
135136

@@ -138,12 +139,12 @@ func runSearchCommand(args []string, namesOnly bool, omitReleasesDetails bool) {
138139

139140
// output from this command requires special formatting, let's create a dedicated
140141
// feedback.Result implementation
141-
type result struct {
142-
results *rpc.LibrarySearchResponse
142+
type librarySearchResult struct {
143+
results *result.LibrarySearchResponse
143144
namesOnly bool
144145
}
145146

146-
func (res result) Data() interface{} {
147+
func (res librarySearchResult) Data() interface{} {
147148
if res.namesOnly {
148149
type LibName struct {
149150
Name string `json:"name"`
@@ -154,33 +155,36 @@ func (res result) Data() interface{} {
154155
}
155156

156157
names := []LibName{}
157-
results := res.results.GetLibraries()
158-
for _, lib := range results {
158+
for _, lib := range res.results.Libraries {
159+
if lib == nil {
160+
continue
161+
}
159162
names = append(names, LibName{lib.Name})
160163
}
161164

162-
return NamesOnly{
163-
names,
164-
}
165+
return NamesOnly{names}
165166
}
166167

167168
return res.results
168169
}
169170

170-
func (res result) String() string {
171-
results := res.results.GetLibraries()
171+
func (res librarySearchResult) String() string {
172+
results := res.results.Libraries
172173
if len(results) == 0 {
173174
return tr("No libraries matching your search.")
174175
}
175176

176177
var out strings.Builder
177178

178-
if res.results.GetStatus() == rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_FAILED {
179+
if string(res.results.Status) == rpc.LibrarySearchStatus_name[int32(rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_FAILED)] {
179180
out.WriteString(tr("No libraries matching your search.\nDid you mean...\n"))
180181
}
181182

182183
for _, lib := range results {
183-
if res.results.GetStatus() == rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS {
184+
if lib == nil {
185+
continue
186+
}
187+
if string(res.results.Status) == rpc.LibrarySearchStatus_name[int32(rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS)] {
184188
out.WriteString(tr(`Name: "%s"`, lib.Name) + "\n")
185189
if res.namesOnly {
186190
continue
@@ -190,14 +194,17 @@ func (res result) String() string {
190194
continue
191195
}
192196

193-
latest := lib.GetLatest()
197+
latest := lib.Latest
194198

195199
deps := []string{}
196-
for _, dep := range latest.GetDependencies() {
197-
if dep.GetVersionConstraint() == "" {
198-
deps = append(deps, dep.GetName())
200+
for _, dep := range latest.Dependencies {
201+
if dep == nil {
202+
continue
203+
}
204+
if dep.VersionConstraint == "" {
205+
deps = append(deps, dep.Name)
199206
} else {
200-
deps = append(deps, dep.GetName()+" ("+dep.GetVersionConstraint()+")")
207+
deps = append(deps, dep.Name+" ("+dep.VersionConstraint+")")
201208
}
202209
}
203210

@@ -212,7 +219,7 @@ func (res result) String() string {
212219
out.WriteString(fmt.Sprintf(" "+tr("Category: %s")+"\n", latest.Category))
213220
out.WriteString(fmt.Sprintf(" "+tr("Architecture: %s")+"\n", strings.Join(latest.Architectures, ", ")))
214221
out.WriteString(fmt.Sprintf(" "+tr("Types: %s")+"\n", strings.Join(latest.Types, ", ")))
215-
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.ReplaceAll(fmt.Sprint(lib.GetAvailableVersions()), " ", ", ")))
222+
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.ReplaceAll(fmt.Sprint(lib.AvailableVersions), " ", ", ")))
216223
if len(latest.ProvidesIncludes) > 0 {
217224
out.WriteString(fmt.Sprintf(" "+tr("Provides includes: %s")+"\n", strings.Join(latest.ProvidesIncludes, ", ")))
218225
}

0 commit comments

Comments
 (0)