Skip to content

Commit 521f189

Browse files
committed
Added lib examples command
1 parent acde186 commit 521f189

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

cli/lib/examples.go

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package lib
17+
18+
import (
19+
"fmt"
20+
"os"
21+
"strings"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cli/cli/instance"
26+
"github.com/arduino/arduino-cli/commands/lib"
27+
rpc "github.com/arduino/arduino-cli/rpc/commands"
28+
"github.com/arduino/go-paths-helper"
29+
"github.com/fatih/color"
30+
"github.com/sirupsen/logrus"
31+
"github.com/spf13/cobra"
32+
"golang.org/x/net/context"
33+
)
34+
35+
func initExamplesCommand() *cobra.Command {
36+
examplesCommand := &cobra.Command{
37+
Use: "examples LIBRARY_NAME",
38+
Short: "Shows the list of the examples for the given library.",
39+
Long: "Shows the list of the examples for the given library.",
40+
Example: " " + os.Args[0] + " lib examples Wire",
41+
Args: cobra.ExactArgs(1),
42+
Run: runExamplesCommand,
43+
}
44+
return examplesCommand
45+
}
46+
47+
func runExamplesCommand(cmd *cobra.Command, args []string) {
48+
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
49+
logrus.Info("Show examples for library")
50+
51+
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
52+
Instance: instance,
53+
All: true,
54+
})
55+
if err != nil {
56+
feedback.Errorf("Error getting libraries info: %v", err)
57+
os.Exit(errorcodes.ErrGeneric)
58+
}
59+
60+
libs := res.GetInstalledLibrary()
61+
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+
}
69+
}
70+
71+
feedback.PrintResult(libraryExamplesResult{found})
72+
logrus.Info("Done")
73+
}
74+
75+
// output from this command requires special formatting, let's create a dedicated
76+
// feedback.Result implementation
77+
78+
type libraryExamples struct {
79+
Library *rpc.Library
80+
Examples []string
81+
}
82+
83+
type libraryExamplesResult struct {
84+
Examples []*libraryExamples
85+
}
86+
87+
func (ir libraryExamplesResult) Data() interface{} {
88+
return ir.Examples
89+
}
90+
91+
func (ir libraryExamplesResult) String() string {
92+
if ir.Examples == nil || len(ir.Examples) == 0 {
93+
return "No libraries found."
94+
}
95+
96+
res := []string{}
97+
for _, lib := range ir.Examples {
98+
name := lib.Library.Name
99+
if lib.Library.ContainerPlatform != "" {
100+
name += " (" + lib.Library.GetContainerPlatform() + ")"
101+
} else if lib.Library.Location != rpc.LibraryLocation_user {
102+
name += " (" + lib.Library.GetLocation().String() + ")"
103+
}
104+
r := fmt.Sprintf("Examples for library %s\n", color.GreenString("%s", name))
105+
for _, example := range lib.Examples {
106+
examplePath := paths.New(example)
107+
r += fmt.Sprintf(" - %s%c%s\n",
108+
color.New(color.Faint).Sprintf("%s", examplePath.Parent()),
109+
os.PathSeparator,
110+
color.HiWhiteString("%s", examplePath.Base()))
111+
}
112+
res = append(res, r)
113+
}
114+
115+
return strings.Join(res, "\n")
116+
}

cli/lib/lib.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func NewCommand() *cobra.Command {
3535
libCommand.AddCommand(initDownloadCommand())
3636
libCommand.AddCommand(initInstallCommand())
3737
libCommand.AddCommand(initListCommand())
38+
libCommand.AddCommand(initExamplesCommand())
3839
libCommand.AddCommand(initSearchCommand())
3940
libCommand.AddCommand(initUninstallCommand())
4041
libCommand.AddCommand(initUpgradeCommand())

0 commit comments

Comments
 (0)