Skip to content

Commit de98dbd

Browse files
committed
Make the "check update" interface a little cleaner
1 parent 78a9599 commit de98dbd

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

main.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@ import (
1010
)
1111

1212
func main() {
13-
printUpdateMessage := make(chan func())
14-
go update.CheckForUpdate(printUpdateMessage)
15-
16-
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
17-
fmt.Fprintln(os.Stderr, err)
18-
_, isFlagError := err.(command.FlagError)
19-
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
20-
fmt.Fprintln(os.Stderr, cmd.UsageString())
13+
isProduction := os.Getenv("APP_ENV") != "production"
14+
update.RunWhileCheckingForUpdate(isProduction, func() {
15+
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
16+
fmt.Fprintln(os.Stderr, err)
17+
_, isFlagError := err.(command.FlagError)
18+
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
19+
fmt.Fprintln(os.Stderr, cmd.UsageString())
20+
}
21+
os.Exit(1)
2122
}
22-
os.Exit(1)
23-
}
24-
25-
printFunc := <-printUpdateMessage
26-
if printFunc != nil {
27-
printFunc()
28-
}
23+
})
2924
}

update/checkForUpdate.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,44 @@ type releaseInfo struct {
1919
URL string `json:"html_url"`
2020
}
2121

22-
func CheckForUpdate(handleUpdate chan func()) {
23-
// Only check for updates in production
24-
if os.Getenv("APP_ENV") != "production" {
25-
handleUpdate <- nil
22+
func RunWhileCheckingForUpdate(isProduction bool, f func()) {
23+
if isProduction {
24+
f()
2625
return
2726
}
2827

28+
newReleaseChan := make(chan *releaseInfo)
29+
go checkForUpdate(newReleaseChan)
30+
f()
31+
32+
newRelease := <-newReleaseChan
33+
if newRelease != nil {
34+
fmt.Printf(utils.Cyan(`
35+
A new version of gh is available! %s → %s
36+
Changelog: %s
37+
Run 'brew upgrade gh' to update!`)+"\n\n", command.Version, newRelease.Version, newRelease.URL)
38+
}
39+
}
40+
41+
func checkForUpdate(newReleaseChan chan *releaseInfo) {
2942
// Ignore if this stdout is not a tty
3043
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
31-
handleUpdate <- nil
44+
newReleaseChan <- nil
3245
return
3346
}
3447

3548
latestRelease, err := getLatestRelease()
3649
if err != nil {
37-
handleUpdate <- nil
50+
newReleaseChan <- nil
3851
return
3952
}
4053

4154
updateAvailable := latestRelease.Version != command.Version
4255

4356
if updateAvailable {
44-
handleUpdate <- func() {
45-
fmt.Printf(utils.Cyan(`
46-
A new version of gh is available! %s → %s
47-
Changelog: %s
48-
Run 'brew upgrade gh' to update!`)+"\n\n", command.Version, latestRelease.Version, latestRelease.URL)
49-
}
57+
newReleaseChan <- latestRelease
5058
} else {
51-
handleUpdate <- nil
59+
newReleaseChan <- nil
5260
}
5361
}
5462

0 commit comments

Comments
 (0)