diff --git a/command/checkUpdate.go b/command/checkUpdate.go new file mode 100644 index 000000000..f0afe9175 --- /dev/null +++ b/command/checkUpdate.go @@ -0,0 +1,47 @@ +package command + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + + "github.com/mattn/go-isatty" +) + +func CheckForUpdate() error { + if !isatty.IsTerminal(os.Stdout.Fd()) { + return nil + } + + latestVersion, err := getLastestVersion() + if err != nil { + return err + } + + fmt.Printf("🌭 %+v %+v\n", latestVersion, Version) + + return nil +} + +func getLastestVersion() (string, error) { + url := "https://api.github.com/repos/github/homebrew-gh/releases/latest" + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var j struct { + LatestVersion string `json:"tag_name"` + } + + json.Unmarshal(data, &j) + return j.LatestVersion, nil +} diff --git a/main.go b/main.go index e7f94a5d0..daa91cc87 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( ) func main() { + command.CheckForUpdate() + if cmd, err := command.RootCmd.ExecuteC(); err != nil { fmt.Fprintln(os.Stderr, err) _, isFlagError := err.(command.FlagError)