Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package main

import (
"context"
"os"

"github.com/arduino/go-paths-helper"
Expand All @@ -35,23 +36,23 @@ 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")

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() {
feedback.Fatal(i18n.Tr("error: %s is not a directory", destDir), feedback.ErrBadArgument)
}

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)
}
Expand Down
8 changes: 5 additions & 3 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package main

import (
"context"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"

Expand All @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions updater/download_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions updater/flasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions updater/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package updater

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
Loading