|
| 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 outdated |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 23 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 24 | + "github.com/arduino/arduino-cli/cli/instance" |
| 25 | + "github.com/arduino/arduino-cli/commands/core" |
| 26 | + "github.com/arduino/arduino-cli/commands/lib" |
| 27 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 28 | + "github.com/arduino/arduino-cli/table" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +// NewCommand creates a new `outdated` command |
| 34 | +func NewCommand() *cobra.Command { |
| 35 | + outdatedCommand := &cobra.Command{ |
| 36 | + Use: "outdated", |
| 37 | + Short: "Lists cores and libraries that can be upgraded\n", |
| 38 | + Long: "This commands shows a list of installed cores and/or libraries\n" + |
| 39 | + "that can be upgraded. If nothing needs to be updated the output is empty.", |
| 40 | + Example: " " + os.Args[0] + " outdated\n", |
| 41 | + Args: cobra.NoArgs, |
| 42 | + Run: runOutdatedCommand, |
| 43 | + } |
| 44 | + |
| 45 | + return outdatedCommand |
| 46 | +} |
| 47 | + |
| 48 | +func runOutdatedCommand(cmd *cobra.Command, args []string) { |
| 49 | + inst, err := instance.CreateInstance() |
| 50 | + if err != nil { |
| 51 | + feedback.Errorf("Error upgrading: %v", err) |
| 52 | + os.Exit(errorcodes.ErrGeneric) |
| 53 | + } |
| 54 | + |
| 55 | + logrus.Info("Executing `arduino outdated`") |
| 56 | + |
| 57 | + // Gets outdated cores |
| 58 | + targets, err := core.GetPlatforms(inst.Id, true) |
| 59 | + if err != nil { |
| 60 | + feedback.Errorf("Error retrieving core list: %v", err) |
| 61 | + os.Exit(errorcodes.ErrGeneric) |
| 62 | + } |
| 63 | + |
| 64 | + // Gets outdated libraries |
| 65 | + res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{ |
| 66 | + Instance: inst, |
| 67 | + All: true, |
| 68 | + Updatable: true, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + feedback.Errorf("Error retrieving library list: %v", err) |
| 72 | + os.Exit(errorcodes.ErrGeneric) |
| 73 | + } |
| 74 | + |
| 75 | + tab := table.New() |
| 76 | + tab.SetHeader("Name", "Installed version", "New version") |
| 77 | + |
| 78 | + // Prints outdated cores |
| 79 | + if len(targets) > 0 { |
| 80 | + for _, t := range targets { |
| 81 | + plat := t.Platform |
| 82 | + tab.AddRow(plat.Name, t.Version, plat.GetLatestRelease().Version) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Prints outdated libraries |
| 87 | + libs := res.GetInstalledLibrary() |
| 88 | + if len(libs) > 0 { |
| 89 | + for _, l := range libs { |
| 90 | + tab.AddRow(l.Library.Name, l.Library.Version, l.Release.Version) |
| 91 | + } |
| 92 | + } |
| 93 | + if len(targets) > 0 || len(libs) > 0 { |
| 94 | + feedback.Print(tab.Render()) |
| 95 | + } |
| 96 | + |
| 97 | + logrus.Info("Done") |
| 98 | +} |
0 commit comments