Use release URL from API
This commit is contained in:
parent
26056f48f4
commit
30bfca0a2a
1 changed files with 15 additions and 13 deletions
|
|
@ -13,47 +13,49 @@ import (
|
|||
|
||||
const nwo = "github/homebrew-gh"
|
||||
|
||||
type releaseInfo struct {
|
||||
Version string `json:"tag_name"`
|
||||
URL string `json:"html_url"`
|
||||
}
|
||||
|
||||
func CheckForUpdate(handleUpdate chan func()) {
|
||||
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
|
||||
handleUpdate <- nil
|
||||
}
|
||||
|
||||
latestVersion, err := getLatestVersion()
|
||||
latestRelease, err := getLatestRelease()
|
||||
if err != nil {
|
||||
handleUpdate <- nil
|
||||
}
|
||||
|
||||
updateAvailable := latestVersion != Version
|
||||
updateAvailable := latestRelease.Version != Version
|
||||
|
||||
if updateAvailable {
|
||||
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 reinstall gh' to update!`)+"\n\n", Version, latestVersion, nwo)
|
||||
Changelog: %s
|
||||
Run 'brew reinstall gh' to update!`)+"\n\n", Version, latestRelease.Version, latestRelease.URL)
|
||||
}
|
||||
} else {
|
||||
handleUpdate <- nil
|
||||
}
|
||||
}
|
||||
|
||||
func getLatestVersion() (string, error) {
|
||||
func getLatestRelease() (*releaseInfo, error) {
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", nwo)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var parsedData struct {
|
||||
LatestVersion string `json:"tag_name"`
|
||||
}
|
||||
|
||||
json.Unmarshal(data, &parsedData)
|
||||
return parsedData.LatestVersion, nil
|
||||
var r releaseInfo
|
||||
json.Unmarshal(data, &r)
|
||||
return r, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue