|
| 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 core |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 25 | + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" |
| 26 | + "github.com/arduino/arduino-cli/commands" |
| 27 | + "github.com/arduino/arduino-cli/common/formatter" |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +func initUninstallCommand() *cobra.Command { |
| 33 | + return &cobra.Command{ |
| 34 | + Use: "uninstall PACKAGER:ARCH[@VERSION] ...", |
| 35 | + Short: "Uninstalls one or more cores and corresponding tool dependencies if no more used.", |
| 36 | + Long: "Uninstalls one or more cores and corresponding tool dependencies if no more used.", |
| 37 | + Example: " " + commands.AppName + " core uninstall arduino:samd\n", |
| 38 | + Args: cobra.MinimumNArgs(1), |
| 39 | + Run: runUninstallCommand, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func runUninstallCommand(cmd *cobra.Command, args []string) { |
| 44 | + logrus.Info("Executing `arduino core download`") |
| 45 | + |
| 46 | + platformsRefs := parsePlatformReferenceArgs(args) |
| 47 | + pm := commands.InitPackageManager() |
| 48 | + |
| 49 | + for _, platformRef := range platformsRefs { |
| 50 | + uninstallPlatformByRef(pm, platformRef) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func uninstallPlatformByRef(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) { |
| 55 | + platform, tools, err := pm.FindPlatformReleaseDependencies(platformRef) |
| 56 | + if err != nil { |
| 57 | + formatter.PrintError(err, "Could not determine platform dependencies") |
| 58 | + os.Exit(commands.ErrBadCall) |
| 59 | + } |
| 60 | + |
| 61 | + uninstallPlatformRelease(pm, platform) |
| 62 | + |
| 63 | + for _, tool := range tools { |
| 64 | + if !pm.IsToolRequired(tool) { |
| 65 | + fmt.Printf("Tool %s is no more required\n", tool) |
| 66 | + uninstallToolRelease(pm, tool) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) { |
| 72 | + log := pm.Log.WithField("platform", platformRelease) |
| 73 | + |
| 74 | + log.Info("Uninstalling platform") |
| 75 | + formatter.Print("Uninstalling " + platformRelease.String() + "...") |
| 76 | + |
| 77 | + if err := pm.UninstallPlatform(platformRelease); err != nil { |
| 78 | + log.WithError(err).Error("Error uninstalling") |
| 79 | + formatter.PrintError(err, "Error uninstalling "+platformRelease.String()) |
| 80 | + os.Exit(commands.ErrGeneric) |
| 81 | + } |
| 82 | + |
| 83 | + log.Info("Platform uninstalled") |
| 84 | + formatter.Print(platformRelease.String() + " uninstalled") |
| 85 | +} |
| 86 | + |
| 87 | +func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) { |
| 88 | + log := pm.Log.WithField("Tool", toolRelease) |
| 89 | + |
| 90 | + log.Info("Uninstalling tool") |
| 91 | + formatter.Print("Uninstalling " + toolRelease.String()) |
| 92 | + |
| 93 | + if err := pm.UninstallTool(toolRelease); err != nil { |
| 94 | + log.WithError(err).Error("Error uninstalling") |
| 95 | + formatter.PrintError(err, "Error uninstalling "+toolRelease.String()) |
| 96 | + os.Exit(commands.ErrGeneric) |
| 97 | + } |
| 98 | + |
| 99 | + log.Info("Tool uninstalled") |
| 100 | + formatter.Print(toolRelease.String() + " uninstalled") |
| 101 | +} |
0 commit comments