Skip to content

Commit 4584531

Browse files
committed
Merge remote-tracking branch 'origin/main' into add-ci-and-scripts
2 parents 5212619 + bf03441 commit 4584531

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ jobs:
6868

6969
- name: Prepare Build Artifacts (!windows)
7070
working-directory: ./${{ env.DIST_DIR }}
71-
run: tar -czf ${{ env.RELEASE_NAME }}.tar.gz arduino-flasher-cli -C ../ LICENSE
71+
run: tar -czf ${{ env.RELEASE_NAME }}.tar.gz arduino-flasher-cli -C ../ LICENSE README.md
7272
if: matrix.os != 'windows'
7373

7474
- name: Prepare Build Artifacts (windows)
7575
working-directory: ./${{ env.DIST_DIR }}
76-
run: 7z a -tzip ${{ env.RELEASE_NAME }}.zip arduino-flasher-cli.exe ../LICENSE
76+
run: 7z a -tzip ${{ env.RELEASE_NAME }}.zip arduino-flasher-cli.exe ../LICENSE ../README.md
7777
if: matrix.os == 'windows'
7878

7979
- name: Upload artifacts
@@ -125,7 +125,7 @@ jobs:
125125
126126
- name: Prepare Build Artifacts
127127
run: |
128-
${{ env.SEVENZ_PATH }} a -tzip ${{ env.RELEASE_NAME }}.zip arduino-flasher-cli.exe LICENSE
128+
${{ env.SEVENZ_PATH }} a -tzip ${{ env.RELEASE_NAME }}.zip arduino-flasher-cli.exe LICENSE README.md
129129
130130
- name: Upload artifacts
131131
uses: actions/upload-artifact@v4
@@ -137,7 +137,7 @@ jobs:
137137

138138
# This step is needed because the self hosted runner does not delete files automatically
139139
- name: Cleanup
140-
run: rm ${{ env.RELEASE_NAME }}.zip LICENSE arduino-flasher-cli.exe
140+
run: rm ${{ env.RELEASE_NAME }}.zip LICENSE README.md arduino-flasher-cli.exe
141141

142142
notarize-macos:
143143
name: Notarize macOS
@@ -253,7 +253,7 @@ jobs:
253253
+x \
254254
"${{ env.PROJECT_NAME }}"
255255
256-
tar -czf ${{ env.PACKAGE_FILENAME }} ${{ env.PROJECT_NAME }} LICENSE
256+
tar -czf ${{ env.PACKAGE_FILENAME }} ${{ env.PROJECT_NAME }} LICENSE README.md
257257
258258
- name: Replace artifact with notarized build
259259
uses: actions/upload-artifact@v4

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
A tool to download and flash Debian images on the board.
44

5+
## Docs
6+
7+
For a full guide on how to use it, see the [User documentation](https://docs.arduino.cc/tutorials/uno-q/update-image/).
8+
59
## Build and test it locally
610

7-
Build it with `task arduino-flasher-cli:build` and run:
11+
Build it with `task build` and run:
812

913
```sh
1014
# Flash the latest release of the Debian image

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"log/slog"
22+
"os"
2223

2324
"github.com/spf13/cobra"
2425
"go.bug.st/cleanup"
@@ -34,7 +35,9 @@ var format string
3435
func main() {
3536
rootCmd := &cobra.Command{
3637
Use: "arduino-flasher-cli",
37-
Short: "A CLI to update and flash the Debian image",
38+
Short: "A CLI to update your Arduino UNO Q board, by downloading and flashing the latest Arduino Linux image",
39+
Example: " " + os.Args[0] + " flash latest\n" +
40+
" " + os.Args[0] + " list\n",
3841
PersistentPreRun: func(cmd *cobra.Command, args []string) {
3942
format, ok := feedback.ParseOutputFormat(format)
4043
if !ok {

updater/download_image.go

Lines changed: 28 additions & 6 deletions
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"
@@ -45,24 +46,27 @@ type Release struct {
4546
// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
4647
type DownloadConfirmCB func(target string) (bool, error)
4748

48-
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, error) {
49+
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
4950
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes)
5051
if err != nil {
51-
return nil, fmt.Errorf("error downloading the image: %v", err)
52+
return nil, "", fmt.Errorf("error downloading the image: %v", err)
5253
}
5354

5455
// Download not confirmed
5556
if tmpZip == nil {
56-
return nil, nil
57+
return nil, "", nil
5758
}
5859

5960
err = ExtractImage(tmpZip, tmpZip.Parent())
6061
if err != nil {
61-
return nil, fmt.Errorf("error extracting the image: %v", err)
62+
return nil, "", fmt.Errorf("error extracting the image: %v", err)
6263
}
6364

6465
imagePath := tmpZip.Parent().Join("arduino-unoq-debian-image-" + version)
65-
return imagePath, nil
66+
if targetVersion == "latest" {
67+
version += "(latest)"
68+
}
69+
return imagePath, version, nil
6670
}
6771

6872
func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
@@ -108,7 +112,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
108112
defer download.Close()
109113

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

updater/flasher.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
3232
if !imagePath.Exist() {
3333
client := NewClient()
3434

35-
tempImagePath, err := DownloadAndExtract(client, version, func(target string) (bool, error) {
35+
tempImagePath, v, err := DownloadAndExtract(client, version, func(target string) (bool, error) {
3636
feedback.Printf("Found Debian image version: %s", target)
3737
feedback.Printf("Do you want to download it? (yes/no)")
3838

@@ -56,9 +56,10 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
5656

5757
defer func() { _ = tempImagePath.Parent().RemoveAll() }()
5858

59+
version = v
5960
imagePath = tempImagePath
6061
} else if !imagePath.IsDir() {
61-
temp, err := paths.MkTempDir("", "debian-image-")
62+
temp, err := GetTempDir("extract-")
6263
if err != nil {
6364
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
6465
}
@@ -77,7 +78,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
7778
imagePath = tempContent[0]
7879
}
7980

80-
return FlashBoard(ctx, imagePath.String(), func(target string) (bool, error) {
81+
return FlashBoard(ctx, imagePath.String(), version, func(target string) (bool, error) {
8182
feedback.Print("\nWARNING: flashing a new Linux image on the board will erase any existing data you have on it.")
8283
feedback.Printf("Do you want to proceed and flash %s on the board? (yes/no)", target)
8384

@@ -91,9 +92,9 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
9192
}, forceYes)
9293
}
9394

94-
func FlashBoard(ctx context.Context, downloadedImagePath string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) error {
95+
func FlashBoard(ctx context.Context, downloadedImagePath string, version string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) error {
9596
if !forceYes {
96-
res, err := upgradeConfirmCb(downloadedImagePath)
97+
res, err := upgradeConfirmCb(version)
9798
if err != nil {
9899
return err
99100
}
@@ -152,5 +153,7 @@ func FlashBoard(ctx context.Context, downloadedImagePath string, upgradeConfirmC
152153
return err
153154
}
154155

156+
feedback.Print("\nThe board has been successfully flashed. You can now power-cycle the board (unplug and re-plug). Remember to remove the jumper.")
157+
155158
return nil
156159
}

0 commit comments

Comments
 (0)