Skip to content

Commit ff44d85

Browse files
feat: add download command (#22)
1 parent fb5dcd7 commit ff44d85

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

download.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is part of arduino-flasher-cli.
2+
//
3+
// Copyright 2025 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-flasher-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 license@arduino.cc.
15+
16+
package main
17+
18+
import (
19+
"os"
20+
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/arduino/arduino-flasher-cli/feedback"
25+
"github.com/arduino/arduino-flasher-cli/i18n"
26+
"github.com/arduino/arduino-flasher-cli/updater"
27+
)
28+
29+
func newDownloadCmd() *cobra.Command {
30+
var destDir string
31+
cmd := &cobra.Command{
32+
Use: "download",
33+
Short: "Download a Linux image to the specified path",
34+
Args: cobra.ExactArgs(1),
35+
Example: " " + os.Args[0] + " download latest\n" +
36+
" " + os.Args[0] + " download latest --dest-dir /tmp\n",
37+
Run: func(cmd *cobra.Command, args []string) {
38+
runDownloadCommand(args, destDir)
39+
},
40+
}
41+
cmd.Flags().StringVar(&destDir, "dest-dir", ".", "Path to the directory in which the image will be downloaded")
42+
43+
return cmd
44+
}
45+
46+
func runDownloadCommand(args []string, destDir string) {
47+
targetVersion := args[0]
48+
downloadPath := paths.New(destDir)
49+
if !downloadPath.IsDir() {
50+
feedback.Fatal(i18n.Tr("error: %s is not a directory", destDir), feedback.ErrBadArgument)
51+
}
52+
53+
client := updater.NewClient()
54+
_, _, err := updater.DownloadImage(client, targetVersion, nil, true, downloadPath)
55+
if err != nil {
56+
feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument)
57+
}
58+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func main() {
5555
newFlashCmd(),
5656
newInstallDriversCmd(),
5757
newListCmd(),
58+
newDownloadCmd(),
5859
&cobra.Command{
5960
Use: "version",
6061
Short: "Print the version number of Arduino Flasher CLI",

updater/download_image.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Release struct {
4747
type DownloadConfirmCB func(target string) (bool, error)
4848

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

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

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

114114
// Download the zip
115-
temp, err := GetTempDir("download-")
116-
if err != nil {
117-
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
115+
if downloadPath == nil {
116+
downloadPath, err = GetTempDir("download-")
117+
if err != nil {
118+
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
119+
}
118120
}
119121

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

0 commit comments

Comments
 (0)