Skip to content

Commit fb93127

Browse files
committed
Fixing test: solving regression in lib search output (part 2)
1 parent eb65649 commit fb93127

File tree

4 files changed

+101
-124
lines changed

4 files changed

+101
-124
lines changed

cli/lib/search.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,19 @@ func initSearchCommand() *cobra.Command {
4343
Args: cobra.ArbitraryArgs,
4444
Run: runSearchCommand,
4545
}
46-
searchCommand.Flags().BoolVar(&searchFlags.names, "names", false, "Show library names only.")
46+
searchCommand.Flags().BoolVar(&searchFlags.namesOnly, "names", false, "Show library names only.")
4747
return searchCommand
4848
}
4949

5050
var searchFlags struct {
51-
names bool // if true outputs lib names only.
51+
namesOnly bool // if true outputs lib names only.
5252
}
5353

5454
func runSearchCommand(cmd *cobra.Command, args []string) {
5555
instance := cli.CreateLibManagerOnlyInstace()
5656
logrus.Info("Executing `arduino lib search`")
57-
//arguments :=
5857
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
5958
Instance: instance,
60-
Names: searchFlags.names,
6159
Query: (strings.Join(args, " ")),
6260
})
6361
if err != nil {
@@ -67,33 +65,37 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
6765

6866
if cli.OutputJSONOrElse(searchResp) {
6967
results := searchResp.GetLibraries()
70-
if len(results) > 0 {
71-
for _, out := range results {
72-
fmt.Println(searchedLibraryToString(out, searchFlags.names))
68+
sort.Slice(results, func(i, j int) bool {
69+
return results[i].Name < results[j].Name
70+
})
71+
if searchFlags.namesOnly {
72+
for _, result := range results {
73+
fmt.Println(result.Name)
7374
}
7475
} else {
75-
formatter.Print("No libraries matching your search.")
76+
if len(results) > 0 {
77+
for _, result := range results {
78+
outputSearchedLibrary(result)
79+
}
80+
} else {
81+
formatter.Print("No libraries matching your search.")
82+
}
7683
}
7784
}
7885
logrus.Info("Done")
7986
}
8087

81-
func searchedLibraryToString(lsr *rpc.SearchedLibrary, 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(versionsFromSearchedLibrary(lsr)), " ", ", ", -1))
95-
}
96-
return strings.TrimSpace(ret)
88+
func outputSearchedLibrary(lsr *rpc.SearchedLibrary) {
89+
fmt.Printf("Name: \"%s\"\n", lsr.Name)
90+
fmt.Printf(" Author: %s\n", lsr.GetLatest().Author)
91+
fmt.Printf(" Maintainer: %s\n", lsr.GetLatest().Maintainer)
92+
fmt.Printf(" Sentence: %s\n", lsr.GetLatest().Sentence)
93+
fmt.Printf(" Paragraph: %s\n", lsr.GetLatest().Paragraph)
94+
fmt.Printf(" Website: %s\n", lsr.GetLatest().Website)
95+
fmt.Printf(" Category: %s\n", lsr.GetLatest().Category)
96+
fmt.Printf(" Architecture: %s\n", strings.Join(lsr.GetLatest().Architectures, ", "))
97+
fmt.Printf(" Types: %s\n", strings.Join(lsr.GetLatest().Types, ", "))
98+
fmt.Printf(" Versions: %s\n", strings.Replace(fmt.Sprint(versionsFromSearchedLibrary(lsr)), " ", ", ", -1))
9799
}
98100

99101
func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version {

commands/lib/search.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
)
2929

3030
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.LibrarySearchResp, error) {
31-
3231
lm := commands.GetLibraryManager(req)
3332
if lm == nil {
3433
return nil, errors.New("invalid instance")
@@ -53,21 +52,6 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.Library
5352
}
5453
}
5554

56-
if req.GetNames() {
57-
restmp := []*rpc.SearchedLibrary{}
58-
for _, lib := range res {
59-
searchedlib := &rpc.SearchedLibrary{
60-
Name: lib.Name,
61-
}
62-
restmp = append(restmp, searchedlib)
63-
}
64-
res = restmp
65-
} else {
66-
if len(res) == 0 {
67-
return &rpc.LibrarySearchResp{}, nil
68-
}
69-
}
70-
7155
return &rpc.LibrarySearchResp{Libraries: res}, nil
7256
}
7357

0 commit comments

Comments
 (0)