Skip to content

Commit c9da144

Browse files
Add --temp-dir flag to allow users to choose where to download and extract images
1 parent fb5dcd7 commit c9da144

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

flash.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
func newFlashCmd() *cobra.Command {
3333
var forceYes bool
34+
var tempDir string
3435
appCmd := &cobra.Command{
3536
Use: "flash",
3637
Short: "Flash a Debian image on the board",
@@ -61,15 +62,17 @@ NOTE: On Windows, required drivers are automatically installed with elevated pri
6162
" " + os.Args[0] + " flash ./my-image.tar.zst\n" +
6263
" " + os.Args[0] + " flash /path/to/debian-image.tar.zst\n" +
6364
" " + os.Args[0] + " flash /path/to/debian-image.tar.xz \n" +
64-
" " + os.Args[0] + " flash /path/to/arduino-unoq-debian-image-20250915-173 \n",
65+
" " + os.Args[0] + " flash /path/to/arduino-unoq-debian-image-20250915-173 \n" +
66+
" " + os.Args[0] + " flash latest --temp-dir /path/to/custom/tempDir \n",
6567

6668
Args: cobra.ExactArgs(1),
6769
Run: func(cmd *cobra.Command, args []string) {
6870
checkDriversInstalled()
69-
runFlashCommand(cmd.Context(), args, forceYes)
71+
runFlashCommand(cmd.Context(), args, forceYes, tempDir)
7072
},
7173
}
7274
appCmd.Flags().BoolVarP(&forceYes, "yes", "y", false, "Automatically confirm all prompts")
75+
appCmd.Flags().StringVar(&tempDir, "temp-dir", "", "Path to the directory in which the image will be downloaded and extracted")
7376
// TODO: add --clean-install flag or something similar to distinguish between keeping and purging the /home directory
7477

7578
return appCmd
@@ -86,13 +89,13 @@ func checkDriversInstalled() {
8689
}
8790
}
8891

89-
func runFlashCommand(ctx context.Context, args []string, forceYes bool) {
92+
func runFlashCommand(ctx context.Context, args []string, forceYes bool, tempDir string) {
9093
imagePath, err := paths.New(args[0]).Abs()
9194
if err != nil {
9295
feedback.Fatal(i18n.Tr("could not find image absolute path: %v", err), feedback.ErrBadArgument)
9396
}
9497

95-
err = updater.Flash(ctx, imagePath, args[0], forceYes)
98+
err = updater.Flash(ctx, imagePath, args[0], forceYes, tempDir)
9699
if err != nil {
97100
feedback.Fatal(i18n.Tr("error flashing the board: %v", err), feedback.ErrBadArgument)
98101
}

updater/download_image.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type Release struct {
4646
// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
4747
type DownloadConfirmCB func(target string) (bool, error)
4848

49-
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
50-
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes)
49+
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, tempDir string) (*paths.Path, string, error) {
50+
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, tempDir)
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, tempDir string) (*paths.Path, string, error) {
7373
var err error
7474

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

114114
// Download the zip
115-
temp, err := GetTempDir("download-")
115+
temp, err := GetTempDir("download-", tempDir)
116116
if err != nil {
117117
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
118118
}
@@ -166,14 +166,18 @@ func ExtractImage(archive, temp *paths.Path) error {
166166

167167
// GetTempDir returns a temporary directory inside the user's cache directory.
168168
// The caller is responsible for removing the directory when no longer needed.
169-
func GetTempDir(prefix string) (*paths.Path, error) {
170-
userCacheDir, err := os.UserCacheDir()
171-
if err != nil {
172-
return nil, fmt.Errorf("could not get user's cache directory: %w", err)
173-
}
169+
func GetTempDir(prefix string, tempDir string) (*paths.Path, error) {
170+
cacheDir := paths.New(tempDir)
171+
172+
if cacheDir == nil {
173+
userCacheDir, err := os.UserCacheDir()
174+
if err != nil {
175+
return nil, fmt.Errorf("could not get user's cache directory: %w", err)
176+
}
174177

175-
cacheDir := paths.New(userCacheDir, "arduino-flasher-cli")
176-
_ = cacheDir.MkdirAll()
178+
cacheDir = paths.New(userCacheDir, "arduino-flasher-cli")
179+
_ = cacheDir.MkdirAll()
180+
}
177181

178182
temp, err := paths.MkTempDir(cacheDir.String(), prefix)
179183
if err != nil {

updater/flasher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/arduino/arduino-flasher-cli/updater/artifacts"
2929
)
3030

31-
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool) error {
31+
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool, tempDir string) error {
3232
if !imagePath.Exist() {
3333
client := NewClient()
3434

@@ -43,7 +43,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
4343
}
4444
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
4545
return yes, nil
46-
}, forceYes)
46+
}, forceYes, tempDir)
4747

4848
if err != nil {
4949
return fmt.Errorf("could not download and extract the image: %v", err)
@@ -59,7 +59,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
5959
version = v
6060
imagePath = tempImagePath
6161
} else if !imagePath.IsDir() {
62-
temp, err := GetTempDir("extract-")
62+
temp, err := GetTempDir("extract-", tempDir)
6363
if err != nil {
6464
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
6565
}

0 commit comments

Comments
 (0)