|
| 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 license@arduino.cc. |
| 16 | + */ |
| 17 | + |
| 18 | +package core |
| 19 | + |
| 20 | +import ( |
| 21 | + "os" |
| 22 | + |
| 23 | + "go.bug.st/downloader" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 26 | + "github.com/arduino/arduino-cli/arduino/cores/packagemanager" |
| 27 | + "github.com/arduino/arduino-cli/cli" |
| 28 | + "github.com/arduino/arduino-cli/common/formatter" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +func initDownloadCommand() *cobra.Command { |
| 34 | + downloadCommand := &cobra.Command{ |
| 35 | + Use: "download [PACKAGER:ARCH[=VERSION]](S)", |
| 36 | + Short: "Downloads one or more cores and corresponding tool dependencies.", |
| 37 | + Long: "Downloads one or more cores and corresponding tool dependencies.", |
| 38 | + Example: "" + |
| 39 | + " " + cli.AppName + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" + |
| 40 | + " " + cli.AppName + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).", |
| 41 | + Args: cobra.MinimumNArgs(1), |
| 42 | + Run: runDownloadCommand, |
| 43 | + } |
| 44 | + return downloadCommand |
| 45 | +} |
| 46 | + |
| 47 | +func runDownloadCommand(cmd *cobra.Command, args []string) { |
| 48 | + logrus.Info("Executing `arduino core download`") |
| 49 | + |
| 50 | + platformsRefs := parsePlatformReferenceArgs(args) |
| 51 | + pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles() |
| 52 | + for _, platformRef := range platformsRefs { |
| 53 | + downloadPlatformByRef(pm, platformRef) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) { |
| 58 | + platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef) |
| 59 | + if err != nil { |
| 60 | + formatter.PrintError(err, "Could not determine platform dependencies") |
| 61 | + os.Exit(cli.ErrBadCall) |
| 62 | + } |
| 63 | + downloadPlatform(pm, platform) |
| 64 | + for _, tool := range tools { |
| 65 | + downloadTool(pm, tool) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) { |
| 70 | + // Download platform |
| 71 | + resp, err := pm.DownloadPlatformRelease(platformRelease) |
| 72 | + download(resp, err, platformRelease.String()) |
| 73 | +} |
| 74 | + |
| 75 | +func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease) { |
| 76 | + // Check if tool has a flavor available for the current OS |
| 77 | + if tool.GetCompatibleFlavour() == nil { |
| 78 | + formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS") |
| 79 | + os.Exit(cli.ErrGeneric) |
| 80 | + } |
| 81 | + |
| 82 | + DownloadToolRelease(pm, tool) |
| 83 | +} |
| 84 | + |
| 85 | +// DownloadToolRelease downloads a ToolRelease |
| 86 | +func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) { |
| 87 | + resp, err := pm.DownloadToolRelease(toolRelease) |
| 88 | + download(resp, err, toolRelease.String()) |
| 89 | +} |
| 90 | + |
| 91 | +func download(d *downloader.Downloader, err error, label string) { |
| 92 | + if err != nil { |
| 93 | + formatter.PrintError(err, "Error downloading "+label) |
| 94 | + os.Exit(cli.ErrNetwork) |
| 95 | + } |
| 96 | + if d == nil { |
| 97 | + formatter.Print(label + " already downloaded") |
| 98 | + return |
| 99 | + } |
| 100 | + formatter.Print("Downloading " + label + "...") |
| 101 | + formatter.DownloadProgressBar(d, label) |
| 102 | + if d.Error() != nil { |
| 103 | + formatter.PrintError(d.Error(), "Error downloading "+label) |
| 104 | + os.Exit(cli.ErrNetwork) |
| 105 | + } |
| 106 | +} |
0 commit comments