@@ -22,6 +22,7 @@ import (
2222 "encoding/hex"
2323 "fmt"
2424 "io"
25+ "os"
2526
2627 "github.com/arduino/go-paths-helper"
2728 "github.com/codeclysm/extract/v4"
@@ -45,24 +46,27 @@ type Release struct {
4546// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
4647type DownloadConfirmCB func (target string ) (bool , error )
4748
48- func DownloadAndExtract (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , error ) {
49+ func DownloadAndExtract (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , string , error ) {
4950 tmpZip , version , err := DownloadImage (client , targetVersion , upgradeConfirmCb , forceYes )
5051 if err != nil {
51- return nil , fmt .Errorf ("error downloading the image: %v" , err )
52+ return nil , "" , fmt .Errorf ("error downloading the image: %v" , err )
5253 }
5354
5455 // Download not confirmed
5556 if tmpZip == nil {
56- return nil , nil
57+ return nil , "" , nil
5758 }
5859
5960 err = ExtractImage (tmpZip , tmpZip .Parent ())
6061 if err != nil {
61- return nil , fmt .Errorf ("error extracting the image: %v" , err )
62+ return nil , "" , fmt .Errorf ("error extracting the image: %v" , err )
6263 }
6364
6465 imagePath := tmpZip .Parent ().Join ("arduino-unoq-debian-image-" + version )
65- return imagePath , nil
66+ if targetVersion == "latest" {
67+ version += "(latest)"
68+ }
69+ return imagePath , version , nil
6670}
6771
6872func DownloadImage (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , string , error ) {
@@ -108,7 +112,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
108112 defer download .Close ()
109113
110114 // Download the zip
111- temp , err := paths . MkTempDir ( "" , "flasher-updater -" )
115+ temp , err := GetTempDir ( "download -" )
112116 if err != nil {
113117 return nil , "" , fmt .Errorf ("could not create temporary download directory: %w" , err )
114118 }
@@ -159,3 +163,21 @@ func ExtractImage(archive, temp *paths.Path) error {
159163 }
160164 return nil
161165}
166+
167+ // GetTempDir returns a temporary directory inside the user's cache directory.
168+ // 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+ }
174+
175+ cacheDir := paths .New (userCacheDir , "arduino-flasher-cli" )
176+ _ = cacheDir .MkdirAll ()
177+
178+ temp , err := paths .MkTempDir (cacheDir .String (), prefix )
179+ if err != nil {
180+ return nil , fmt .Errorf ("could not create .cache directory: %w" , err )
181+ }
182+ return temp , nil
183+ }
0 commit comments