Skip to content

Commit c307f49

Browse files
WIP: move logic closer to the cobra command
1 parent e939816 commit c307f49

File tree

2 files changed

+77
-84
lines changed

2 files changed

+77
-84
lines changed

flash.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@ package main
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"os"
2122
"runtime"
23+
"strings"
2224

2325
"github.com/arduino/go-paths-helper"
2426
runas "github.com/arduino/go-windows-runas"
27+
"github.com/shirou/gopsutil/v4/disk"
2528
"github.com/spf13/cobra"
2629

2730
"github.com/arduino/arduino-flasher-cli/feedback"
2831
"github.com/arduino/arduino-flasher-cli/i18n"
2932
"github.com/arduino/arduino-flasher-cli/updater"
3033
)
3134

35+
const GiB = uint64(1024 * 1024 * 1024)
36+
const DownloadDiskSpace = uint64(12)
37+
const ExtractDiskSpace = uint64(10)
38+
3239
func newFlashCmd() *cobra.Command {
3340
var forceYes bool
3441
var tempDir string
@@ -90,12 +97,81 @@ func checkDriversInstalled() {
9097
}
9198

9299
func runFlashCommand(ctx context.Context, args []string, forceYes bool, tempDir string) {
100+
version := args[0]
93101
imagePath, err := paths.New(args[0]).Abs()
94102
if err != nil {
95103
feedback.Fatal(i18n.Tr("could not find image absolute path: %v", err), feedback.ErrBadArgument)
96104
}
97105

98-
err = updater.Flash(ctx, imagePath, args[0], forceYes, tempDir)
106+
if !imagePath.Exist() {
107+
client := updater.NewClient()
108+
109+
temp, err := updater.SetTempDir("download-", tempDir)
110+
if err != nil {
111+
feedback.Fatal(i18n.Tr("error creating a temporary directory to extract the archive: %v", err), feedback.ErrBadArgument)
112+
}
113+
defer func() { _ = temp.RemoveAll() }()
114+
115+
// Check if there is enough free disk space before downloading and extracting an image
116+
d, err := disk.Usage(temp.String())
117+
if err != nil {
118+
feedback.Fatal(err.Error(), feedback.ErrBadArgument)
119+
}
120+
if d.Free/GiB < DownloadDiskSpace {
121+
feedback.Fatal(i18n.Tr("download and extraction requires up to %d GiB of free space", DownloadDiskSpace), feedback.ErrBadArgument)
122+
}
123+
124+
tempImagePath, v, err := updater.DownloadAndExtract(ctx, client, version, temp)
125+
126+
if err != nil {
127+
feedback.Fatal(i18n.Tr("could not download and extract the image: %v", err), feedback.ErrBadArgument)
128+
}
129+
130+
defer func() { _ = tempImagePath.Parent().RemoveAll() }()
131+
132+
version = v
133+
imagePath = tempImagePath
134+
} else if !imagePath.IsDir() {
135+
temp, err := updater.SetTempDir("extract-", tempDir)
136+
if err != nil {
137+
feedback.Fatal(i18n.Tr("error creating a temporary directory to extract the archive: %v", err), feedback.ErrBadArgument)
138+
}
139+
defer func() { _ = temp.RemoveAll() }()
140+
141+
// Check if there is enough free disk space before extracting an image
142+
d, err := disk.Usage(temp.String())
143+
if err != nil {
144+
feedback.Fatal(err.Error(), feedback.ErrBadArgument)
145+
}
146+
if d.Free/GiB < ExtractDiskSpace {
147+
feedback.Fatal(i18n.Tr("extraction requires up to %d GiB of free space", ExtractDiskSpace), feedback.ErrBadArgument)
148+
}
149+
150+
err = updater.ExtractImage(ctx, imagePath, temp)
151+
if err != nil {
152+
feedback.Fatal(i18n.Tr("error extracting the archive: %v", err), feedback.ErrBadArgument)
153+
}
154+
155+
tempContent, err := temp.ReadDir(paths.AndFilter(paths.FilterDirectories(), paths.FilterPrefixes("arduino-unoq-debian-image-")))
156+
if err != nil {
157+
feedback.Fatal(i18n.Tr("could not find Debian image directory: %v", err), feedback.ErrBadArgument)
158+
}
159+
160+
imagePath = tempContent[0]
161+
}
162+
163+
err = updater.FlashBoard(ctx, imagePath.String(), version, func(target string) (bool, error) {
164+
feedback.Print("\nWARNING: flashing a new Linux image on the board will erase any existing data you have on it.")
165+
feedback.Printf("Do you want to proceed and flash %s on the board? (yes/no)", target)
166+
167+
var yesInput string
168+
_, err := fmt.Scanf("%s\n", &yesInput)
169+
if err != nil {
170+
return false, err
171+
}
172+
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
173+
return yes, nil
174+
}, forceYes)
99175
if err != nil {
100176
feedback.Fatal(i18n.Tr("error flashing the board: %v", err), feedback.ErrBadArgument)
101177
}

updater/flasher.go

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,97 +19,14 @@ import (
1919
"context"
2020
"fmt"
2121
"runtime"
22-
"strings"
2322

2423
"github.com/arduino/go-paths-helper"
25-
"github.com/shirou/gopsutil/v4/disk"
2624

2725
"github.com/arduino/arduino-flasher-cli/feedback"
2826
"github.com/arduino/arduino-flasher-cli/i18n"
2927
"github.com/arduino/arduino-flasher-cli/updater/artifacts"
3028
)
3129

32-
const GiB = uint64(1024 * 1024 * 1024)
33-
const DownloadDiskSpace = uint64(12)
34-
const ExtractDiskSpace = uint64(10)
35-
36-
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool, tempDir string) error {
37-
if !imagePath.Exist() {
38-
client := NewClient()
39-
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-
55-
tempImagePath, v, err := DownloadAndExtract(ctx, client, version, temp)
56-
57-
if err != nil {
58-
return fmt.Errorf("could not download and extract the image: %v", err)
59-
}
60-
61-
// Download not confirmed
62-
if tempImagePath == nil {
63-
return nil
64-
}
65-
66-
defer func() { _ = tempImagePath.Parent().RemoveAll() }()
67-
68-
version = v
69-
imagePath = tempImagePath
70-
} else if !imagePath.IsDir() {
71-
temp, err := SetTempDir("extract-", tempDir)
72-
if err != nil {
73-
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
74-
}
75-
defer func() { _ = temp.RemoveAll() }()
76-
77-
// Check if there is enough free disk space before extracting an image
78-
d, err := disk.Usage(temp.String())
79-
if err != nil {
80-
return err
81-
}
82-
if d.Free/GiB < ExtractDiskSpace {
83-
return fmt.Errorf("extraction requires up to %d GiB of free space", ExtractDiskSpace)
84-
}
85-
86-
err = ExtractImage(ctx, imagePath, temp)
87-
if err != nil {
88-
return fmt.Errorf("error extracting the archive: %v", err)
89-
}
90-
91-
tempContent, err := temp.ReadDir(paths.AndFilter(paths.FilterDirectories(), paths.FilterPrefixes("arduino-unoq-debian-image-")))
92-
if err != nil {
93-
return fmt.Errorf("could not find Debian image directory: %v", err)
94-
}
95-
96-
imagePath = tempContent[0]
97-
}
98-
99-
return FlashBoard(ctx, imagePath.String(), version, func(target string) (bool, error) {
100-
feedback.Print("\nWARNING: flashing a new Linux image on the board will erase any existing data you have on it.")
101-
feedback.Printf("Do you want to proceed and flash %s on the board? (yes/no)", target)
102-
103-
var yesInput string
104-
_, err := fmt.Scanf("%s\n", &yesInput)
105-
if err != nil {
106-
return false, err
107-
}
108-
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
109-
return yes, nil
110-
}, forceYes)
111-
}
112-
11330
func FlashBoard(ctx context.Context, downloadedImagePath string, version string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) error {
11431
if !forceYes {
11532
res, err := upgradeConfirmCb(version)

0 commit comments

Comments
 (0)