diff --git a/updater/download_image.go b/updater/download_image.go index b5a3170..8c8b603 100644 --- a/updater/download_image.go +++ b/updater/download_image.go @@ -22,6 +22,7 @@ import ( "encoding/hex" "fmt" "io" + "os" "github.com/arduino/go-paths-helper" "github.com/codeclysm/extract/v4" @@ -108,7 +109,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo defer download.Close() // Download the zip - temp, err := paths.MkTempDir("", "flasher-updater-") + temp, err := GetTempDir("download-") if err != nil { return nil, "", fmt.Errorf("could not create temporary download directory: %w", err) } @@ -159,3 +160,21 @@ func ExtractImage(archive, temp *paths.Path) error { } return nil } + +// GetTempDir returns a temporary directory inside the user's cache directory. +// The caller is responsible for removing the directory when no longer needed. +func GetTempDir(prefix string) (*paths.Path, error) { + userCacheDir, err := os.UserCacheDir() + if err != nil { + return nil, fmt.Errorf("could not get user's cache directory: %w", err) + } + + cacheDir := paths.New(userCacheDir, "arduino-flasher-cli") + _ = cacheDir.MkdirAll() + + temp, err := paths.MkTempDir(cacheDir.String(), prefix) + if err != nil { + return nil, fmt.Errorf("could not create .cache directory: %w", err) + } + return temp, nil +} diff --git a/updater/flasher.go b/updater/flasher.go index 98284e9..f8edac2 100644 --- a/updater/flasher.go +++ b/updater/flasher.go @@ -58,7 +58,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes imagePath = tempImagePath } else if !imagePath.IsDir() { - temp, err := paths.MkTempDir("", "debian-image-") + temp, err := GetTempDir("extract-") if err != nil { return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err) }