diff --git a/nex/nex.go b/nex/nex.go index 1f1bbcaf..78c923a5 100644 --- a/nex/nex.go +++ b/nex/nex.go @@ -58,6 +58,8 @@ var ( ) func init() { + _ = versionCheck() + ncli.Flag("server", "NATS server urls").Short('s').Envar("NATS_URL").PlaceHolder("URL").StringVar(&Opts.Servers) ncli.Flag("user", "Username or Token").Envar("NATS_USER").PlaceHolder("USER").StringVar(&Opts.Username) ncli.Flag("password", "Password").Envar("NATS_PASSWORD").PlaceHolder("PASSWORD").StringVar(&Opts.Password) diff --git a/nex/update.go b/nex/update.go new file mode 100644 index 00000000..92cc25a5 --- /dev/null +++ b/nex/update.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "net/http" +) + +func versionCheck() error { + if VERSION == "development" { + return nil + } + + res, err := http.Get("https://api.github.com/repos/synadia-io/nex/releases/latest") + if err != nil { + return err + } + defer res.Body.Close() + + b, err := io.ReadAll(res.Body) + if err != nil { + return err + } + + payload := make(map[string]interface{}) + err = json.Unmarshal(b, &payload) + if err != nil { + return err + } + + latestTag, ok := payload["tag_name"].(string) + if !ok { + return errors.New("error parsing tag_name") + } + + if latestTag != VERSION { + fmt.Printf(`================================================================ +🎉 There is a newer version [v%s] of the NEX CLI available 🎉 +To update, run: + curl -sSf https://nex.synadia.com/install.sh | sh +================================================================ + +`, + latestTag) + } + + return nil +}