diff --git a/cli/lib/check_deps.go b/cli/lib/check_deps.go new file mode 100644 index 00000000000..1c3c94dd707 --- /dev/null +++ b/cli/lib/check_deps.go @@ -0,0 +1,93 @@ +/* + * This file is part of arduino-cli. + * + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) + * + * This software is released under the GNU General Public License version 3, + * which covers the main part of arduino-cli. + * The terms of this license can be found at: + * https://www.gnu.org/licenses/gpl-3.0.en.html + * + * You can be released from the requirements of the above licenses by purchasing + * a commercial license. Buying such a license is mandatory if you want to modify or + * otherwise use the software for commercial activities involving the Arduino + * software without disclosing the source code of your own applications. To purchase + * a commercial license, send an email to license@arduino.cc. + */ + +package lib + +import ( + "context" + "os" + + "github.com/arduino/arduino-cli/cli/errorcodes" + "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/arduino-cli/cli/globals" + "github.com/arduino/arduino-cli/cli/instance" + "github.com/arduino/arduino-cli/commands/lib" + rpc "github.com/arduino/arduino-cli/rpc/commands" + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +func initDepsCommand() *cobra.Command { + depsCommand := &cobra.Command{ + Use: "deps LIBRARY[@VERSION_NUMBER](S)", + Short: "Check dependencies status for the specified library.", + Long: "Check dependencies status for the specified library.", + Example: "" + + " " + os.Args[0] + " lib deps AudioZero # for the latest version.\n" + + " " + os.Args[0] + " lib deps AudioZero@1.0.0 # for the specific version.", + Args: cobra.ExactArgs(1), + Run: runDepsCommand, + } + return depsCommand +} + +func runDepsCommand(cmd *cobra.Command, args []string) { + libRef, err := globals.ParseLibraryReferenceArg(args[0]) + if err != nil { + feedback.Errorf("Arguments error: %v", err) + os.Exit(errorcodes.ErrBadArgument) + } + + instance := instance.CreateInstaceIgnorePlatformIndexErrors() + deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{ + Instance: instance, + Name: libRef.Name, + Version: libRef.Version, + }) + if err != nil { + feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err) + } + + if globals.OutputFormat == "json" { + feedback.PrintJSON(deps) + } else { + outputDeps(deps) + } +} + +func outputDeps(deps *rpc.LibraryResolveDependenciesResp) { + for _, dep := range deps.GetDependencies() { + outputDep(dep) + } +} + +func outputDep(dep *rpc.LibraryDependencyStatus) { + green := color.New(color.FgGreen) + red := color.New(color.FgRed) + yellow := color.New(color.FgYellow) + if dep.GetVersionInstalled() == "" { + feedback.Printf("%s must be installed.", + red.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired())) + } else if dep.GetVersionInstalled() == dep.GetVersionRequired() { + feedback.Printf("%s is already installed.", + green.Sprintf("✓ %s %s", dep.GetName(), dep.GetVersionRequired())) + } else { + feedback.Printf("%s is required but %s is currently installed.", + yellow.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired()), + yellow.Sprintf("%s", dep.GetVersionInstalled())) + } +} diff --git a/cli/lib/lib.go b/cli/lib/lib.go index 08ad40c22cf..4803a0248ad 100644 --- a/cli/lib/lib.go +++ b/cli/lib/lib.go @@ -41,6 +41,6 @@ func NewCommand() *cobra.Command { libCommand.AddCommand(initUninstallCommand()) libCommand.AddCommand(initUpgradeCommand()) libCommand.AddCommand(initUpdateIndexCommand()) - + libCommand.AddCommand(initDepsCommand()) return libCommand }