Go routine it
Co-Authored-By: Josh Abernathy <joshaber@gmail.com>
This commit is contained in:
parent
9d0a32fab9
commit
5a3a2cf95e
2 changed files with 17 additions and 9 deletions
|
|
@ -13,29 +13,32 @@ import (
|
|||
|
||||
const nwo = "github/homebrew-gh"
|
||||
|
||||
func CheckForUpdate() error {
|
||||
func CheckForUpdate(handleUpdate chan func()) {
|
||||
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
||||
return nil
|
||||
handleUpdate <- nil
|
||||
}
|
||||
|
||||
latestVersion, err := getLastestVersion()
|
||||
if err != nil {
|
||||
return err
|
||||
handleUpdate <- nil
|
||||
}
|
||||
|
||||
updateAvailable := latestVersion != Version
|
||||
|
||||
if updateAvailable {
|
||||
fmt.Printf(utils.Cyan(`
|
||||
New version of gh is available! %s → %s
|
||||
handleUpdate <- func() {
|
||||
fmt.Printf(utils.Cyan(`
|
||||
A new version of gh is available! %s → %s
|
||||
Changelog: https://github.com/%s/releases/tag/%[2]s
|
||||
Run 'brew upgrade github/gh/gh' to update!`)+"\n\n", Version, latestVersion, nwo)
|
||||
}
|
||||
} else {
|
||||
handleUpdate <- nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLastestVersion() (string, error) {
|
||||
url := fmt.Sprint("https://api.github.com/repos/%s/releases/latest", nwo)
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", nwo)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
7
main.go
7
main.go
|
|
@ -9,7 +9,10 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
command.CheckForUpdate()
|
||||
handleUpdate := make(chan func())
|
||||
go command.CheckForUpdate(handleUpdate)
|
||||
|
||||
fmt.Printf("🌭 %+v\n", "bang")
|
||||
|
||||
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
|
|
@ -19,4 +22,6 @@ func main() {
|
|||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
(<-handleUpdate)()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue