Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is part of arduino-flasher-cli.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-flasher-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package main

import (
"os"

"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"

"github.com/arduino/arduino-flasher-cli/feedback"
"github.com/arduino/arduino-flasher-cli/i18n"
"github.com/arduino/arduino-flasher-cli/updater"
)

func newDownloadCmd() *cobra.Command {
var destDir string
cmd := &cobra.Command{
Use: "download",
Short: "Download a Linux image to the specified path",
Args: cobra.ExactArgs(1),
Example: " " + os.Args[0] + " download latest\n" +
" " + os.Args[0] + " download latest --dest-dir /tmp\n",
Run: func(cmd *cobra.Command, args []string) {
runDownloadCommand(args, destDir)
},
}
cmd.Flags().StringVar(&destDir, "dest-dir", ".", "Path to the directory in which the image will be downloaded")

return cmd
}

func runDownloadCommand(args []string, destDir string) {
targetVersion := args[0]
downloadPath := paths.New(destDir)
if !downloadPath.IsDir() {
feedback.Fatal(i18n.Tr("error: %s is not a directory", destDir), feedback.ErrBadArgument)
}

client := updater.NewClient()
_, _, err := updater.DownloadImage(client, targetVersion, nil, true, downloadPath)
if err != nil {
feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument)
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {
newFlashCmd(),
newInstallDriversCmd(),
newListCmd(),
newDownloadCmd(),
&cobra.Command{
Use: "version",
Short: "Print the version number of Arduino Flasher CLI",
Expand Down
14 changes: 8 additions & 6 deletions updater/download_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Release struct {
type DownloadConfirmCB func(target string) (bool, error)

func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes)
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, nil)
if err != nil {
return nil, "", fmt.Errorf("error downloading the image: %v", err)
}
Expand All @@ -69,7 +69,7 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
return imagePath, version, nil
}

func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) {
var err error

feedback.Print(i18n.Tr("Checking for Debian image releases"))
Expand Down Expand Up @@ -112,12 +112,14 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
defer download.Close()

// Download the zip
temp, err := GetTempDir("download-")
if err != nil {
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
if downloadPath == nil {
downloadPath, err = GetTempDir("download-")
if err != nil {
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
}
}

tmpZip := temp.Join("update.tar.zst")
tmpZip := downloadPath.Join("arduino-unoq-debian-image-" + rel.Version + ".tar.zst")
tmpZipFile, err := tmpZip.Create()
if err != nil {
return nil, "", err
Expand Down