Skip to content

Commit 888afe5

Browse files
authored
fix: move temp into cache (#5)
* fix: download stuff into cached dir * fixup! fix: download stuff into cached dir
1 parent 3a82156 commit 888afe5

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

updater/download_image.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -108,7 +109,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
108109
defer download.Close()
109110

110111
// Download the zip
111-
temp, err := paths.MkTempDir("", "flasher-updater-")
112+
temp, err := GetTempDir("download-")
112113
if err != nil {
113114
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
114115
}
@@ -159,3 +160,21 @@ func ExtractImage(archive, temp *paths.Path) error {
159160
}
160161
return nil
161162
}
163+
164+
// GetTempDir returns a temporary directory inside the user's cache directory.
165+
// The caller is responsible for removing the directory when no longer needed.
166+
func GetTempDir(prefix string) (*paths.Path, error) {
167+
userCacheDir, err := os.UserCacheDir()
168+
if err != nil {
169+
return nil, fmt.Errorf("could not get user's cache directory: %w", err)
170+
}
171+
172+
cacheDir := paths.New(userCacheDir, "arduino-flasher-cli")
173+
_ = cacheDir.MkdirAll()
174+
175+
temp, err := paths.MkTempDir(cacheDir.String(), prefix)
176+
if err != nil {
177+
return nil, fmt.Errorf("could not create .cache directory: %w", err)
178+
}
179+
return temp, nil
180+
}

updater/flasher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
5858

5959
imagePath = tempImagePath
6060
} else if !imagePath.IsDir() {
61-
temp, err := paths.MkTempDir("", "debian-image-")
61+
temp, err := GetTempDir("extract-")
6262
if err != nil {
6363
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
6464
}

0 commit comments

Comments
 (0)