Skip to content

Commit 4276dfd

Browse files
author
Massimiliano Pippi
committed
make lib search format agnostic
1 parent 3f8a384 commit 4276dfd

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

cli/lib/search.go

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
"github.com/arduino/arduino-cli/cli/errorcodes"
2828
"github.com/arduino/arduino-cli/cli/feedback"
29-
"github.com/arduino/arduino-cli/cli/globals"
3029
"github.com/arduino/arduino-cli/cli/instance"
3130
"github.com/arduino/arduino-cli/commands/lib"
3231
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -64,63 +63,75 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
6463
os.Exit(errorcodes.ErrGeneric)
6564
}
6665

67-
if globals.OutputFormat == "json" {
68-
if searchFlags.namesOnly {
69-
type LibName struct {
70-
Name string `json:"name,required"`
71-
}
72-
73-
type NamesOnly struct {
74-
Libraries []LibName `json:"libraries,required"`
75-
}
76-
77-
names := []LibName{}
78-
results := searchResp.GetLibraries()
79-
for _, lsr := range results {
80-
names = append(names, LibName{lsr.Name})
81-
}
82-
feedback.PrintJSON(NamesOnly{
83-
names,
84-
})
85-
} else {
86-
feedback.PrintJSON(searchResp)
66+
// get a sorted slice of results
67+
results := searchResp.GetLibraries()
68+
sort.Slice(results, func(i, j int) bool {
69+
return results[i].Name < results[j].Name
70+
})
71+
72+
feedback.PrintResult(result{
73+
results: results,
74+
namesOnly: searchFlags.namesOnly,
75+
})
76+
77+
logrus.Info("Done")
78+
}
79+
80+
// ouput from this command requires special formatting, let's create a dedicated
81+
// feedback.Result implementation
82+
type result struct {
83+
results []*rpc.SearchedLibrary
84+
namesOnly bool
85+
}
86+
87+
func (res result) Data() interface{} {
88+
if res.namesOnly {
89+
type LibName struct {
90+
Name string `json:"name,required"`
91+
}
92+
93+
type NamesOnly struct {
94+
Libraries []LibName `json:"libraries,required"`
95+
}
96+
97+
names := []LibName{}
98+
for _, lsr := range res.results {
99+
names = append(names, LibName{lsr.Name})
100+
}
101+
102+
return NamesOnly{
103+
names,
87104
}
88-
} else {
89-
// get a sorted slice of results
90-
results := searchResp.GetLibraries()
91-
sort.Slice(results, func(i, j int) bool {
92-
return results[i].Name < results[j].Name
93-
})
94-
95-
// print all the things
96-
outputSearchedLibrary(results, searchFlags.namesOnly)
97105
}
98106

99-
logrus.Info("Done")
107+
return res.results
100108
}
101109

102-
func outputSearchedLibrary(results []*rpc.SearchedLibrary, namesOnly bool) {
103-
if len(results) == 0 {
104-
feedback.Print("No libraries matching your search.")
105-
return
110+
func (res result) String() string {
111+
if len(res.results) == 0 {
112+
return "No libraries matching your search."
106113
}
107114

108-
for _, lsr := range results {
109-
feedback.Printf(`Name: "%s"`, lsr.Name)
110-
if namesOnly {
115+
var out strings.Builder
116+
117+
for _, lsr := range res.results {
118+
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lsr.Name))
119+
if res.namesOnly {
111120
continue
112121
}
113122

114-
feedback.Printf(" Author: %s", lsr.GetLatest().Author)
115-
feedback.Printf(" Maintainer: %s", lsr.GetLatest().Maintainer)
116-
feedback.Printf(" Sentence: %s", lsr.GetLatest().Sentence)
117-
feedback.Printf(" Paragraph: %s", lsr.GetLatest().Paragraph)
118-
feedback.Printf(" Website: %s", lsr.GetLatest().Website)
119-
feedback.Printf(" Category: %s", lsr.GetLatest().Category)
120-
feedback.Printf(" Architecture: %s", strings.Join(lsr.GetLatest().Architectures, ", "))
121-
feedback.Printf(" Types: %s", strings.Join(lsr.GetLatest().Types, ", "))
122-
feedback.Printf(" Versions: %s", strings.Replace(fmt.Sprint(versionsFromSearchedLibrary(lsr)), " ", ", ", -1))
123+
out.WriteString(fmt.Sprintf(" Author: %s\n", lsr.GetLatest().Author))
124+
out.WriteString(fmt.Sprintf(" Maintainer: %s\n", lsr.GetLatest().Maintainer))
125+
out.WriteString(fmt.Sprintf(" Sentence: %s\n", lsr.GetLatest().Sentence))
126+
out.WriteString(fmt.Sprintf(" Paragraph: %s\n", lsr.GetLatest().Paragraph))
127+
out.WriteString(fmt.Sprintf(" Website: %s\n", lsr.GetLatest().Website))
128+
out.WriteString(fmt.Sprintf(" Category: %s\n", lsr.GetLatest().Category))
129+
out.WriteString(fmt.Sprintf(" Architecture: %s\n", strings.Join(lsr.GetLatest().Architectures, ", ")))
130+
out.WriteString(fmt.Sprintf(" Types: %s\n", strings.Join(lsr.GetLatest().Types, ", ")))
131+
out.WriteString(fmt.Sprintf(" Versions: %s\n", strings.Replace(fmt.Sprint(versionsFromSearchedLibrary(lsr)), " ", ", ", -1)))
123132
}
133+
134+
return fmt.Sprintf("%s", out.String())
124135
}
125136

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

0 commit comments

Comments
 (0)