Skip to content

Commit de882e4

Browse files
committed
Added lib deps command
1 parent 92db6f7 commit de882e4

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

Diff for: cli/lib/check_deps.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
25+
"github.com/arduino/arduino-cli/cli/errorcodes"
26+
"github.com/arduino/arduino-cli/cli/feedback"
27+
"github.com/arduino/arduino-cli/cli/globals"
28+
"github.com/arduino/arduino-cli/cli/instance"
29+
"github.com/arduino/arduino-cli/commands/lib"
30+
rpc "github.com/arduino/arduino-cli/rpc/commands"
31+
"github.com/fatih/color"
32+
"github.com/spf13/cobra"
33+
)
34+
35+
func initDepsCommand() *cobra.Command {
36+
depsCommand := &cobra.Command{
37+
Use: "deps LIBRARY[@VERSION_NUMBER](S)",
38+
Short: "Check dependencies status for the specified library.",
39+
Long: "Check dependencies status for the specified library.",
40+
Example: "" +
41+
" " + os.Args[0] + " lib deps AudioZero # for the latest version.\n" +
42+
" " + os.Args[0] + " lib deps [email protected] # for the specific version.",
43+
Args: cobra.ExactArgs(1),
44+
Run: runDepsCommand,
45+
}
46+
return depsCommand
47+
}
48+
49+
func runDepsCommand(cmd *cobra.Command, args []string) {
50+
libRef, err := globals.ParseLibraryReferenceArg(args[0])
51+
if err != nil {
52+
feedback.Errorf("Arguments error: %v", err)
53+
os.Exit(errorcodes.ErrBadArgument)
54+
}
55+
56+
instance := instance.CreateInstaceIgnorePlatformIndexErrors()
57+
deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{
58+
Instance: instance,
59+
Name: libRef.Name,
60+
Version: libRef.Version,
61+
})
62+
if err != nil {
63+
feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err)
64+
}
65+
66+
feedback.PrintResult(&checkDepResult{deps: deps})
67+
}
68+
69+
// output from this command requires special formatting, let's create a dedicated
70+
// feedback.Result implementation
71+
type checkDepResult struct {
72+
deps *rpc.LibraryResolveDependenciesResp
73+
}
74+
75+
func (dr checkDepResult) Data() interface{} {
76+
return dr.deps
77+
}
78+
79+
func (dr checkDepResult) String() string {
80+
res := ""
81+
for _, dep := range dr.deps.GetDependencies() {
82+
res += outputDep(dep)
83+
}
84+
return res
85+
}
86+
87+
func outputDep(dep *rpc.LibraryDependencyStatus) string {
88+
res := ""
89+
green := color.New(color.FgGreen)
90+
red := color.New(color.FgRed)
91+
yellow := color.New(color.FgYellow)
92+
if dep.GetVersionInstalled() == "" {
93+
res += fmt.Sprintf("%s must be installed.\n",
94+
red.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired()))
95+
} else if dep.GetVersionInstalled() == dep.GetVersionRequired() {
96+
res += fmt.Sprintf("%s is already installed.\n",
97+
green.Sprintf("✓ %s %s", dep.GetName(), dep.GetVersionRequired()))
98+
} else {
99+
res += fmt.Sprintf("%s is required but %s is currently installed.\n",
100+
yellow.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired()),
101+
yellow.Sprintf("%s", dep.GetVersionInstalled()))
102+
}
103+
return res
104+
}

Diff for: cli/lib/lib.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ func NewCommand() *cobra.Command {
4141
libCommand.AddCommand(initUninstallCommand())
4242
libCommand.AddCommand(initUpgradeCommand())
4343
libCommand.AddCommand(initUpdateIndexCommand())
44-
44+
libCommand.AddCommand(initDepsCommand())
4545
return libCommand
4646
}

0 commit comments

Comments
 (0)