|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "io" |
@@ -53,17 +54,24 @@ func main() { |
53 | 54 | func mainRun() exitCode { |
54 | 55 | buildDate := build.Date |
55 | 56 | buildVersion := build.Version |
| 57 | + hasDebug, _ := utils.IsDebugEnabled() |
| 58 | + |
| 59 | + cmdFactory := factory.New(buildVersion) |
| 60 | + stderr := cmdFactory.IOStreams.ErrOut |
| 61 | + |
| 62 | + ctx := context.Background() |
56 | 63 |
|
| 64 | + updateCtx, updateCancel := context.WithCancel(ctx) |
| 65 | + defer updateCancel() |
57 | 66 | updateMessageChan := make(chan *update.ReleaseInfo) |
58 | 67 | go func() { |
59 | | - rel, _ := checkForUpdate(buildVersion) |
| 68 | + rel, err := checkForUpdate(updateCtx, cmdFactory, buildVersion) |
| 69 | + if err != nil && hasDebug { |
| 70 | + fmt.Fprintf(stderr, "warning: checking for update failed: %v", err) |
| 71 | + } |
60 | 72 | updateMessageChan <- rel |
61 | 73 | }() |
62 | 74 |
|
63 | | - hasDebug, _ := utils.IsDebugEnabled() |
64 | | - |
65 | | - cmdFactory := factory.New(buildVersion) |
66 | | - stderr := cmdFactory.IOStreams.ErrOut |
67 | 75 | if !cmdFactory.IOStreams.ColorEnabled() { |
68 | 76 | surveyCore.DisableColor = true |
69 | 77 | ansi.DisableColors(true) |
@@ -209,7 +217,7 @@ func mainRun() exitCode { |
209 | 217 |
|
210 | 218 | rootCmd.SetArgs(expandedArgs) |
211 | 219 |
|
212 | | - if cmd, err := rootCmd.ExecuteC(); err != nil { |
| 220 | + if cmd, err := rootCmd.ExecuteContextC(ctx); err != nil { |
213 | 221 | var pagerPipeError *iostreams.ErrClosedPagerPipe |
214 | 222 | var noResultsError cmdutil.NoResultsError |
215 | 223 | if err == cmdutil.SilentError { |
@@ -257,6 +265,7 @@ func mainRun() exitCode { |
257 | 265 | return exitError |
258 | 266 | } |
259 | 267 |
|
| 268 | + updateCancel() // if the update checker hasn't completed by now, abort it |
260 | 269 | newRelease := <-updateMessageChan |
261 | 270 | if newRelease != nil { |
262 | 271 | isHomebrew := isUnderHomebrew(cmdFactory.Executable()) |
@@ -348,21 +357,17 @@ func isCI() bool { |
348 | 357 | os.Getenv("RUN_ID") != "" // TaskCluster, dsari |
349 | 358 | } |
350 | 359 |
|
351 | | -func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) { |
| 360 | +func checkForUpdate(ctx context.Context, f *cmdutil.Factory, currentVersion string) (*update.ReleaseInfo, error) { |
352 | 361 | if !shouldCheckForUpdate() { |
353 | 362 | return nil, nil |
354 | 363 | } |
355 | | - httpClient, err := api.NewHTTPClient(api.HTTPClientOptions{ |
356 | | - AppVersion: currentVersion, |
357 | | - Log: os.Stderr, |
358 | | - }) |
| 364 | + httpClient, err := f.HttpClient() |
359 | 365 | if err != nil { |
360 | 366 | return nil, err |
361 | 367 | } |
362 | | - client := api.NewClientFromHTTP(httpClient) |
363 | 368 | repo := updaterEnabled |
364 | 369 | stateFilePath := filepath.Join(config.StateDir(), "state.yml") |
365 | | - return update.CheckForUpdate(client, stateFilePath, repo, currentVersion) |
| 370 | + return update.CheckForUpdate(ctx, httpClient, stateFilePath, repo, currentVersion) |
366 | 371 | } |
367 | 372 |
|
368 | 373 | func isRecentRelease(publishedAt time.Time) bool { |
|
0 commit comments