From e939816eccfcd3c42ec71dd6840a0ba0d12b034e Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Wed, 19 Nov 2025 09:14:53 +0100 Subject: [PATCH 1/5] Remove callback function and download archives automatically --- download.go | 2 +- updater/download_image.go | 17 +++-------------- updater/flasher.go | 13 +------------ 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/download.go b/download.go index e4e111e..46bab2d 100644 --- a/download.go +++ b/download.go @@ -53,7 +53,7 @@ func runDownloadCommand(ctx context.Context, args []string, destDir string) { } client := updater.NewClient() - downloadPath, _, err := updater.DownloadImage(ctx, client, targetVersion, nil, true, downloadPath) + downloadPath, _, err := updater.DownloadImage(ctx, client, targetVersion, downloadPath) if err != nil { feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument) } diff --git a/updater/download_image.go b/updater/download_image.go index 06db7fe..976dfb9 100644 --- a/updater/download_image.go +++ b/updater/download_image.go @@ -46,8 +46,8 @@ type Release struct { // DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded. type DownloadConfirmCB func(target string) (bool, error) -func DownloadAndExtract(ctx context.Context, client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, temp *paths.Path) (*paths.Path, string, error) { - tmpZip, version, err := DownloadImage(ctx, client, targetVersion, upgradeConfirmCb, forceYes, temp) +func DownloadAndExtract(ctx context.Context, client *Client, targetVersion string, temp *paths.Path) (*paths.Path, string, error) { + tmpZip, version, err := DownloadImage(ctx, client, targetVersion, temp) if err != nil { return nil, "", fmt.Errorf("error downloading the image: %v", err) } @@ -69,7 +69,7 @@ func DownloadAndExtract(ctx context.Context, client *Client, targetVersion strin return imagePath, version, nil } -func DownloadImage(ctx context.Context, client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) { +func DownloadImage(ctx context.Context, client *Client, targetVersion string, downloadPath *paths.Path) (*paths.Path, string, error) { var err error feedback.Print(i18n.Tr("Checking for Debian image releases")) @@ -94,17 +94,6 @@ func DownloadImage(ctx context.Context, client *Client, targetVersion string, up return nil, "", fmt.Errorf("could not find Debian image %s", targetVersion) } - if !forceYes { - res, err := upgradeConfirmCb(rel.Version) - if err != nil { - return nil, "", err - } - if !res { - feedback.Print(i18n.Tr("Download not confirmed by user, exiting")) - return nil, "", nil - } - } - download, size, err := client.FetchZip(ctx, rel.Url) if err != nil { return nil, "", fmt.Errorf("could not fetch Debian image: %w", err) diff --git a/updater/flasher.go b/updater/flasher.go index d0104ce..73f12ca 100644 --- a/updater/flasher.go +++ b/updater/flasher.go @@ -52,18 +52,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes return fmt.Errorf("download and extraction requires up to %d GiB of free space", DownloadDiskSpace) } - tempImagePath, v, err := DownloadAndExtract(ctx, client, version, func(target string) (bool, error) { - feedback.Printf("Found Debian image version: %s", target) - feedback.Printf("Do you want to download it? (yes/no)") - - var yesInput string - _, err := fmt.Scanf("%s\n", &yesInput) - if err != nil { - return false, err - } - yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y" - return yes, nil - }, forceYes, temp) + tempImagePath, v, err := DownloadAndExtract(ctx, client, version, temp) if err != nil { return fmt.Errorf("could not download and extract the image: %v", err) From 377e91b23471f8411c351bc85f7fbb160c8135ea Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Wed, 19 Nov 2025 15:06:56 +0100 Subject: [PATCH 2/5] Create client inside DownloadImage function --- download.go | 3 +-- updater/download_image.go | 13 ++++--------- updater/flasher.go | 11 +---------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/download.go b/download.go index 46bab2d..00e7a3b 100644 --- a/download.go +++ b/download.go @@ -52,8 +52,7 @@ func runDownloadCommand(ctx context.Context, args []string, destDir string) { feedback.Fatal(i18n.Tr("error: %s is not a directory. Please, select an existing directory.", destDir), feedback.ErrBadArgument) } - client := updater.NewClient() - downloadPath, _, err := updater.DownloadImage(ctx, client, targetVersion, downloadPath) + downloadPath, _, err := updater.DownloadImage(ctx, targetVersion, downloadPath) if err != nil { feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument) } diff --git a/updater/download_image.go b/updater/download_image.go index 976dfb9..9967009 100644 --- a/updater/download_image.go +++ b/updater/download_image.go @@ -46,17 +46,12 @@ type Release struct { // DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded. type DownloadConfirmCB func(target string) (bool, error) -func DownloadAndExtract(ctx context.Context, client *Client, targetVersion string, temp *paths.Path) (*paths.Path, string, error) { - tmpZip, version, err := DownloadImage(ctx, client, targetVersion, temp) +func DownloadAndExtract(ctx context.Context, targetVersion string, temp *paths.Path) (*paths.Path, string, error) { + tmpZip, version, err := DownloadImage(ctx, targetVersion, temp) if err != nil { return nil, "", fmt.Errorf("error downloading the image: %v", err) } - // Download not confirmed - if tmpZip == nil { - return nil, "", nil - } - err = ExtractImage(ctx, tmpZip, tmpZip.Parent()) if err != nil { return nil, "", fmt.Errorf("error extracting the image: %v", err) @@ -69,10 +64,10 @@ func DownloadAndExtract(ctx context.Context, client *Client, targetVersion strin return imagePath, version, nil } -func DownloadImage(ctx context.Context, client *Client, targetVersion string, downloadPath *paths.Path) (*paths.Path, string, error) { +func DownloadImage(ctx context.Context, targetVersion string, downloadPath *paths.Path) (*paths.Path, string, error) { var err error - feedback.Print(i18n.Tr("Checking for Debian image releases")) + client := NewClient() manifest, err := client.GetInfoManifest(ctx) if err != nil { return nil, "", err diff --git a/updater/flasher.go b/updater/flasher.go index 73f12ca..c821321 100644 --- a/updater/flasher.go +++ b/updater/flasher.go @@ -35,8 +35,6 @@ const ExtractDiskSpace = uint64(10) func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes bool, tempDir string) error { if !imagePath.Exist() { - client := NewClient() - temp, err := SetTempDir("download-", tempDir) if err != nil { return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err) @@ -52,19 +50,12 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes return fmt.Errorf("download and extraction requires up to %d GiB of free space", DownloadDiskSpace) } - tempImagePath, v, err := DownloadAndExtract(ctx, client, version, temp) + tempImagePath, v, err := DownloadAndExtract(ctx, version, temp) if err != nil { return fmt.Errorf("could not download and extract the image: %v", err) } - // Download not confirmed - if tempImagePath == nil { - return nil - } - - defer func() { _ = tempImagePath.Parent().RemoveAll() }() - version = v imagePath = tempImagePath } else if !imagePath.IsDir() { From 236ab8ba0cfab2099635a5d6482a911220f998ec Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 25 Nov 2025 15:53:38 +0100 Subject: [PATCH 3/5] Move interaction with user closer to cobra function --- flash.go | 19 +++++++++++++++++++ updater/flasher.go | 29 ++--------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/flash.go b/flash.go index 77c6b03..1616dd1 100644 --- a/flash.go +++ b/flash.go @@ -17,8 +17,10 @@ package main import ( "context" + "fmt" "os" "runtime" + "strings" "github.com/arduino/go-paths-helper" runas "github.com/arduino/go-windows-runas" @@ -95,8 +97,25 @@ func runFlashCommand(ctx context.Context, args []string, forceYes bool, tempDir feedback.Fatal(i18n.Tr("could not find image absolute path: %v", err), feedback.ErrBadArgument) } + if !forceYes { + feedback.Print("\nWARNING: flashing a new Linux image on the board will erase any existing data you have on it.") + feedback.Printf("Do you want to proceed and flash %s on the board? (yes/no)", args[0]) + + var yesInput string + _, err := fmt.Scanf("%s\n", &yesInput) + if err != nil { + feedback.Fatal(err.Error(), feedback.ErrBadArgument) + } + yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y" + + if !yes { + return + } + } + err = updater.Flash(ctx, imagePath, args[0], forceYes, tempDir) if err != nil { feedback.Fatal(i18n.Tr("error flashing the board: %v", err), feedback.ErrBadArgument) } + feedback.Print("\nThe board has been successfully flashed. You can now power-cycle the board (unplug and re-plug). Remember to remove the jumper.") } diff --git a/updater/flasher.go b/updater/flasher.go index c821321..9066245 100644 --- a/updater/flasher.go +++ b/updater/flasher.go @@ -19,7 +19,6 @@ import ( "context" "fmt" "runtime" - "strings" "github.com/arduino/go-paths-helper" "github.com/shirou/gopsutil/v4/disk" @@ -87,32 +86,10 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes imagePath = tempContent[0] } - return FlashBoard(ctx, imagePath.String(), version, func(target string) (bool, error) { - feedback.Print("\nWARNING: flashing a new Linux image on the board will erase any existing data you have on it.") - feedback.Printf("Do you want to proceed and flash %s on the board? (yes/no)", target) - - var yesInput string - _, err := fmt.Scanf("%s\n", &yesInput) - if err != nil { - return false, err - } - yes := strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y" - return yes, nil - }, forceYes) + return FlashBoard(ctx, imagePath.String(), version) } -func FlashBoard(ctx context.Context, downloadedImagePath string, version string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) error { - if !forceYes { - res, err := upgradeConfirmCb(version) - if err != nil { - return err - } - if !res { - feedback.Print(i18n.Tr("Flashing not confirmed by user, exiting")) - return nil - } - } - +func FlashBoard(ctx context.Context, downloadedImagePath string, version string) error { var flashDir *paths.Path for _, entry := range []string{"flash", "flash_UnoQ"} { if p := paths.New(downloadedImagePath, entry); p.Exist() { @@ -162,7 +139,5 @@ func FlashBoard(ctx context.Context, downloadedImagePath string, version string, return err } - feedback.Print("\nThe board has been successfully flashed. You can now power-cycle the board (unplug and re-plug). Remember to remove the jumper.") - return nil } From f85b5f807f520e4081a3c04579d544f6bf359817 Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 25 Nov 2025 16:44:01 +0100 Subject: [PATCH 4/5] Reorganize project structure --- Taskfile.yml | 16 ++--- .../arduino-flasher-cli/download/download.go | 10 +-- .../arduino-flasher-cli/drivers/drivers.go | 8 +-- .../drivers/drivers_others.go | 2 +- .../drivers/drivers_windows.go | 4 +- .../drivers/src}/dpinst-amd64.exe | Bin .../drivers/src}/dpinst-x86.exe | Bin .../arduino-flasher-cli/drivers/src}/unoq.cat | Bin .../arduino-flasher-cli/drivers/src}/unoq.inf | 0 .../arduino-flasher-cli/flash/flash.go | 10 +-- .../arduino-flasher-cli/list/list.go | 12 ++-- main.go => cmd/arduino-flasher-cli/main.go | 58 ++++----------- .../version/check_for_updates.go | 6 +- cmd/arduino-flasher-cli/version/version.go | 67 ++++++++++++++++++ {feedback => cmd/feedback}/errorcodes.go | 0 {feedback => cmd/feedback}/feedback.go | 2 +- {feedback => cmd/feedback}/feedback_test.go | 0 {feedback => cmd/feedback}/stdio.go | 2 +- {i18n => cmd/i18n}/i18n.go | 0 {i18n => cmd/i18n}/i18n_test.go | 0 .../tablestyle}/tablestyle.go | 0 .../updater}/artifacts/.gitignore | 0 .../artifacts/artifacts_darwin_amd64.go | 0 .../artifacts/artifacts_darwin_arm64.go | 0 .../artifacts/artifacts_linux_amd64.go | 0 .../artifacts/artifacts_linux_arm64.go | 0 .../artifacts/artifacts_windows_amd64.go | 0 .../updater}/artifacts/download_resources.sh | 0 .../updater}/download_image.go | 4 +- {updater => internal/updater}/flasher.go | 6 +- {updater => internal/updater}/http_client.go | 0 31 files changed, 121 insertions(+), 86 deletions(-) rename download.go => cmd/arduino-flasher-cli/download/download.go (90%) rename drivers.go => cmd/arduino-flasher-cli/drivers/drivers.go (86%) rename drivers_others.go => cmd/arduino-flasher-cli/drivers/drivers_others.go (98%) rename drivers_windows.go => cmd/arduino-flasher-cli/drivers/drivers_windows.go (98%) rename {drivers => cmd/arduino-flasher-cli/drivers/src}/dpinst-amd64.exe (100%) mode change 100755 => 100644 rename {drivers => cmd/arduino-flasher-cli/drivers/src}/dpinst-x86.exe (100%) mode change 100755 => 100644 rename {drivers => cmd/arduino-flasher-cli/drivers/src}/unoq.cat (100%) mode change 100755 => 100644 rename {drivers => cmd/arduino-flasher-cli/drivers/src}/unoq.inf (100%) mode change 100755 => 100644 rename flash.go => cmd/arduino-flasher-cli/flash/flash.go (95%) rename list.go => cmd/arduino-flasher-cli/list/list.go (87%) rename main.go => cmd/arduino-flasher-cli/main.go (58%) rename check_for_updates.go => cmd/arduino-flasher-cli/version/check_for_updates.go (93%) create mode 100644 cmd/arduino-flasher-cli/version/version.go rename {feedback => cmd/feedback}/errorcodes.go (100%) rename {feedback => cmd/feedback}/feedback.go (99%) rename {feedback => cmd/feedback}/feedback_test.go (100%) rename {feedback => cmd/feedback}/stdio.go (98%) rename {i18n => cmd/i18n}/i18n.go (100%) rename {i18n => cmd/i18n}/i18n_test.go (100%) rename {tablestyle => internal/tablestyle}/tablestyle.go (100%) rename {updater => internal/updater}/artifacts/.gitignore (100%) rename {updater => internal/updater}/artifacts/artifacts_darwin_amd64.go (100%) rename {updater => internal/updater}/artifacts/artifacts_darwin_arm64.go (100%) rename {updater => internal/updater}/artifacts/artifacts_linux_amd64.go (100%) rename {updater => internal/updater}/artifacts/artifacts_linux_arm64.go (100%) rename {updater => internal/updater}/artifacts/artifacts_windows_amd64.go (100%) rename {updater => internal/updater}/artifacts/download_resources.sh (100%) mode change 100755 => 100644 rename {updater => internal/updater}/download_image.go (97%) rename {updater => internal/updater}/flasher.go (95%) rename {updater => internal/updater}/http_client.go (100%) diff --git a/Taskfile.yml b/Taskfile.yml index 7b1ba3e..40dd5b9 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -65,21 +65,21 @@ tasks: vars: VERSION: "{{.VERSION }}" cmds: - - cmd: go build -ldflags "-X main.Version={{.VERSION}}" -v -o ./build/arduino-flasher-cli . + - cmd: go build -ldflags "-X main.Version={{.VERSION}}" -v -o ./build/arduino-flasher-cli ./cmd/arduino-flasher-cli/ platforms: [linux, darwin] - - cmd: go build -ldflags "-X main.Version={{.VERSION}}" -v -o ./build/arduino-flasher-cli.exe . + - cmd: go build -ldflags "-X main.Version={{.VERSION}}" -v -o ./build/arduino-flasher-cli.exe ./cmd/arduino-flasher-cli/ platforms: [windows] build:artifacts: desc: Prepare the arduino-flasher-cli artifacts internal: true status: - - test -f ./updater/artifacts/resources_darwin_amd64/qdl - - test -f ./updater/artifacts/resources_darwin_arm64/qdl - - test -f ./updater/artifacts/resources_linux_amd64/qdl - - test -f ./updater/artifacts/resources_linux_arm64/qdl - - test -f ./updater/artifacts/resources_windows_amd64/qdl.exe - cmd: sh ./updater/artifacts/download_resources.sh + - test -f ./internal/updater/artifacts/resources_darwin_amd64/qdl + - test -f ./internal/updater/artifacts/resources_darwin_arm64/qdl + - test -f ./internal/updater/artifacts/resources_linux_amd64/qdl + - test -f ./internal/updater/artifacts/resources_linux_arm64/qdl + - test -f ./internal/updater/artifacts/resources_windows_amd64/qdl.exe + cmd: sh ./internal/updater/artifacts/download_resources.sh release: desc: Create a tag on the current commit and push it to the remote to create the release diff --git a/download.go b/cmd/arduino-flasher-cli/download/download.go similarity index 90% rename from download.go rename to cmd/arduino-flasher-cli/download/download.go index 00e7a3b..679436d 100644 --- a/download.go +++ b/cmd/arduino-flasher-cli/download/download.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package download import ( "context" @@ -22,12 +22,12 @@ import ( "github.com/arduino/go-paths-helper" "github.com/spf13/cobra" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" - "github.com/arduino/arduino-flasher-cli/updater" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" + "github.com/arduino/arduino-flasher-cli/internal/updater" ) -func newDownloadCmd() *cobra.Command { +func NewDownloadCmd() *cobra.Command { var destDir string cmd := &cobra.Command{ Use: "download", diff --git a/drivers.go b/cmd/arduino-flasher-cli/drivers/drivers.go similarity index 86% rename from drivers.go rename to cmd/arduino-flasher-cli/drivers/drivers.go index 29b6244..77a79de 100644 --- a/drivers.go +++ b/cmd/arduino-flasher-cli/drivers/drivers.go @@ -13,16 +13,16 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package drivers import ( "github.com/spf13/cobra" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" ) -func newInstallDriversCmd() *cobra.Command { +func NewInstallDriversCmd() *cobra.Command { cmd := &cobra.Command{ Use: "install-drivers", Hidden: true, diff --git a/drivers_others.go b/cmd/arduino-flasher-cli/drivers/drivers_others.go similarity index 98% rename from drivers_others.go rename to cmd/arduino-flasher-cli/drivers/drivers_others.go index f09d437..a98a787 100644 --- a/drivers_others.go +++ b/cmd/arduino-flasher-cli/drivers/drivers_others.go @@ -15,7 +15,7 @@ //go:build !windows -package main +package drivers // installDrivers is a no-op on non-Windows platforms func installDrivers() error { diff --git a/drivers_windows.go b/cmd/arduino-flasher-cli/drivers/drivers_windows.go similarity index 98% rename from drivers_windows.go rename to cmd/arduino-flasher-cli/drivers/drivers_windows.go index f3aa04f..d2eeeb2 100644 --- a/drivers_windows.go +++ b/cmd/arduino-flasher-cli/drivers/drivers_windows.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package drivers import ( "embed" @@ -23,7 +23,7 @@ import ( "github.com/arduino/go-paths-helper" ) -//go:embed drivers +//go:embed src var drivers embed.FS // installDrivers installs the Windows driver using dpinst.exe. This requires diff --git a/drivers/dpinst-amd64.exe b/cmd/arduino-flasher-cli/drivers/src/dpinst-amd64.exe old mode 100755 new mode 100644 similarity index 100% rename from drivers/dpinst-amd64.exe rename to cmd/arduino-flasher-cli/drivers/src/dpinst-amd64.exe diff --git a/drivers/dpinst-x86.exe b/cmd/arduino-flasher-cli/drivers/src/dpinst-x86.exe old mode 100755 new mode 100644 similarity index 100% rename from drivers/dpinst-x86.exe rename to cmd/arduino-flasher-cli/drivers/src/dpinst-x86.exe diff --git a/drivers/unoq.cat b/cmd/arduino-flasher-cli/drivers/src/unoq.cat old mode 100755 new mode 100644 similarity index 100% rename from drivers/unoq.cat rename to cmd/arduino-flasher-cli/drivers/src/unoq.cat diff --git a/drivers/unoq.inf b/cmd/arduino-flasher-cli/drivers/src/unoq.inf old mode 100755 new mode 100644 similarity index 100% rename from drivers/unoq.inf rename to cmd/arduino-flasher-cli/drivers/src/unoq.inf diff --git a/flash.go b/cmd/arduino-flasher-cli/flash/flash.go similarity index 95% rename from flash.go rename to cmd/arduino-flasher-cli/flash/flash.go index 1616dd1..e12b58c 100644 --- a/flash.go +++ b/cmd/arduino-flasher-cli/flash/flash.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package flash import ( "context" @@ -26,12 +26,12 @@ import ( runas "github.com/arduino/go-windows-runas" "github.com/spf13/cobra" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" - "github.com/arduino/arduino-flasher-cli/updater" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" + "github.com/arduino/arduino-flasher-cli/internal/updater" ) -func newFlashCmd() *cobra.Command { +func NewFlashCmd() *cobra.Command { var forceYes bool var tempDir string appCmd := &cobra.Command{ diff --git a/list.go b/cmd/arduino-flasher-cli/list/list.go similarity index 87% rename from list.go rename to cmd/arduino-flasher-cli/list/list.go index ed30d57..655ffc9 100644 --- a/list.go +++ b/cmd/arduino-flasher-cli/list/list.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package list import ( "context" @@ -21,13 +21,13 @@ import ( "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" - "github.com/arduino/arduino-flasher-cli/tablestyle" - "github.com/arduino/arduino-flasher-cli/updater" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" + "github.com/arduino/arduino-flasher-cli/internal/tablestyle" + "github.com/arduino/arduino-flasher-cli/internal/updater" ) -func newListCmd() *cobra.Command { +func NewListCmd() *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "List the available Linux images", diff --git a/main.go b/cmd/arduino-flasher-cli/main.go similarity index 58% rename from main.go rename to cmd/arduino-flasher-cli/main.go index 846f047..d783968 100644 --- a/main.go +++ b/cmd/arduino-flasher-cli/main.go @@ -17,16 +17,19 @@ package main import ( "context" - "fmt" "log/slog" "os" - "github.com/fatih/color" "github.com/spf13/cobra" "go.bug.st/cleanup" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" + "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/download" + "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/drivers" + "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/flash" + "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/list" + "github.com/arduino/arduino-flasher-cli/cmd/arduino-flasher-cli/version" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" ) // Version will be set a build time with -ldflags @@ -53,33 +56,12 @@ func main() { rootCmd.PersistentFlags().StringVar(&format, "format", "text", "Output format (text, json)") rootCmd.AddCommand( - newFlashCmd(), - newInstallDriversCmd(), - newListCmd(), - newDownloadCmd(), - &cobra.Command{ - Use: "version", - Short: "Print the version number of Arduino Flasher CLI", - Run: func(cmd *cobra.Command, args []string) { - feedback.PrintResult(versionResult{ - Name: "Arduino Flasher CLI", - Version: Version, - }) - - latest, err := checkForUpdates() - if err != nil { - feedback.Warning(color.YellowString("\n\nFailed to check for updates: "+err.Error()) + "\n") - } - if latest != "" { - msg := fmt.Sprintf("\n\n%s %s → %s\n%s", - color.YellowString(i18n.Tr("A new release of Arduino Flasher CLI is available:")), - color.CyanString(Version), - color.CyanString(latest), - color.YellowString("https://www.arduino.cc/en/software/#flasher-tool")) - feedback.Warning(msg) - } - }, - }) + flash.NewFlashCmd(), + drivers.NewInstallDriversCmd(), + list.NewListCmd(), + download.NewDownloadCmd(), + version.NewVersionCmd(Version), + ) ctx := context.Background() ctx, _ = cleanup.InterruptableContext(ctx) @@ -87,17 +69,3 @@ func main() { slog.Error(err.Error()) } } - -type versionResult struct { - Name string `json:"name"` - Version string `json:"version"` -} - -func (r versionResult) String() string { - resultMessage := fmt.Sprintf("Arduino Flasher CLI version %s", r.Version) - return resultMessage -} - -func (r versionResult) Data() interface{} { - return r -} diff --git a/check_for_updates.go b/cmd/arduino-flasher-cli/version/check_for_updates.go similarity index 93% rename from check_for_updates.go rename to cmd/arduino-flasher-cli/version/check_for_updates.go index 7802f83..35be015 100644 --- a/check_for_updates.go +++ b/cmd/arduino-flasher-cli/version/check_for_updates.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package main +package version import ( "encoding/json" @@ -27,8 +27,8 @@ import ( const maxTime time.Duration = 1 * time.Second -func checkForUpdates() (string, error) { - currentVersion, err := semver.Parse(Version) +func checkForUpdates(version string) (string, error) { + currentVersion, err := semver.Parse(version) if err != nil { return "", err } diff --git a/cmd/arduino-flasher-cli/version/version.go b/cmd/arduino-flasher-cli/version/version.go new file mode 100644 index 0000000..d97fc97 --- /dev/null +++ b/cmd/arduino-flasher-cli/version/version.go @@ -0,0 +1,67 @@ +// This file is part of arduino-flasher-cli. +// +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-flasher-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package version + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/spf13/cobra" + + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" +) + +func NewVersionCmd(version string) *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print the version number of Arduino Flasher CLI", + Run: func(cmd *cobra.Command, args []string) { + feedback.PrintResult(versionResult{ + Name: "Arduino Flasher CLI", + Version: version, + }) + + latest, err := checkForUpdates(version) + if err != nil { + feedback.Warning(color.YellowString("\n\nFailed to check for updates: "+err.Error()) + "\n") + } + if latest != "" { + msg := fmt.Sprintf("\n\n%s %s → %s\n%s", + color.YellowString(i18n.Tr("A new release of Arduino Flasher CLI is available:")), + color.CyanString(version), + color.CyanString(latest), + color.YellowString("https://www.arduino.cc/en/software/#flasher-tool")) + feedback.Warning(msg) + } + }, + } + return cmd +} + +type versionResult struct { + Name string `json:"name"` + Version string `json:"version"` +} + +func (r versionResult) String() string { + resultMessage := fmt.Sprintf("Arduino Flasher CLI version %s", r.Version) + return resultMessage +} + +func (r versionResult) Data() interface{} { + return r +} diff --git a/feedback/errorcodes.go b/cmd/feedback/errorcodes.go similarity index 100% rename from feedback/errorcodes.go rename to cmd/feedback/errorcodes.go diff --git a/feedback/feedback.go b/cmd/feedback/feedback.go similarity index 99% rename from feedback/feedback.go rename to cmd/feedback/feedback.go index 96cb6aa..6e0ece7 100644 --- a/feedback/feedback.go +++ b/cmd/feedback/feedback.go @@ -22,7 +22,7 @@ import ( "io" "os" - "github.com/arduino/arduino-flasher-cli/i18n" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" ) // OutputFormat is an output format diff --git a/feedback/feedback_test.go b/cmd/feedback/feedback_test.go similarity index 100% rename from feedback/feedback_test.go rename to cmd/feedback/feedback_test.go diff --git a/feedback/stdio.go b/cmd/feedback/stdio.go similarity index 98% rename from feedback/stdio.go rename to cmd/feedback/stdio.go index d3b8343..c85f274 100644 --- a/feedback/stdio.go +++ b/cmd/feedback/stdio.go @@ -20,7 +20,7 @@ import ( "errors" "io" - "github.com/arduino/arduino-flasher-cli/i18n" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" ) // DirectStreams returns the underlying io.Writer to directly stream to diff --git a/i18n/i18n.go b/cmd/i18n/i18n.go similarity index 100% rename from i18n/i18n.go rename to cmd/i18n/i18n.go diff --git a/i18n/i18n_test.go b/cmd/i18n/i18n_test.go similarity index 100% rename from i18n/i18n_test.go rename to cmd/i18n/i18n_test.go diff --git a/tablestyle/tablestyle.go b/internal/tablestyle/tablestyle.go similarity index 100% rename from tablestyle/tablestyle.go rename to internal/tablestyle/tablestyle.go diff --git a/updater/artifacts/.gitignore b/internal/updater/artifacts/.gitignore similarity index 100% rename from updater/artifacts/.gitignore rename to internal/updater/artifacts/.gitignore diff --git a/updater/artifacts/artifacts_darwin_amd64.go b/internal/updater/artifacts/artifacts_darwin_amd64.go similarity index 100% rename from updater/artifacts/artifacts_darwin_amd64.go rename to internal/updater/artifacts/artifacts_darwin_amd64.go diff --git a/updater/artifacts/artifacts_darwin_arm64.go b/internal/updater/artifacts/artifacts_darwin_arm64.go similarity index 100% rename from updater/artifacts/artifacts_darwin_arm64.go rename to internal/updater/artifacts/artifacts_darwin_arm64.go diff --git a/updater/artifacts/artifacts_linux_amd64.go b/internal/updater/artifacts/artifacts_linux_amd64.go similarity index 100% rename from updater/artifacts/artifacts_linux_amd64.go rename to internal/updater/artifacts/artifacts_linux_amd64.go diff --git a/updater/artifacts/artifacts_linux_arm64.go b/internal/updater/artifacts/artifacts_linux_arm64.go similarity index 100% rename from updater/artifacts/artifacts_linux_arm64.go rename to internal/updater/artifacts/artifacts_linux_arm64.go diff --git a/updater/artifacts/artifacts_windows_amd64.go b/internal/updater/artifacts/artifacts_windows_amd64.go similarity index 100% rename from updater/artifacts/artifacts_windows_amd64.go rename to internal/updater/artifacts/artifacts_windows_amd64.go diff --git a/updater/artifacts/download_resources.sh b/internal/updater/artifacts/download_resources.sh old mode 100755 new mode 100644 similarity index 100% rename from updater/artifacts/download_resources.sh rename to internal/updater/artifacts/download_resources.sh diff --git a/updater/download_image.go b/internal/updater/download_image.go similarity index 97% rename from updater/download_image.go rename to internal/updater/download_image.go index 9967009..782373f 100644 --- a/updater/download_image.go +++ b/internal/updater/download_image.go @@ -28,8 +28,8 @@ import ( "github.com/codeclysm/extract/v4" "github.com/schollz/progressbar/v3" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" ) type Manifest struct { diff --git a/updater/flasher.go b/internal/updater/flasher.go similarity index 95% rename from updater/flasher.go rename to internal/updater/flasher.go index 9066245..182dbf5 100644 --- a/updater/flasher.go +++ b/internal/updater/flasher.go @@ -23,9 +23,9 @@ import ( "github.com/arduino/go-paths-helper" "github.com/shirou/gopsutil/v4/disk" - "github.com/arduino/arduino-flasher-cli/feedback" - "github.com/arduino/arduino-flasher-cli/i18n" - "github.com/arduino/arduino-flasher-cli/updater/artifacts" + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" + "github.com/arduino/arduino-flasher-cli/internal/updater/artifacts" ) const GiB = uint64(1024 * 1024 * 1024) diff --git a/updater/http_client.go b/internal/updater/http_client.go similarity index 100% rename from updater/http_client.go rename to internal/updater/http_client.go From e8af622aa550f7642bc4d7860b54e2828059c593 Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 25 Nov 2025 16:59:03 +0100 Subject: [PATCH 5/5] Run task fmt --- cmd/arduino-flasher-cli/version/version.go | 134 ++++++++++----------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/cmd/arduino-flasher-cli/version/version.go b/cmd/arduino-flasher-cli/version/version.go index d97fc97..02f814b 100644 --- a/cmd/arduino-flasher-cli/version/version.go +++ b/cmd/arduino-flasher-cli/version/version.go @@ -1,67 +1,67 @@ -// This file is part of arduino-flasher-cli. -// -// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-flasher-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package version - -import ( - "fmt" - - "github.com/fatih/color" - "github.com/spf13/cobra" - - "github.com/arduino/arduino-flasher-cli/cmd/feedback" - "github.com/arduino/arduino-flasher-cli/cmd/i18n" -) - -func NewVersionCmd(version string) *cobra.Command { - cmd := &cobra.Command{ - Use: "version", - Short: "Print the version number of Arduino Flasher CLI", - Run: func(cmd *cobra.Command, args []string) { - feedback.PrintResult(versionResult{ - Name: "Arduino Flasher CLI", - Version: version, - }) - - latest, err := checkForUpdates(version) - if err != nil { - feedback.Warning(color.YellowString("\n\nFailed to check for updates: "+err.Error()) + "\n") - } - if latest != "" { - msg := fmt.Sprintf("\n\n%s %s → %s\n%s", - color.YellowString(i18n.Tr("A new release of Arduino Flasher CLI is available:")), - color.CyanString(version), - color.CyanString(latest), - color.YellowString("https://www.arduino.cc/en/software/#flasher-tool")) - feedback.Warning(msg) - } - }, - } - return cmd -} - -type versionResult struct { - Name string `json:"name"` - Version string `json:"version"` -} - -func (r versionResult) String() string { - resultMessage := fmt.Sprintf("Arduino Flasher CLI version %s", r.Version) - return resultMessage -} - -func (r versionResult) Data() interface{} { - return r -} +// This file is part of arduino-flasher-cli. +// +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-flasher-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package version + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/spf13/cobra" + + "github.com/arduino/arduino-flasher-cli/cmd/feedback" + "github.com/arduino/arduino-flasher-cli/cmd/i18n" +) + +func NewVersionCmd(version string) *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print the version number of Arduino Flasher CLI", + Run: func(cmd *cobra.Command, args []string) { + feedback.PrintResult(versionResult{ + Name: "Arduino Flasher CLI", + Version: version, + }) + + latest, err := checkForUpdates(version) + if err != nil { + feedback.Warning(color.YellowString("\n\nFailed to check for updates: "+err.Error()) + "\n") + } + if latest != "" { + msg := fmt.Sprintf("\n\n%s %s → %s\n%s", + color.YellowString(i18n.Tr("A new release of Arduino Flasher CLI is available:")), + color.CyanString(version), + color.CyanString(latest), + color.YellowString("https://www.arduino.cc/en/software/#flasher-tool")) + feedback.Warning(msg) + } + }, + } + return cmd +} + +type versionResult struct { + Name string `json:"name"` + Version string `json:"version"` +} + +func (r versionResult) String() string { + resultMessage := fmt.Sprintf("Arduino Flasher CLI version %s", r.Version) + return resultMessage +} + +func (r versionResult) Data() interface{} { + return r +}