cli/update/update.go
Mislav Marohnić 67f0cf3ce3 Improvements to update notifier
- Only report an update available if the version number of the release
  is greater than the current version

- Removes `command` dependency from `update` package; instead, pass
  current version as an argument

- Remove `brew upgrade` instructions since we can't be certain that gh
  was installed via Homebrew in the first place.

- Does not check for updates unless stderr is a tty

- Preserve stderr color output even if stdout is not a tty

- Fixes stderr color output on Windows
2019-12-04 14:47:27 +01:00

36 lines
872 B
Go

package update
import (
"fmt"
"github.com/github/gh-cli/api"
"github.com/hashicorp/go-version"
)
// ReleaseInfo stores information about a release
type ReleaseInfo struct {
Version string `json:"tag_name"`
URL string `json:"html_url"`
}
// CheckForUpdate checks whether this software has had a newer relase on GitHub
func CheckForUpdate(client *api.Client, repo, currentVersion string) (*ReleaseInfo, error) {
latestRelease := ReleaseInfo{}
err := client.REST("GET", fmt.Sprintf("repos/%s/releases/latest", repo), nil, &latestRelease)
if err != nil {
return nil, err
}
if versionGreaterThan(latestRelease.Version, currentVersion) {
return &latestRelease, nil
}
return nil, nil
}
func versionGreaterThan(v, w string) bool {
vv, ve := version.NewVersion(v)
vw, we := version.NewVersion(w)
return ve == nil && we == nil && vv.GreaterThan(vw)
}