Skip to content

Commit 71a8576

Browse files
authored
Added --omit-releases-details flag in lib search (arduino#2102)
* Added flag to LibrarySearchRequest gRPC API to reduce response output * Added --omit-releases-details flag in 'lib search'
1 parent fbeb271 commit 71a8576

File tree

4 files changed

+293
-260
lines changed

4 files changed

+293
-260
lines changed

Diff for: commands/lib/search.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
5353
}
5454

5555
if utils.Match(toTest, queryTerms) {
56-
res = append(res, indexLibraryToRPCSearchLibrary(lib))
56+
res = append(res, indexLibraryToRPCSearchLibrary(lib, req.GetOmitReleasesDetails()))
5757
}
5858
}
5959

@@ -74,17 +74,31 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
7474
}
7575

7676
// indexLibraryToRPCSearchLibrary converts a librariindex.Library to rpc.SearchLibrary
77-
func indexLibraryToRPCSearchLibrary(lib *librariesindex.Library) *rpc.SearchedLibrary {
78-
releases := map[string]*rpc.LibraryRelease{}
79-
for str, rel := range lib.Releases {
80-
releases[str] = getLibraryParameters(rel)
77+
func indexLibraryToRPCSearchLibrary(lib *librariesindex.Library, omitReleasesDetails bool) *rpc.SearchedLibrary {
78+
var releases map[string]*rpc.LibraryRelease
79+
if !omitReleasesDetails {
80+
releases = map[string]*rpc.LibraryRelease{}
81+
for str, rel := range lib.Releases {
82+
releases[str] = getLibraryParameters(rel)
83+
}
84+
}
85+
86+
versions := semver.List{}
87+
for _, rel := range lib.Releases {
88+
versions = append(versions, rel.Version)
89+
}
90+
sort.Sort(versions)
91+
92+
versionsString := []string{}
93+
for _, v := range versions {
94+
versionsString = append(versionsString, v.String())
8195
}
82-
latest := getLibraryParameters(lib.Latest)
8396

8497
return &rpc.SearchedLibrary{
85-
Name: lib.Name,
86-
Releases: releases,
87-
Latest: latest,
98+
Name: lib.Name,
99+
Releases: releases,
100+
Latest: getLibraryParameters(lib.Latest),
101+
AvailableVersions: versionsString,
88102
}
89103
}
90104

Diff for: internal/cli/lib/search.go

+9-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"fmt"
2121
"os"
22-
"sort"
2322
"strings"
2423
"time"
2524

@@ -32,29 +31,30 @@ import (
3231
"github.com/arduino/go-paths-helper"
3332
"github.com/sirupsen/logrus"
3433
"github.com/spf13/cobra"
35-
semver "go.bug.st/relaxed-semver"
3634
)
3735

3836
func initSearchCommand() *cobra.Command {
39-
var namesOnly bool // if true outputs lib names only.
37+
var namesOnly bool
38+
var omitReleasesDetails bool
4039
searchCommand := &cobra.Command{
4140
Use: fmt.Sprintf("search [%s]", tr("LIBRARY_NAME")),
4241
Short: tr("Searches for one or more libraries data."),
4342
Long: tr("Search for one or more libraries data (case insensitive search)."),
4443
Example: " " + os.Args[0] + " lib search audio",
4544
Args: cobra.ArbitraryArgs,
4645
Run: func(cmd *cobra.Command, args []string) {
47-
runSearchCommand(args, namesOnly)
46+
runSearchCommand(args, namesOnly, omitReleasesDetails)
4847
},
4948
}
5049
searchCommand.Flags().BoolVar(&namesOnly, "names", false, tr("Show library names only."))
50+
searchCommand.Flags().BoolVar(&omitReleasesDetails, "omit-releases-details", false, tr("Omit library details far all versions except the latest (produce a more compact JSON output)."))
5151
return searchCommand
5252
}
5353

5454
// indexUpdateInterval specifies the time threshold over which indexes are updated
5555
const indexUpdateInterval = 60 * time.Minute
5656

57-
func runSearchCommand(args []string, namesOnly bool) {
57+
func runSearchCommand(args []string, namesOnly bool, omitReleasesDetails bool) {
5858
inst, status := instance.Create()
5959
logrus.Info("Executing `arduino-cli lib search`")
6060

@@ -75,8 +75,9 @@ func runSearchCommand(args []string, namesOnly bool) {
7575
instance.Init(inst)
7676

7777
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
78-
Instance: inst,
79-
Query: strings.Join(args, " "),
78+
Instance: inst,
79+
Query: strings.Join(args, " "),
80+
OmitReleasesDetails: omitReleasesDetails,
8081
})
8182
if err != nil {
8283
feedback.Fatal(tr("Error searching for Libraries: %v", err), feedback.ErrGeneric)
@@ -166,7 +167,7 @@ func (res result) String() string {
166167
out.WriteString(fmt.Sprintf(" "+tr("Category: %s")+"\n", latest.Category))
167168
out.WriteString(fmt.Sprintf(" "+tr("Architecture: %s")+"\n", strings.Join(latest.Architectures, ", ")))
168169
out.WriteString(fmt.Sprintf(" "+tr("Types: %s")+"\n", strings.Join(latest.Types, ", ")))
169-
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.Replace(fmt.Sprint(versionsFromSearchedLibrary(lib)), " ", ", ", -1)))
170+
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.Replace(fmt.Sprint(lib.GetAvailableVersions()), " ", ", ", -1)))
170171
if len(latest.ProvidesIncludes) > 0 {
171172
out.WriteString(fmt.Sprintf(" "+tr("Provides includes: %s")+"\n", strings.Join(latest.ProvidesIncludes, ", ")))
172173
}
@@ -178,17 +179,6 @@ func (res result) String() string {
178179
return out.String()
179180
}
180181

181-
func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version {
182-
res := []*semver.Version{}
183-
for str := range library.Releases {
184-
if v, err := semver.Parse(str); err == nil {
185-
res = append(res, v)
186-
}
187-
}
188-
sort.Sort(semver.List(res))
189-
return res
190-
}
191-
192182
// indexNeedsUpdating returns whether library_index.json needs updating
193183
func indexNeedsUpdating(timeout time.Duration) bool {
194184
// Library index path is constant (relative to the data directory).

0 commit comments

Comments
 (0)