Skip to content

Commit 9ae5d76

Browse files
committed
Allow library listing filter by name
1 parent 5455937 commit 9ae5d76

File tree

5 files changed

+135
-109
lines changed

5 files changed

+135
-109
lines changed

cli/lib/examples.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,19 @@ func runExamplesCommand(cmd *cobra.Command, args []string) {
5151
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
5252
Instance: instance,
5353
All: true,
54+
Name: args[0],
5455
})
5556
if err != nil {
5657
feedback.Errorf("Error getting libraries info: %v", err)
5758
os.Exit(errorcodes.ErrGeneric)
5859
}
5960

60-
libs := res.GetInstalledLibrary()
6161
found := []*libraryExamples{}
62-
for _, lib := range libs {
63-
if lib.Library.Name == args[0] {
64-
found = append(found, &libraryExamples{
65-
Library: lib.Library,
66-
Examples: lib.Library.Examples,
67-
})
68-
}
62+
for _, lib := range res.GetInstalledLibrary() {
63+
found = append(found, &libraryExamples{
64+
Library: lib.Library,
65+
Examples: lib.Library.Examples,
66+
})
6967
}
7068

7169
feedback.PrintResult(libraryExamplesResult{found})

cli/lib/list.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ import (
3131

3232
func initListCommand() *cobra.Command {
3333
listCommand := &cobra.Command{
34-
Use: "list",
35-
Short: "Shows a list of all installed libraries.",
36-
Long: "Shows a list of all installed libraries.",
34+
Use: "list [LIBNAME]",
35+
Short: "Shows a list of installed libraries.",
36+
Long: "Shows a list of installed libraries.\n\n" +
37+
"If the LIBNAME parameter is specified the listing is limited to that specific\n" +
38+
"library. By default the libraries provided as built-in by platforms/core are\n" +
39+
"not listed, they can be listed by adding the --all flag.",
3740
Example: " " + os.Args[0] + " lib list",
38-
Args: cobra.NoArgs,
41+
Args: cobra.MaximumNArgs(1),
3942
Run: runListCommand,
4043
}
4144
listCommand.Flags().BoolVar(&listFlags.all, "all", false, "Include built-in libraries (from platforms and IDE) in listing.")
@@ -52,10 +55,16 @@ func runListCommand(cmd *cobra.Command, args []string) {
5255
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
5356
logrus.Info("Listing")
5457

58+
name := ""
59+
if len(args) > 0 {
60+
name = args[0]
61+
}
62+
5563
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
5664
Instance: instance,
5765
All: listFlags.all,
5866
Updatable: listFlags.updatable,
67+
Name: name,
5968
})
6069
if err != nil {
6170
feedback.Errorf("Error listing Libraries: %v", err)

commands/lib/list.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package lib
1717

1818
import (
1919
"context"
20+
"strings"
2021

2122
"github.com/arduino/arduino-cli/arduino/libraries"
2223
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -34,10 +35,15 @@ type installedLib struct {
3435
func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {
3536
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3637

38+
nameFilter := strings.ToLower(req.GetName())
39+
3740
instaledLib := []*rpc.InstalledLibrary{}
3841
res := listLibraries(lm, req.GetUpdatable(), req.GetAll())
3942
if len(res) > 0 {
4043
for _, lib := range res {
44+
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
45+
continue
46+
}
4147
libtmp := GetOutputLibrary(lib.Library)
4248
release := GetOutputRelease(lib.Available)
4349
instaledLib = append(instaledLib, &rpc.InstalledLibrary{

0 commit comments

Comments
 (0)