Skip to content

Commit 43d3a92

Browse files
Flasher: fix inconsistent checksum and delete temporary directories at each run (#705)
1 parent 709d5c2 commit 43d3a92

File tree

3 files changed

+77
-77
lines changed

3 files changed

+77
-77
lines changed

arduino-flasher-cli/flash.go

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package main
33
import (
44
"arduino-flasher/updater"
55
"context"
6-
"fmt"
7-
"net/url"
86
"os"
97
"runtime"
10-
"strings"
118

129
"github.com/arduino/go-paths-helper"
1310
runas "github.com/arduino/go-windows-runas"
@@ -53,76 +50,8 @@ func runFlashCommand(ctx context.Context, args []string, forceYes bool) {
5350
feedback.Fatal(i18n.Tr("could not find image absolute path: %v", err), feedback.ErrBadArgument)
5451
}
5552

56-
if !imagePath.Exist() {
57-
version := args[0]
58-
59-
updateURL := os.Getenv("UPDATE_URL")
60-
if updateURL == "" {
61-
// TODO: change to prod
62-
updateURL = "https://downloads.oniudra.cc"
63-
}
64-
65-
parsedURL, err := url.Parse(updateURL)
66-
if err != nil {
67-
feedback.Fatal(i18n.Tr("invalid UPDATE_URL:", err), feedback.ErrBadArgument)
68-
}
69-
70-
headers := map[string]string{}
71-
clientID := os.Getenv("CF_ACCESS_CLIENT_ID")
72-
clientSecret := os.Getenv("CF_ACCESS_CLIENT_SECRET")
73-
if clientID != "" && clientSecret != "" {
74-
headers["CF-Access-Client-Id"] = clientID
75-
headers["CF-Access-Client-Secret"] = clientSecret
76-
}
77-
78-
var client *updater.Client
79-
if len(headers) == 2 {
80-
client = updater.NewClient(parsedURL, "debian-im/Stable", updater.WithHeaders(headers))
81-
} else {
82-
client = updater.NewClient(parsedURL, "debian-im/Stable")
83-
}
84-
85-
tempImagePath, err := updater.DownloadAndExtract(client, version, func(target string) (bool, error) {
86-
feedback.Printf("Found Debian image version: %s", target)
87-
feedback.Printf("Do you want to download it and flash it on the board? (yes/no)")
88-
89-
var yesInput string
90-
_, err := fmt.Scanf("%s\n", &yesInput)
91-
if err != nil {
92-
return false, err
93-
}
94-
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
95-
return yes, nil
96-
}, forceYes)
97-
98-
if err != nil {
99-
feedback.Fatal(i18n.Tr("could not download and extract the image: %v", err), feedback.ErrBadArgument)
100-
}
101-
102-
defer tempImagePath.Parent().RemoveAll()
103-
104-
imagePath = tempImagePath
105-
} else if !imagePath.IsDir() {
106-
temp, err := paths.MkTempDir("", "debian-image-")
107-
if err != nil {
108-
feedback.Fatal(i18n.Tr("error creating a temporary directory to extract the archive: %v", err), feedback.ErrBadArgument)
109-
}
110-
defer temp.RemoveAll()
111-
112-
err = updater.ExtractImage(imagePath, temp)
113-
if err != nil {
114-
feedback.Fatal(i18n.Tr("error extracting the archive: %v", err), feedback.ErrBadArgument)
115-
}
116-
117-
tempContent, err := temp.ReadDir(paths.AndFilter(paths.FilterDirectories(), paths.FilterPrefixes("arduino-unoq-debian-image-")))
118-
if err != nil {
119-
feedback.Fatal(i18n.Tr("could not find Debian image directory: %v", err), feedback.ErrBadArgument)
120-
}
121-
122-
imagePath = tempContent[0]
123-
}
124-
125-
if err := updater.FlashBoard(ctx, imagePath.String()); err != nil {
53+
err = updater.Flash(ctx, imagePath, args[0], forceYes)
54+
if err != nil {
12655
feedback.Fatal(i18n.Tr("error flashing the board: %v", err), feedback.ErrBadArgument)
12756
}
12857
}

arduino-flasher-cli/updater/download_image.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@ type PassThru struct {
4343
// use it to keep track of the progress and then forward the call.
4444
func (pt *PassThru) Read(p []byte) (int, error) {
4545
n, err := pt.Reader.Read(p)
46-
if err != nil {
47-
return 0, err
48-
}
4946
pt.total += float64(n)
5047
percentage := pt.total / float64(pt.length) * float64(100)
5148
if percentage-pt.progress > 1 {
5249
pt.progressCB(percentage)
5350
pt.progress = percentage
5451
}
5552

56-
return n, nil
53+
return n, err
5754
}
5855

5956
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, error) {

arduino-flasher-cli/updater/flasher.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"context"
55
"fmt"
66
"log/slog"
7+
"net/url"
8+
"os"
79
"runtime"
10+
"strings"
811

912
"embed"
1013

@@ -15,6 +18,77 @@ import (
1518
//go:embed assets
1619
var assets embed.FS
1720

21+
func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool) error {
22+
if !imagePath.Exist() {
23+
updateURL := os.Getenv("UPDATE_URL")
24+
if updateURL == "" {
25+
// TODO: change to prod
26+
updateURL = "https://downloads.oniudra.cc"
27+
}
28+
29+
parsedURL, err := url.Parse(updateURL)
30+
if err != nil {
31+
return fmt.Errorf("invalid UPDATE_URL: %v", err)
32+
}
33+
34+
headers := map[string]string{}
35+
clientID := os.Getenv("CF_ACCESS_CLIENT_ID")
36+
clientSecret := os.Getenv("CF_ACCESS_CLIENT_SECRET")
37+
if clientID != "" && clientSecret != "" {
38+
headers["CF-Access-Client-Id"] = clientID
39+
headers["CF-Access-Client-Secret"] = clientSecret
40+
}
41+
42+
var client *Client
43+
if len(headers) == 2 {
44+
client = NewClient(parsedURL, "debian-im/Stable", WithHeaders(headers))
45+
} else {
46+
client = NewClient(parsedURL, "debian-im/Stable")
47+
}
48+
49+
tempImagePath, err := DownloadAndExtract(client, version, func(target string) (bool, error) {
50+
feedback.Printf("Found Debian image version: %s", target)
51+
feedback.Printf("Do you want to download it and flash it on the board? (yes/no)")
52+
53+
var yesInput string
54+
_, err := fmt.Scanf("%s\n", &yesInput)
55+
if err != nil {
56+
return false, err
57+
}
58+
yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
59+
return yes, nil
60+
}, forceYes)
61+
62+
if err != nil {
63+
return fmt.Errorf("could not download and extract the image: %v", err)
64+
}
65+
66+
defer tempImagePath.Parent().RemoveAll()
67+
68+
imagePath = tempImagePath
69+
} else if !imagePath.IsDir() {
70+
temp, err := paths.MkTempDir("", "debian-image-")
71+
if err != nil {
72+
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
73+
}
74+
defer temp.RemoveAll()
75+
76+
err = ExtractImage(imagePath, temp)
77+
if err != nil {
78+
return fmt.Errorf("error extracting the archive: %v", err)
79+
}
80+
81+
tempContent, err := temp.ReadDir(paths.AndFilter(paths.FilterDirectories(), paths.FilterPrefixes("arduino-unoq-debian-image-")))
82+
if err != nil {
83+
return fmt.Errorf("could not find Debian image directory: %v", err)
84+
}
85+
86+
imagePath = tempContent[0]
87+
}
88+
89+
return FlashBoard(ctx, imagePath.String())
90+
}
91+
1892
func FlashBoard(ctx context.Context, downloadedImagePath string) error {
1993
qdl, err := getQdlBytes()
2094
if err != nil {

0 commit comments

Comments
 (0)