Skip to content

Commit e8a1a28

Browse files
Rocketctcmaglie
authored andcommitted
fix lib search output
1 parent f7128ff commit e8a1a28

File tree

5 files changed

+378
-83
lines changed

5 files changed

+378
-83
lines changed

cli/lib/search.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package lib
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"os"
24+
"sort"
2325

2426
"strings"
2527

@@ -29,6 +31,7 @@ import (
2931
"github.com/arduino/arduino-cli/rpc"
3032
"github.com/sirupsen/logrus"
3133
"github.com/spf13/cobra"
34+
semver "go.bug.st/relaxed-semver"
3235
)
3336

3437
func initSearchCommand() *cobra.Command {
@@ -51,15 +54,56 @@ var searchFlags struct {
5154
func runSearchCommand(cmd *cobra.Command, args []string) {
5255
instance := cli.CreateInstance()
5356
logrus.Info("Executing `arduino lib search`")
54-
arguments := strings.ToLower(strings.Join(args, " "))
55-
_, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
57+
//arguments :=
58+
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
5659
Instance: instance,
5760
Names: searchFlags.names,
58-
Query: arguments,
61+
Query: (strings.Join(args, " ")),
5962
})
6063
if err != nil {
6164
formatter.PrintError(err, "Error saerching for Library")
6265
os.Exit(cli.ErrGeneric)
6366
}
67+
68+
results := searchResp.GetSearchOutput()
69+
if cli.OutputJSONOrElse(results) {
70+
if len(results) > 0 {
71+
for _, out := range results {
72+
fmt.Println(SearchOutputToString(out, searchFlags.names))
73+
}
74+
} else {
75+
formatter.Print("No libraries matching your search.")
76+
}
77+
}
6478
logrus.Info("Done")
6579
}
80+
81+
func SearchOutputToString(lsr *rpc.SearchLibraryOutput, names bool) string {
82+
ret := ""
83+
84+
ret += fmt.Sprintf("Name: \"%s\"\n", lsr.Name)
85+
if !names {
86+
ret += fmt.Sprintln(" Author: ", lsr.GetLatest().Author) +
87+
fmt.Sprintln(" Maintainer: ", lsr.GetLatest().Maintainer) +
88+
fmt.Sprintln(" Sentence: ", lsr.GetLatest().Sentence) +
89+
fmt.Sprintln(" Paragraph: ", lsr.GetLatest().Paragraph) +
90+
fmt.Sprintln(" Website: ", lsr.GetLatest().Website) +
91+
fmt.Sprintln(" Category: ", lsr.GetLatest().Category) +
92+
fmt.Sprintln(" Architecture: ", strings.Join(lsr.GetLatest().Architectures, ", ")) +
93+
fmt.Sprintln(" Types: ", strings.Join(lsr.GetLatest().Types, ", ")) +
94+
fmt.Sprintln(" Versions: ", strings.Replace(fmt.Sprint(Versions(lsr.GetLatest(), lsr)), " ", ", ", -1))
95+
}
96+
return strings.TrimSpace(ret)
97+
}
98+
99+
func Versions(l *rpc.LibraryRelease, library *rpc.SearchLibraryOutput) []*semver.Version {
100+
res := []*semver.Version{}
101+
for str, _ := range library.Releases {
102+
v, err := semver.Parse(str)
103+
if err == nil {
104+
res = append(res, v)
105+
}
106+
}
107+
sort.Sort(semver.List(res))
108+
return res
109+
}

commands/lib/search.go

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,75 @@ package lib
1919

2020
import (
2121
"context"
22-
"fmt"
22+
"errors"
2323
"strings"
2424

25+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2526
"github.com/arduino/arduino-cli/commands"
26-
"github.com/arduino/arduino-cli/common/formatter"
2727
"github.com/arduino/arduino-cli/rpc"
2828
)
2929

3030
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.LibrarySearchResp, error) {
3131

3232
lm := commands.GetLibraryManager(req)
33+
if lm == nil {
34+
return nil, errors.New("invalid instance")
35+
}
3336

3437
res := []*rpc.SearchLibraryOutput{}
35-
release, index string
3638

3739
for _, lib := range lm.Index.Libraries {
38-
if strings.Contains(strings.ToLower(lib.Name), req.GetQuery()) {
39-
for rel := range lib.Releases {
40-
release += rel;
40+
if strings.Contains(strings.ToLower(lib.Name), strings.ToLower(req.GetQuery())) {
41+
releases := map[string]*rpc.LibraryRelease{}
42+
for str, rel := range lib.Releases {
43+
releases[str] = GetLibraryParameters(rel)
4144
}
45+
latest := GetLibraryParameters(lib.Latest)
4246

43-
for idx := range &lib.Libraries {
44-
index =
45-
}
46-
res = append(res, &rpc.SearchLibraryOutput{
47+
searchedlib := &rpc.SearchLibraryOutput{
4748
Name: lib.Name,
48-
Releases: release,
49-
Latest: lib.Latest.String(),
50-
Index: index,
51-
})
49+
Releases: releases,
50+
Latest: latest,
51+
}
52+
res = append(res, searchedlib)
5253
}
5354
}
5455

5556
if req.GetNames() {
56-
for _, lib := range res.Libraries {
57-
formatter.Print(lib.Name)
57+
restmp := []*rpc.SearchLibraryOutput{}
58+
for _, lib := range res {
59+
searchedlib := &rpc.SearchLibraryOutput{
60+
Name: lib.Name,
61+
}
62+
restmp = append(restmp, searchedlib)
5863
}
64+
res = restmp
5965
} else {
60-
if len(res.Libraries) == 0 {
61-
formatter.Print(fmt.Sprintf("No library found matching `%s` search query", req.GetQuery()))
62-
} else {
63-
formatter.Print(res)
66+
if len(res) == 0 {
67+
return &rpc.LibrarySearchResp{}, nil
6468
}
6569
}
6670

67-
return &rpc.LibrarySearchResp{}, nil
71+
return &rpc.LibrarySearchResp{SearchOutput: res}, nil
72+
}
73+
func GetLibraryParameters(rel *librariesindex.Release) *rpc.LibraryRelease {
74+
75+
return &rpc.LibraryRelease{
76+
Author: rel.Author,
77+
Version: rel.Version.String(),
78+
Maintainer: rel.Maintainer,
79+
Sentence: rel.Sentence,
80+
Paragraph: rel.Paragraph,
81+
Website: rel.Website,
82+
Category: rel.Category,
83+
Architectures: rel.Architectures,
84+
Types: rel.Types,
85+
Resources: &rpc.DownloadResource{
86+
Url: rel.Resource.URL,
87+
Archivefilename: rel.Resource.ArchiveFileName,
88+
Checksum: rel.Resource.Checksum,
89+
Size: rel.Resource.Size,
90+
Cachepath: rel.Resource.CachePath,
91+
},
92+
}
6893
}

daemon/client/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os"
2525

26+
"github.com/arduino/arduino-cli/common/formatter"
2627
"github.com/arduino/arduino-cli/rpc"
2728
"google.golang.org/grpc"
2829
)
@@ -381,6 +382,20 @@ func main() {
381382
}
382383
}
383384

385+
// LIB SEARCH
386+
fmt.Println("=== calling LibrarySearch(audio)")
387+
outputsrc, err := client.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
388+
Instance: instance,
389+
Names: true,
390+
Query: "audio",
391+
})
392+
if err != nil {
393+
formatter.PrintError(err, "Error saerching for Library")
394+
os.Exit(1)
395+
}
396+
397+
fmt.Println(outputsrc)
398+
384399
// LIB UNINSTALL
385400
fmt.Println("=== calling LibraryUninstall(WiFi101)")
386401
libUninstallRespStream, err := client.LibraryUninstall(context.Background(), &rpc.LibraryUninstallReq{

0 commit comments

Comments
 (0)