Skip to content

Commit 7bc34a6

Browse files
committed
Fix update check context handling
1 parent c9cf5ae commit 7bc34a6

8 files changed

Lines changed: 31 additions & 31 deletions

File tree

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ run:
55
linters:
66
default: all
77
disable:
8+
- goconst
89
- containedctx
910
- copyloopvar
1011
- cyclop

internal/cli/cli.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func Main(version string, buildDate string) int {
104104
}
105105

106106
if rootFlags.upgrade {
107-
upgrader, err := upgrade.NewBinaryUpgrader(registry.NewGithubRegistry(ctx), version)
107+
upgrader, err := upgrade.NewBinaryUpgrader(registry.NewGithubRegistry(), version)
108108
if err == nil {
109-
err = upgrader.Upgrade()
109+
err = upgrader.Upgrade(ctx)
110110
}
111111

112112
if err != nil {
@@ -138,6 +138,7 @@ func Main(version string, buildDate string) int {
138138
}
139139

140140
log.Errorf("lets: %s", err.Error())
141+
141142
return getExitCode(err, 1)
142143
}
143144

@@ -212,7 +213,7 @@ func maybeStartUpdateCheck(
212213

213214
log.Debugf("lets: start update check")
214215

215-
notifier, err := upgrade.NewUpdateNotifier(registry.NewGithubRegistry(ctx))
216+
notifier, err := upgrade.NewUpdateNotifier(registry.NewGithubRegistry())
216217
if err != nil {
217218
return nil, func() {}
218219
}
@@ -225,6 +226,7 @@ func maybeStartUpdateCheck(
225226
if err != nil {
226227
upgrade.LogUpdateCheckError(err)
227228
}
229+
228230
log.Debugf("lets: update check done")
229231

230232
ch <- updateCheckResult{

internal/upgrade/notifier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (n *UpdateNotifier) noticeFromState(
152152
}
153153

154154
command := "lets self upgrade"
155+
155156
if isHomebrewInstall(n.executablePath) {
156157
if !state.LatestPublishedAt.IsZero() && now.Sub(state.LatestPublishedAt) < homebrewNoticeDelay {
157158
return nil

internal/upgrade/notifier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (m *mockNotifierRegistry) GetLatestReleaseInfo(ctx context.Context) (*regis
1919
return m.release, nil
2020
}
2121

22-
func (m *mockNotifierRegistry) GetLatestRelease() (string, error) {
22+
func (m *mockNotifierRegistry) GetLatestRelease(ctx context.Context) (string, error) {
2323
m.calls++
2424
if m.release == nil {
2525
return "", nil
@@ -28,7 +28,7 @@ func (m *mockNotifierRegistry) GetLatestRelease() (string, error) {
2828
return m.release.TagName, nil
2929
}
3030

31-
func (m *mockNotifierRegistry) DownloadReleaseBinary(packageName string, version string, dstPath string) error {
31+
func (m *mockNotifierRegistry) DownloadReleaseBinary(ctx context.Context, packageName string, version string, dstPath string) error {
3232
return nil
3333
}
3434

internal/upgrade/registry/registry.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,28 @@ var osMap = map[string]string{
2626

2727
type RepoRegistry interface {
2828
GetLatestReleaseInfo(ctx context.Context) (*ReleaseInfo, error)
29-
GetLatestRelease() (string, error)
30-
DownloadReleaseBinary(packageName string, version string, dstPath string) error
29+
GetLatestRelease(ctx context.Context) (string, error)
30+
DownloadReleaseBinary(ctx context.Context, packageName string, version string, dstPath string) error
3131
GetPackageName(os string, arch string) (string, error)
3232
GetDownloadURL(repoURI string, packageName string, version string) string
3333
}
3434

3535
type GithubRegistry struct {
3636
client *http.Client
37-
ctx context.Context
3837
repoURI string
3938
apiURI string
4039
downloadURL string
4140
downloadPackageTimeout time.Duration
4241
latestReleaseTimeout time.Duration
4342
}
4443

45-
func NewGithubRegistry(ctx context.Context) *GithubRegistry {
44+
func NewGithubRegistry() *GithubRegistry {
4645
client := &http.Client{
4746
Timeout: 15 * 60 * time.Second, // global timeout
4847
}
4948

5049
reg := &GithubRegistry{
5150
client: client,
52-
ctx: ctx,
5351
repoURI: "https://github.com/lets-cli/lets",
5452
apiURI: "https://api.github.com/repos/lets-cli/lets",
5553
downloadURL: "",
@@ -76,13 +74,14 @@ func (reg *GithubRegistry) GetPackageName(os string, arch string) (string, error
7674
}
7775

7876
func (reg *GithubRegistry) DownloadReleaseBinary(
77+
ctx context.Context,
7978
packageName string,
8079
version string,
8180
dstPath string,
8281
) error {
8382
downloadURL := reg.GetDownloadURL(reg.repoURI, packageName+".tar.gz", version)
8483

85-
ctx, cancel := context.WithTimeout(reg.ctx, reg.downloadPackageTimeout)
84+
ctx, cancel := context.WithTimeout(ctx, reg.downloadPackageTimeout)
8685
defer cancel()
8786

8887
req, err := http.NewRequestWithContext(
@@ -128,7 +127,7 @@ func (reg *GithubRegistry) DownloadReleaseBinary(
128127

129128
// TODO add download progress bar
130129
// TODO drop extract dependency, replace with own code
131-
err = extract.Gz(reg.ctx, resp.Body, dstDir, nil)
130+
err = extract.Gz(ctx, resp.Body, dstDir, nil)
132131
if err != nil {
133132
return fmt.Errorf("failed to extract package: %w", err)
134133
}
@@ -147,8 +146,8 @@ type ReleaseInfo struct {
147146
PublishedAt time.Time `json:"published_at"`
148147
}
149148

150-
func (reg *GithubRegistry) GetLatestRelease() (string, error) {
151-
release, err := reg.GetLatestReleaseInfo(reg.ctx)
149+
func (reg *GithubRegistry) GetLatestRelease(ctx context.Context) (string, error) {
150+
release, err := reg.GetLatestReleaseInfo(ctx)
152151
if err != nil {
153152
return "", err
154153
}
@@ -157,12 +156,7 @@ func (reg *GithubRegistry) GetLatestRelease() (string, error) {
157156
}
158157

159158
func (reg *GithubRegistry) GetLatestReleaseInfo(ctx context.Context) (*ReleaseInfo, error) {
160-
requestCtx := reg.ctx
161-
if ctx != nil {
162-
requestCtx = ctx
163-
}
164-
165-
requestCtx, cancel := context.WithTimeout(requestCtx, reg.latestReleaseTimeout)
159+
requestCtx, cancel := context.WithTimeout(ctx, reg.latestReleaseTimeout)
166160
defer cancel()
167161

168162
url := reg.apiURI + "/releases/latest"

internal/upgrade/registry/registry_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestGithubRegistryGetLatestReleaseInfo(t *testing.T) {
2121
}))
2222
defer server.Close()
2323

24-
reg := NewGithubRegistry(context.Background())
24+
reg := NewGithubRegistry()
2525
reg.apiURI = server.URL
2626

2727
release, err := reg.GetLatestReleaseInfo(context.Background())
@@ -43,10 +43,10 @@ func TestGithubRegistryGetLatestRelease(t *testing.T) {
4343
}))
4444
defer server.Close()
4545

46-
reg := NewGithubRegistry(context.Background())
46+
reg := NewGithubRegistry()
4747
reg.apiURI = server.URL
4848

49-
version, err := reg.GetLatestRelease()
49+
version, err := reg.GetLatestRelease(context.Background())
5050
if err != nil {
5151
t.Fatalf("GetLatestRelease() error = %v", err)
5252
}

internal/upgrade/upgrade.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package upgrade
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"os"
@@ -12,7 +13,7 @@ import (
1213
)
1314

1415
type Upgrader interface {
15-
Upgrade() error
16+
Upgrade(ctx context.Context) error
1617
}
1718

1819
type BinaryUpgrader struct {
@@ -39,8 +40,8 @@ func NewBinaryUpgrader(reg registry.RepoRegistry, currentVersion string) (*Binar
3940
}, nil
4041
}
4142

42-
func (up *BinaryUpgrader) Upgrade() error {
43-
latestVersion, err := up.registry.GetLatestRelease()
43+
func (up *BinaryUpgrader) Upgrade(ctx context.Context) error {
44+
latestVersion, err := up.registry.GetLatestRelease(ctx)
4445
if err != nil {
4546
return fmt.Errorf("failed to get latest release version: %w", err)
4647
}
@@ -59,6 +60,7 @@ func (up *BinaryUpgrader) Upgrade() error {
5960
log.Printf("Downloading latest release %s...", latestVersion)
6061

6162
err = up.registry.DownloadReleaseBinary(
63+
ctx,
6264
packageName,
6365
latestVersion,
6466
up.downloadPath,

internal/upgrade/upgrade_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ type MockRegistry struct {
1515
latestVersion string
1616
}
1717

18-
func (m MockRegistry) GetLatestRelease() (string, error) {
18+
func (m MockRegistry) GetLatestRelease(ctx context.Context) (string, error) {
1919
return m.latestVersion, nil
2020
}
2121

2222
func (m MockRegistry) GetLatestReleaseInfo(ctx context.Context) (*registry.ReleaseInfo, error) {
2323
return &registry.ReleaseInfo{TagName: m.latestVersion}, nil
2424
}
2525

26-
func (m MockRegistry) DownloadReleaseBinary(packageName string, version string, dstPath string) error {
26+
func (m MockRegistry) DownloadReleaseBinary(ctx context.Context, packageName string, version string, dstPath string) error {
2727
file, err := os.Create(dstPath)
2828
if err != nil {
2929
return err
3030
}
3131

32-
latest, _ := m.GetLatestRelease()
32+
latest, _ := m.GetLatestRelease(ctx)
3333

3434
_, err = fmt.Fprint(file, latest)
3535
if err != nil {
@@ -101,7 +101,7 @@ func TestSelfUpgrade(t *testing.T) {
101101
t.Errorf("expected version %s", currentVersion)
102102
}
103103

104-
err = upgrader.Upgrade()
104+
err = upgrader.Upgrade(context.Background())
105105
if err != nil {
106106
t.Errorf("failed to upgrade: %s", err)
107107
}
@@ -129,7 +129,7 @@ func TestSelfUpgrade(t *testing.T) {
129129
// sleep to be sure files not created at the same time
130130
time.Sleep(10 * time.Millisecond)
131131

132-
err = upgrader.Upgrade()
132+
err = upgrader.Upgrade(context.Background())
133133
if err != nil {
134134
t.Errorf("failed to upgrade: %s", err)
135135
}

0 commit comments

Comments
 (0)