|
| 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 update |
| 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/cli/output" |
| 26 | + "github.com/arduino/arduino-cli/commands" |
| 27 | + "github.com/arduino/arduino-cli/commands/core" |
| 28 | + "github.com/arduino/arduino-cli/commands/lib" |
| 29 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 30 | + "github.com/arduino/arduino-cli/table" |
| 31 | + "github.com/sirupsen/logrus" |
| 32 | + "github.com/spf13/cobra" |
| 33 | +) |
| 34 | + |
| 35 | +// NewCommand creates a new `update` command |
| 36 | +func NewCommand() *cobra.Command { |
| 37 | + updateCommand := &cobra.Command{ |
| 38 | + Use: "update", |
| 39 | + Short: "Updates the index of cores and libraries", |
| 40 | + Long: "Updates the index of cores and libraries to the latest versions.", |
| 41 | + Example: " " + os.Args[0] + " update", |
| 42 | + Args: cobra.NoArgs, |
| 43 | + Run: runUpdateCommand, |
| 44 | + } |
| 45 | + updateCommand.Flags().BoolVar(&updateFlags.showOutdated, "outdated", false, "Show outdated cores and libraries after index update") |
| 46 | + return updateCommand |
| 47 | +} |
| 48 | + |
| 49 | +var updateFlags struct { |
| 50 | + showOutdated bool |
| 51 | +} |
| 52 | + |
| 53 | +func runUpdateCommand(cmd *cobra.Command, args []string) { |
| 54 | + instance := instance.CreateInstanceIgnorePlatformIndexErrors() |
| 55 | + |
| 56 | + logrus.Info("Executing `arduino update`") |
| 57 | + |
| 58 | + _, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{ |
| 59 | + Instance: instance, |
| 60 | + }, output.ProgressBar()) |
| 61 | + if err != nil { |
| 62 | + feedback.Errorf("Error updating core index: %v", err) |
| 63 | + os.Exit(errorcodes.ErrGeneric) |
| 64 | + } |
| 65 | + |
| 66 | + err = commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{ |
| 67 | + Instance: instance, |
| 68 | + }, output.ProgressBar()) |
| 69 | + if err != nil { |
| 70 | + feedback.Errorf("Error updating library index: %v", err) |
| 71 | + os.Exit(errorcodes.ErrGeneric) |
| 72 | + } |
| 73 | + |
| 74 | + if updateFlags.showOutdated { |
| 75 | + // Gets outdated cores |
| 76 | + targets, err := core.GetPlatforms(instance.Id, true) |
| 77 | + if err != nil { |
| 78 | + feedback.Errorf("Error retrieving core list: %v", err) |
| 79 | + os.Exit(errorcodes.ErrGeneric) |
| 80 | + } |
| 81 | + |
| 82 | + // Gets outdated libraries |
| 83 | + res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{ |
| 84 | + Instance: instance, |
| 85 | + All: false, |
| 86 | + Updatable: true, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + feedback.Errorf("Error retrieving library list: %v", err) |
| 90 | + os.Exit(errorcodes.ErrGeneric) |
| 91 | + } |
| 92 | + |
| 93 | + // Prints outdated cores |
| 94 | + tab := table.New() |
| 95 | + tab.SetHeader("Core name", "Installed version", "New version") |
| 96 | + if len(targets) > 0 { |
| 97 | + for _, t := range targets { |
| 98 | + plat := t.Platform |
| 99 | + tab.AddRow(plat.Name, t.Version, plat.GetLatestRelease().Version) |
| 100 | + } |
| 101 | + feedback.Print(tab.Render()) |
| 102 | + } |
| 103 | + |
| 104 | + // Prints outdated libraries |
| 105 | + tab = table.New() |
| 106 | + tab.SetHeader("Library name", "Installed version", "New version") |
| 107 | + libs := res.GetInstalledLibrary() |
| 108 | + if len(libs) > 0 { |
| 109 | + for _, l := range libs { |
| 110 | + tab.AddRow(l.Library.Name, l.Library.Version, l.Release.Version) |
| 111 | + } |
| 112 | + feedback.Print(tab.Render()) |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + logrus.Info("Done") |
| 118 | +} |
0 commit comments