Skip to content

Commit

Permalink
pipe context into update
Browse files Browse the repository at this point in the history
  • Loading branch information
abuchanan-airbyte committed Sep 19, 2024
1 parent 0b8a47f commit 153400a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type doer interface {
// This is accomplished by fetching the latest github tag and comparing it to the version provided.
// Returns the latest version, or an empty string if we're already running the latest version.
// Will return ErrDevVersion if the build.Version is currently set to "dev".
func Check() (string, error) {
ctx, updateCancel := context.WithTimeout(context.Background(), 2*time.Second)
func Check(ctx context.Context) (string, error) {
ctx, updateCancel := context.WithTimeout(ctx, 2*time.Second)
defer updateCancel()
return check(ctx, http.DefaultClient, build.Version)
}
Expand Down
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
func main() {
// ensure the pterm info width matches the other printers
pterm.Info.Prefix.Text = " INFO "
printUpdateMsg := checkForNewerAbctlVersion()
handleErr(run())

ctx := cliContext()
printUpdateMsg := checkForNewerAbctlVersion(ctx)
handleErr(run(ctx))
printUpdateMsg()
}

func run() error {
ctx, cancel := cliContext()
defer cancel()
func run(ctx context.Context) error {


var root cmd.Cmd
parser, err := kong.New(
Expand Down Expand Up @@ -68,11 +69,11 @@ func handleErr(err error) {

// checks for a newer version of abctl.
// returns a function that, when called, will print the message about the new version.
func checkForNewerAbctlVersion() func() {
func checkForNewerAbctlVersion(ctx context.Context) func() {
c := make(chan string)
go func() {
defer close(c)
ver, err := update.Check()
ver, err := update.Check(ctx)
if err != nil {
pterm.Debug.Printfln("update check: %s", err)
} else {
Expand All @@ -90,7 +91,7 @@ func checkForNewerAbctlVersion() func() {
}

// get a context that listens for interrupt/shutdown signals.
func cliContext() (context.Context, context.CancelFunc) {
func cliContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
// listen for shutdown signals
go func() {
Expand All @@ -100,7 +101,7 @@ func cliContext() (context.Context, context.CancelFunc) {

cancel()
}()
return ctx, cancel
return ctx
}

// bindCtx exists to allow kong to correctly inject a context.Context into the Run methods on the commands.
Expand Down

0 comments on commit 153400a

Please sign in to comment.