Skip to content

Commit

Permalink
Added --omit-releases-details flag in lib search (#2102)
Browse files Browse the repository at this point in the history
* Added flag to LibrarySearchRequest gRPC API to reduce response output

* Added --omit-releases-details flag in 'lib search'
  • Loading branch information
cmaglie authored Mar 13, 2023
1 parent fbeb271 commit 71a8576
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 260 deletions.
32 changes: 23 additions & 9 deletions commands/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
}

if utils.Match(toTest, queryTerms) {
res = append(res, indexLibraryToRPCSearchLibrary(lib))
res = append(res, indexLibraryToRPCSearchLibrary(lib, req.GetOmitReleasesDetails()))
}
}

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

// indexLibraryToRPCSearchLibrary converts a librariindex.Library to rpc.SearchLibrary
func indexLibraryToRPCSearchLibrary(lib *librariesindex.Library) *rpc.SearchedLibrary {
releases := map[string]*rpc.LibraryRelease{}
for str, rel := range lib.Releases {
releases[str] = getLibraryParameters(rel)
func indexLibraryToRPCSearchLibrary(lib *librariesindex.Library, omitReleasesDetails bool) *rpc.SearchedLibrary {
var releases map[string]*rpc.LibraryRelease
if !omitReleasesDetails {
releases = map[string]*rpc.LibraryRelease{}
for str, rel := range lib.Releases {
releases[str] = getLibraryParameters(rel)
}
}

versions := semver.List{}
for _, rel := range lib.Releases {
versions = append(versions, rel.Version)
}
sort.Sort(versions)

versionsString := []string{}
for _, v := range versions {
versionsString = append(versionsString, v.String())
}
latest := getLibraryParameters(lib.Latest)

return &rpc.SearchedLibrary{
Name: lib.Name,
Releases: releases,
Latest: latest,
Name: lib.Name,
Releases: releases,
Latest: getLibraryParameters(lib.Latest),
AvailableVersions: versionsString,
}
}

Expand Down
28 changes: 9 additions & 19 deletions internal/cli/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"

Expand All @@ -32,29 +31,30 @@ import (
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
semver "go.bug.st/relaxed-semver"
)

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

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

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

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

searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
Instance: inst,
Query: strings.Join(args, " "),
Instance: inst,
Query: strings.Join(args, " "),
OmitReleasesDetails: omitReleasesDetails,
})
if err != nil {
feedback.Fatal(tr("Error searching for Libraries: %v", err), feedback.ErrGeneric)
Expand Down Expand Up @@ -166,7 +167,7 @@ func (res result) String() string {
out.WriteString(fmt.Sprintf(" "+tr("Category: %s")+"\n", latest.Category))
out.WriteString(fmt.Sprintf(" "+tr("Architecture: %s")+"\n", strings.Join(latest.Architectures, ", ")))
out.WriteString(fmt.Sprintf(" "+tr("Types: %s")+"\n", strings.Join(latest.Types, ", ")))
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.Replace(fmt.Sprint(versionsFromSearchedLibrary(lib)), " ", ", ", -1)))
out.WriteString(fmt.Sprintf(" "+tr("Versions: %s")+"\n", strings.Replace(fmt.Sprint(lib.GetAvailableVersions()), " ", ", ", -1)))
if len(latest.ProvidesIncludes) > 0 {
out.WriteString(fmt.Sprintf(" "+tr("Provides includes: %s")+"\n", strings.Join(latest.ProvidesIncludes, ", ")))
}
Expand All @@ -178,17 +179,6 @@ func (res result) String() string {
return out.String()
}

func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version {
res := []*semver.Version{}
for str := range library.Releases {
if v, err := semver.Parse(str); err == nil {
res = append(res, v)
}
}
sort.Sort(semver.List(res))
return res
}

// indexNeedsUpdating returns whether library_index.json needs updating
func indexNeedsUpdating(timeout time.Duration) bool {
// Library index path is constant (relative to the data directory).
Expand Down
Loading

0 comments on commit 71a8576

Please sign in to comment.