Skip to content

Commit a2f6aab

Browse files
fix: handle interrupt signals and correctly delete temp files (#33)
* fix: handle interrupt signals and correctly delete temp files * Use context while retrieving info manifest
1 parent 9c33638 commit a2f6aab

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

download.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package main
1717

1818
import (
19+
"context"
1920
"os"
2021

2122
"github.com/arduino/go-paths-helper"
@@ -35,23 +36,23 @@ func newDownloadCmd() *cobra.Command {
3536
Example: " " + os.Args[0] + " download latest\n" +
3637
" " + os.Args[0] + " download latest --dest-dir /tmp\n",
3738
Run: func(cmd *cobra.Command, args []string) {
38-
runDownloadCommand(args, destDir)
39+
runDownloadCommand(cmd.Context(), args, destDir)
3940
},
4041
}
4142
cmd.Flags().StringVar(&destDir, "dest-dir", ".", "Path to the directory in which the image will be downloaded")
4243

4344
return cmd
4445
}
4546

46-
func runDownloadCommand(args []string, destDir string) {
47+
func runDownloadCommand(ctx context.Context, args []string, destDir string) {
4748
targetVersion := args[0]
4849
downloadPath := paths.New(destDir)
4950
if !downloadPath.IsDir() {
5051
feedback.Fatal(i18n.Tr("error: %s is not a directory. Please, select an existing directory.", destDir), feedback.ErrBadArgument)
5152
}
5253

5354
client := updater.NewClient()
54-
_, _, err := updater.DownloadImage(client, targetVersion, nil, true, downloadPath)
55+
_, _, err := updater.DownloadImage(ctx, client, targetVersion, nil, true, downloadPath)
5556
if err != nil {
5657
feedback.Fatal(i18n.Tr("error downloading the image: %v", err), feedback.ErrBadArgument)
5758
}

list.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package main
1717

1818
import (
19+
"context"
20+
1921
"github.com/jedib0t/go-pretty/v6/table"
2022
"github.com/spf13/cobra"
2123

@@ -31,16 +33,16 @@ func newListCmd() *cobra.Command {
3133
Short: "List the available Linux images",
3234
Args: cobra.NoArgs,
3335
Run: func(cmd *cobra.Command, args []string) {
34-
runListCommand()
36+
runListCommand(cmd.Context())
3537
},
3638
}
3739
return cmd
3840
}
3941

40-
func runListCommand() {
42+
func runListCommand(ctx context.Context) {
4143
client := updater.NewClient()
4244

43-
manifest, err := client.GetInfoManifest()
45+
manifest, err := client.GetInfoManifest(ctx)
4446
if err != nil {
4547
feedback.Fatal(i18n.Tr("error retrieving the manifest: %v", err), feedback.ErrBadArgument)
4648
}

updater/download_image.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type Release struct {
4646
// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
4747
type DownloadConfirmCB func(target string) (bool, error)
4848

49-
func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, temp *paths.Path) (*paths.Path, string, error) {
50-
tmpZip, version, err := DownloadImage(client, targetVersion, upgradeConfirmCb, forceYes, temp)
49+
func DownloadAndExtract(ctx context.Context, client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, temp *paths.Path) (*paths.Path, string, error) {
50+
tmpZip, version, err := DownloadImage(ctx, client, targetVersion, upgradeConfirmCb, forceYes, temp)
5151
if err != nil {
5252
return nil, "", fmt.Errorf("error downloading the image: %v", err)
5353
}
@@ -57,7 +57,7 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
5757
return nil, "", nil
5858
}
5959

60-
err = ExtractImage(tmpZip, tmpZip.Parent())
60+
err = ExtractImage(ctx, tmpZip, tmpZip.Parent())
6161
if err != nil {
6262
return nil, "", fmt.Errorf("error extracting the image: %v", err)
6363
}
@@ -69,11 +69,11 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
6969
return imagePath, version, nil
7070
}
7171

72-
func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) {
72+
func DownloadImage(ctx context.Context, client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool, downloadPath *paths.Path) (*paths.Path, string, error) {
7373
var err error
7474

7575
feedback.Print(i18n.Tr("Checking for Debian image releases"))
76-
manifest, err := client.GetInfoManifest()
76+
manifest, err := client.GetInfoManifest(ctx)
7777
if err != nil {
7878
return nil, "", err
7979
}
@@ -105,7 +105,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
105105
}
106106
}
107107

108-
download, size, err := client.FetchZip(rel.Url)
108+
download, size, err := client.FetchZip(ctx, rel.Url)
109109
if err != nil {
110110
return nil, "", fmt.Errorf("could not fetch Debian image: %w", err)
111111
}
@@ -140,7 +140,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
140140
return tmpZip, rel.Version, nil
141141
}
142142

143-
func ExtractImage(archive, temp *paths.Path) error {
143+
func ExtractImage(ctx context.Context, archive, temp *paths.Path) error {
144144
// Unzip the Debian image
145145
feedback.Print(i18n.Tr("Unzipping Debian image"))
146146
tmpZipFile, err := archive.Open()
@@ -149,7 +149,7 @@ func ExtractImage(archive, temp *paths.Path) error {
149149
}
150150
defer tmpZipFile.Close()
151151

152-
if err := extract.Archive(context.Background(), tmpZipFile, temp.String(), func(s string) string {
152+
if err := extract.Archive(ctx, tmpZipFile, temp.String(), func(s string) string {
153153
feedback.Print(s)
154154
return s
155155
}); err != nil {

updater/flasher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
5252
return fmt.Errorf("download and extraction requires up to %d GiB of free space", DownloadDiskSpace)
5353
}
5454

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

@@ -94,7 +94,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
9494
return fmt.Errorf("extraction requires up to %d GiB of free space", ExtractDiskSpace)
9595
}
9696

97-
err = ExtractImage(imagePath, temp)
97+
err = ExtractImage(ctx, imagePath, temp)
9898
if err != nil {
9999
return fmt.Errorf("error extracting the archive: %v", err)
100100
}

updater/http_client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package updater
1717

1818
import (
19+
"context"
1920
"crypto/sha256"
2021
"encoding/hex"
2122
"encoding/json"
@@ -79,9 +80,9 @@ func (c *Client) addHeaders(req *http.Request) {
7980
}
8081

8182
// GetInfoManifest fetches and decodes the Debian images info.json.
82-
func (c *Client) GetInfoManifest() (Manifest, error) {
83+
func (c *Client) GetInfoManifest(ctx context.Context) (Manifest, error) {
8384
manifestURL := baseURL.JoinPath(pathRelease, "info.json").String()
84-
req, err := http.NewRequest("GET", manifestURL, nil)
85+
req, err := http.NewRequestWithContext(ctx, "GET", manifestURL, nil)
8586
if err != nil {
8687
return Manifest{}, fmt.Errorf("failed to create request: %w", err)
8788
}
@@ -109,8 +110,8 @@ func (c *Client) GetInfoManifest() (Manifest, error) {
109110
}
110111

111112
// FetchZip fetches the Debian image archive.
112-
func (c *Client) FetchZip(zipURL string) (io.ReadCloser, int64, error) {
113-
req, err := http.NewRequest("GET", zipURL, nil)
113+
func (c *Client) FetchZip(ctx context.Context, zipURL string) (io.ReadCloser, int64, error) {
114+
req, err := http.NewRequestWithContext(ctx, "GET", zipURL, nil)
114115
if err != nil {
115116
return nil, 0, fmt.Errorf("failed to create request: %w", err)
116117
}

0 commit comments

Comments
 (0)