diff --git a/download.go b/download.go index 6197139..9df4420 100644 --- a/download.go +++ b/download.go @@ -16,6 +16,7 @@ package main import ( + "context" "os" "github.com/arduino/go-paths-helper" @@ -35,7 +36,7 @@ func newDownloadCmd() *cobra.Command { Example: " " + os.Args[0] + " download latest\n" + " " + os.Args[0] + " download latest --dest-dir /tmp\n", Run: func(cmd *cobra.Command, args []string) { - runDownloadCommand(args, destDir) + runDownloadCommand(cmd.Context(), args, destDir) }, } cmd.Flags().StringVar(&destDir, "dest-dir", ".", "Path to the directory in which the image will be downloaded") @@ -43,7 +44,7 @@ func newDownloadCmd() *cobra.Command { return cmd } -func runDownloadCommand(args []string, destDir string) { +func runDownloadCommand(ctx context.Context, args []string, destDir string) { targetVersion := args[0] downloadPath := paths.New(destDir) if !downloadPath.IsDir() { @@ -51,7 +52,7 @@ func runDownloadCommand(args []string, destDir string) { } client := updater.NewClient() - _, _, err := updater.DownloadImage(client, targetVersion, nil, true, downloadPath) + _, _, err := updater.DownloadImage(ctx, client, targetVersion, nil, true, downloadPath) if err != nil { feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument) } diff --git a/list.go b/list.go index 3902cf0..ed30d57 100644 --- a/list.go +++ b/list.go @@ -16,6 +16,8 @@ package main import ( + "context" + "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" @@ -31,16 +33,16 @@ func newListCmd() *cobra.Command { Short: "List the available Linux images", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - runListCommand() + runListCommand(cmd.Context()) }, } return cmd } -func runListCommand() { +func runListCommand(ctx context.Context) { client := updater.NewClient() - manifest, err := client.GetInfoManifest() + manifest, err := client.GetInfoManifest(ctx) if err != nil { feedback.Fatal(i18n.Tr("error retrieving the manifest: %v", err), feedback.ErrBadArgument) } diff --git a/updater/download_image.go b/updater/download_image.go index 28b980f..36f92f4 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(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, temp *paths.Path) (*paths.Path, string, error) { - tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, temp) +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) if err != nil { return nil, "", fmt.Errorf("error downloading the image: %v", err) } @@ -57,7 +57,7 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D return nil, "", nil } - err = ExtractImage(tmpZip, tmpZip.Parent()) + err = ExtractImage(ctx, tmpZip, tmpZip.Parent()) if err != nil { return nil, "", fmt.Errorf("error extracting the image: %v", err) } @@ -69,11 +69,11 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D return imagePath, version, nil } -func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) { +func DownloadImage(ctx context.Context, client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) { var err error feedback.Print(i18n.Tr("Checking for Debian image releases")) - manifest, err := client.GetInfoManifest() + manifest, err := client.GetInfoManifest(ctx) if err != nil { return nil, "", err } @@ -105,7 +105,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo } } - download, size, err := client.FetchZip(rel.Url) + download, size, err := client.FetchZip(ctx, rel.Url) if err != nil { return nil, "", fmt.Errorf("could not fetch Debian image: %w", err) } @@ -140,7 +140,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo return tmpZip, rel.Version, nil } -func ExtractImage(archive, temp *paths.Path) error { +func ExtractImage(ctx context.Context, archive, temp *paths.Path) error { // Unzip the Debian image feedback.Print(i18n.Tr("Unzipping Debian image")) tmpZipFile, err := archive.Open() @@ -149,7 +149,7 @@ func ExtractImage(archive, temp *paths.Path) error { } defer tmpZipFile.Close() - if err := extract.Archive(context.Background(), tmpZipFile, temp.String(), func(s string) string { + if err := extract.Archive(ctx, tmpZipFile, temp.String(), func(s string) string { feedback.Print(s) return s }); err != nil { diff --git a/updater/flasher.go b/updater/flasher.go index 616a6fb..d0104ce 100644 --- a/updater/flasher.go +++ b/updater/flasher.go @@ -52,7 +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(client, version, func(target string) (bool, error) { + 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)") @@ -94,7 +94,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes return fmt.Errorf("extraction requires up to %d GiB of free space", ExtractDiskSpace) } - err = ExtractImage(imagePath, temp) + err = ExtractImage(ctx, imagePath, temp) if err != nil { return fmt.Errorf("error extracting the archive: %v", err) } diff --git a/updater/http_client.go b/updater/http_client.go index 240dc98..58b0a3e 100644 --- a/updater/http_client.go +++ b/updater/http_client.go @@ -16,6 +16,7 @@ package updater import ( + "context" "crypto/sha256" "encoding/hex" "encoding/json" @@ -79,9 +80,9 @@ func (c *Client) addHeaders(req *http.Request) { } // GetInfoManifest fetches and decodes the Debian images info.json. -func (c *Client) GetInfoManifest() (Manifest, error) { +func (c *Client) GetInfoManifest(ctx context.Context) (Manifest, error) { manifestURL := baseURL.JoinPath(pathRelease, "info.json").String() - req, err := http.NewRequest("GET", manifestURL, nil) + req, err := http.NewRequestWithContext(ctx, "GET", manifestURL, nil) if err != nil { return Manifest{}, fmt.Errorf("failed to create request: %w", err) } @@ -109,8 +110,8 @@ func (c *Client) GetInfoManifest() (Manifest, error) { } // FetchZip fetches the Debian image archive. -func (c *Client) FetchZip(zipURL string) (io.ReadCloser, int64, error) { - req, err := http.NewRequest("GET", zipURL, nil) +func (c *Client) FetchZip(ctx context.Context, zipURL string) (io.ReadCloser, int64, error) { + req, err := http.NewRequestWithContext(ctx, "GET", zipURL, nil) if err != nil { return nil, 0, fmt.Errorf("failed to create request: %w", err) }