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 85% rename from download.go rename to cmd/arduino-flasher-cli/download/download.go index e4e111e..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", @@ -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, nil, true, 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/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 80% rename from flash.go rename to cmd/arduino-flasher-cli/flash/flash.go index 77c6b03..e12b58c 100644 --- a/flash.go +++ b/cmd/arduino-flasher-cli/flash/flash.go @@ -13,23 +13,25 @@ // 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" + "fmt" "os" "runtime" + "strings" "github.com/arduino/go-paths-helper" 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{ @@ -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/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..02f814b --- /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 82% rename from updater/download_image.go rename to internal/updater/download_image.go index 06db7fe..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 { @@ -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, 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, 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, upgradeConfirmCb DownloadConfirmCB, forceYes bool, 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 @@ -94,17 +89,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/internal/updater/flasher.go similarity index 69% rename from updater/flasher.go rename to internal/updater/flasher.go index d0104ce..182dbf5 100644 --- a/updater/flasher.go +++ b/internal/updater/flasher.go @@ -19,14 +19,13 @@ import ( "context" "fmt" "runtime" - "strings" "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) @@ -35,8 +34,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,30 +49,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, 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, 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() { @@ -107,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() { @@ -182,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 } 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