|
| 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 | + "os" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 25 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 26 | + "github.com/arduino/arduino-cli/cli/globals" |
| 27 | + "github.com/arduino/arduino-cli/cli/instance" |
| 28 | + "github.com/arduino/arduino-cli/commands/lib" |
| 29 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 30 | + "github.com/fatih/color" |
| 31 | + "github.com/spf13/cobra" |
| 32 | +) |
| 33 | + |
| 34 | +func initDepsCommand() *cobra.Command { |
| 35 | + depsCommand := &cobra.Command{ |
| 36 | + Use: "deps LIBRARY[@VERSION_NUMBER](S)", |
| 37 | + Short: "Check dependencies status for the specified library.", |
| 38 | + Long: "Check dependencies status for the specified library.", |
| 39 | + Example: "" + |
| 40 | + " " + os.Args[0] + " lib deps AudioZero # for the latest version.\n" + |
| 41 | + " " + os. Args[ 0] + " lib deps [email protected] # for the specific version.", |
| 42 | + Args: cobra.ExactArgs(1), |
| 43 | + Run: runDepsCommand, |
| 44 | + } |
| 45 | + return depsCommand |
| 46 | +} |
| 47 | + |
| 48 | +func runDepsCommand(cmd *cobra.Command, args []string) { |
| 49 | + libRef, err := globals.ParseLibraryReferenceArg(args[0]) |
| 50 | + if err != nil { |
| 51 | + feedback.Errorf("Arguments error: %v", err) |
| 52 | + os.Exit(errorcodes.ErrBadArgument) |
| 53 | + } |
| 54 | + |
| 55 | + instance := instance.CreateInstaceIgnorePlatformIndexErrors() |
| 56 | + deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{ |
| 57 | + Instance: instance, |
| 58 | + Name: libRef.Name, |
| 59 | + Version: libRef.Version, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err) |
| 63 | + } |
| 64 | + |
| 65 | + if globals.OutputFormat == "json" { |
| 66 | + feedback.PrintJSON(deps) |
| 67 | + } else { |
| 68 | + outputDeps(deps) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func outputDeps(deps *rpc.LibraryResolveDependenciesResp) { |
| 73 | + for _, dep := range deps.GetDependencies() { |
| 74 | + outputDep(dep) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func outputDep(dep *rpc.LibraryDependencyStatus) { |
| 79 | + green := color.New(color.FgGreen) |
| 80 | + red := color.New(color.FgRed) |
| 81 | + yellow := color.New(color.FgYellow) |
| 82 | + if dep.GetVersionInstalled() == "" { |
| 83 | + feedback.Printf("%s must be installed.", |
| 84 | + red.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired())) |
| 85 | + } else if dep.GetVersionInstalled() == dep.GetVersionRequired() { |
| 86 | + feedback.Printf("%s is already installed.", |
| 87 | + green.Sprintf("✓ %s %s", dep.GetName(), dep.GetVersionRequired())) |
| 88 | + } else { |
| 89 | + feedback.Printf("%s is required but %s is currently installed.", |
| 90 | + yellow.Sprintf("✕ %s %s", dep.GetName(), dep.GetVersionRequired()), |
| 91 | + yellow.Sprintf("%s", dep.GetVersionInstalled())) |
| 92 | + } |
| 93 | +} |
0 commit comments