Skip to content

Commit e930b80

Browse files
Refactor the flow of the commands to make them more readable
1 parent 9ddd61a commit e930b80

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func runDownloadCommand(args []string, destDir string) {
5151
}
5252

5353
client := updater.NewClient()
54-
_, _, err := updater.DownloadImage(client, targetVersion, nil, true, "", downloadPath)
54+
_, _, err := updater.DownloadImage(client, targetVersion, nil, true, downloadPath)
5555
if err != nil {
5656
feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument)
5757
}

updater/download_image.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ import (
2727
"github.com/arduino/go-paths-helper"
2828
"github.com/codeclysm/extract/v4"
2929
"github.com/schollz/progressbar/v3"
30-
"github.com/shirou/gopsutil/v4/disk"
3130

3231
"github.com/arduino/arduino-flasher-cli/feedback"
3332
"github.com/arduino/arduino-flasher-cli/i18n"
3433
)
3534

36-
const GiB = uint64(1024 * 1024 * 1024)
37-
const NeededDiskSpace = uint64(15)
38-
3935
type Manifest struct {
4036
Latest Release `json:"latest"`
4137
Releases []Release `json:"releases"`
@@ -50,8 +46,8 @@ type Release struct {
5046
// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
5147
type DownloadConfirmCB func(target string) (bool, error)
5248

53-
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, tempDir string) (*paths.Path, string, error) {
54-
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, tempDir, nil)
49+
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, temp *paths.Path) (*paths.Path, string, error) {
50+
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, temp)
5551
if err != nil {
5652
return nil, "", fmt.Errorf("error downloading the image: %v", err)
5753
}
@@ -73,7 +69,7 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
7369
return imagePath, version, nil
7470
}
7571

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

7975
feedback.Print(i18n.Tr("Checking for Debian image releases"))
@@ -115,14 +111,6 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
115111
}
116112
defer download.Close()
117113

118-
// Download the zip
119-
if downloadPath == nil {
120-
downloadPath, err = SetTempDir("download-", tempDir)
121-
if err != nil {
122-
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
123-
}
124-
}
125-
126114
tmpZip := downloadPath.Join("arduino-unoq-debian-image-" + rel.Version + ".tar.zst")
127115
tmpZipFile, err := tmpZip.Create()
128116
if err != nil {
@@ -191,14 +179,5 @@ func SetTempDir(prefix string, tempDir string) (*paths.Path, error) {
191179
return nil, fmt.Errorf("could not create .cache directory: %w", err)
192180
}
193181

194-
// Check if there is enough free disk space before downloading/extracting an image
195-
d, err := disk.Usage(temp.String())
196-
if err != nil {
197-
return nil, err
198-
}
199-
if d.Free/GiB < NeededDiskSpace {
200-
return nil, fmt.Errorf("aborting: download and extraction requires up to %d GiB of free space", NeededDiskSpace)
201-
}
202-
203182
return temp, nil
204183
}

updater/flasher.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,36 @@ import (
2222
"strings"
2323

2424
"github.com/arduino/go-paths-helper"
25+
"github.com/shirou/gopsutil/v4/disk"
2526

2627
"github.com/arduino/arduino-flasher-cli/feedback"
2728
"github.com/arduino/arduino-flasher-cli/i18n"
2829
"github.com/arduino/arduino-flasher-cli/updater/artifacts"
2930
)
3031

32+
const GiB = uint64(1024 * 1024 * 1024)
33+
const DownloadDiskSpace = uint64(12)
34+
const ExtractDiskSpace = uint64(10)
35+
3136
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool, tempDir string) error {
3237
if !imagePath.Exist() {
3338
client := NewClient()
3439

40+
temp, err := SetTempDir("download-", tempDir)
41+
if err != nil {
42+
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
43+
}
44+
defer func() { _ = temp.RemoveAll() }()
45+
46+
// Check if there is enough free disk space before downloading and extracting an image
47+
d, err := disk.Usage(temp.String())
48+
if err != nil {
49+
return err
50+
}
51+
if d.Free/GiB < DownloadDiskSpace {
52+
return fmt.Errorf("download and extraction requires up to %d GiB of free space", DownloadDiskSpace)
53+
}
54+
3555
tempImagePath, v, err := DownloadAndExtract(client, version, func(target string) (bool, error) {
3656
feedback.Printf("Found Debian image version: %s", target)
3757
feedback.Printf("Do you want to download it? (yes/no)")
@@ -43,7 +63,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
4363
}
4464
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
4565
return yes, nil
46-
}, forceYes, tempDir)
66+
}, forceYes, temp)
4767

4868
if err != nil {
4969
return fmt.Errorf("could not download and extract the image: %v", err)
@@ -65,6 +85,15 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
6585
}
6686
defer func() { _ = temp.RemoveAll() }()
6787

88+
// Check if there is enough free disk space before extracting an image
89+
d, err := disk.Usage(temp.String())
90+
if err != nil {
91+
return err
92+
}
93+
if d.Free/GiB < ExtractDiskSpace {
94+
return fmt.Errorf("extraction requires up to %d GiB of free space", ExtractDiskSpace)
95+
}
96+
6897
err = ExtractImage(imagePath, temp)
6998
if err != nil {
7099
return fmt.Errorf("error extracting the archive: %v", err)

0 commit comments

Comments
 (0)