diff --git a/.hof/Cli/cmd/mvs/cmd/root.go b/.hof/Cli/cmd/mvs/cmd/root.go index 34f4917..2c3fe66 100644 --- a/.hof/Cli/cmd/mvs/cmd/root.go +++ b/.hof/Cli/cmd/mvs/cmd/root.go @@ -34,6 +34,13 @@ func RootPersistentPreRun(args []string) (err error) { return err } +func RootPersistentPostRun(args []string) (err error) { + + PrintUpdateAvailable() + + return err +} + var RootCmd = &cobra.Command{ Use: "mvs", @@ -59,6 +66,18 @@ var RootCmd = &cobra.Command{ ga.SendGaEvent("root", "", 0) }, + + PersistentPostRun: func(cmd *cobra.Command, args []string) { + var err error + + // Argument Parsing + + err = RootPersistentPostRun(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, } func init() { diff --git a/.hof/Cli/cmd/mvs/cmd/update.go b/.hof/Cli/cmd/mvs/cmd/update.go index 8925e46..0eb856f 100644 --- a/.hof/Cli/cmd/mvs/cmd/update.go +++ b/.hof/Cli/cmd/mvs/cmd/update.go @@ -21,7 +21,8 @@ import ( var UpdateLong = `Print the build version for mvs` var ( - UpdateCheckFlag bool + UpdateCheckFlag bool + UpdateVersionFlag string UpdateStarted bool UpdateErrored bool @@ -32,10 +33,11 @@ var ( func init() { UpdateCmd.Flags().BoolVarP(&UpdateCheckFlag, "check", "", false, "set to only check for an update") + UpdateCmd.Flags().StringVarP(&UpdateVersionFlag, "version", "v", "", "the version to update to") } const updateMessage = ` -Updates available. v%s -> %s (latest) +Updates available. v%s -> %s run 'mvs update' to get the latest. @@ -53,6 +55,7 @@ var UpdateCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { ga.SendGaEvent("update", "", 0) + }, Run: func(cmd *cobra.Command, args []string) { @@ -65,7 +68,7 @@ var UpdateCmd = &cobra.Command{ // Semver Check? cur := ProgramVersion{Version: "v" + verinfo.Version} - if latest.Version == cur.Version || cur.Version == "vLocal" { + if latest.Version == cur.Version || (UpdateVersionFlag == "" && cur.Version == "vLocal") { return } else { if UpdateCheckFlag { @@ -73,7 +76,7 @@ var UpdateCmd = &cobra.Command{ } } - err = InstallLatest() + err = InstallUpdate() if err != nil { fmt.Println(err) os.Exit(-1) @@ -100,11 +103,16 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { cur := ProgramVersion{Version: "v" + verinfo.Version} checkURL := "https://api.github.com/repos/hofstadter-io/mvs/releases/latest" + if UpdateVersionFlag != "" { + checkURL = "https://api.github.com/repos/hofstadter-io/mvs/releases/tags/" + UpdateVersionFlag + manual = true + } - req := gorequest.New().Get(checkURL). - Query("current=" + cur.Version). - Query("manual=" + fmt.Sprint(manual)) - resp, b, errs := req.EndBytes() + req := gorequest.New() + if os.Getenv("GITHUB_TOKEN") != "" { + req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN")) + } + resp, b, errs := req.Get(checkURL).EndBytes() UpdateErrored = true check := "http2: server sent GOAWAY and closed the connection" @@ -118,6 +126,7 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { } if resp.StatusCode >= 400 { if resp.StatusCode == 404 { + fmt.Println("404?!", checkURL) return ver, fmt.Errorf("No releases available :[") } return ver, fmt.Errorf("Bad Request: " + string(b)) @@ -157,7 +166,7 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { UpdateChecked = true // Semver Check? - if ver.Version != cur.Version && cur.Version != "vLocal" { + if ver.Version != cur.Version && (manual || cur.Version != "vLocal") { UpdateAvailable = &ver aI, ok := gh["assets"] if ok { @@ -184,7 +193,7 @@ func PrintUpdateAvailable() { } } -func InstallLatest() (err error) { +func InstallUpdate() (err error) { fmt.Printf("Installing mvs@%s\n", UpdateAvailable.Version) if UpdateData == nil { @@ -263,8 +272,12 @@ func InstallLatest() (err error) { } func downloadAndInstall(url string) error { - req := gorequest.New().Get(url) - resp, content, errs := req.EndBytes() + req := gorequest.New() + if os.Getenv("GITHUB_TOKEN") != "" { + req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN")) + } + + resp, content, errs := req.Get(url).EndBytes() check := "http2: server sent GOAWAY and closed the connection" if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) { diff --git a/cmd/mvs/cmd/root.go b/cmd/mvs/cmd/root.go index 34f4917..2c3fe66 100644 --- a/cmd/mvs/cmd/root.go +++ b/cmd/mvs/cmd/root.go @@ -34,6 +34,13 @@ func RootPersistentPreRun(args []string) (err error) { return err } +func RootPersistentPostRun(args []string) (err error) { + + PrintUpdateAvailable() + + return err +} + var RootCmd = &cobra.Command{ Use: "mvs", @@ -59,6 +66,18 @@ var RootCmd = &cobra.Command{ ga.SendGaEvent("root", "", 0) }, + + PersistentPostRun: func(cmd *cobra.Command, args []string) { + var err error + + // Argument Parsing + + err = RootPersistentPostRun(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, } func init() { diff --git a/cmd/mvs/cmd/update.go b/cmd/mvs/cmd/update.go index 8925e46..0eb856f 100644 --- a/cmd/mvs/cmd/update.go +++ b/cmd/mvs/cmd/update.go @@ -21,7 +21,8 @@ import ( var UpdateLong = `Print the build version for mvs` var ( - UpdateCheckFlag bool + UpdateCheckFlag bool + UpdateVersionFlag string UpdateStarted bool UpdateErrored bool @@ -32,10 +33,11 @@ var ( func init() { UpdateCmd.Flags().BoolVarP(&UpdateCheckFlag, "check", "", false, "set to only check for an update") + UpdateCmd.Flags().StringVarP(&UpdateVersionFlag, "version", "v", "", "the version to update to") } const updateMessage = ` -Updates available. v%s -> %s (latest) +Updates available. v%s -> %s run 'mvs update' to get the latest. @@ -53,6 +55,7 @@ var UpdateCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { ga.SendGaEvent("update", "", 0) + }, Run: func(cmd *cobra.Command, args []string) { @@ -65,7 +68,7 @@ var UpdateCmd = &cobra.Command{ // Semver Check? cur := ProgramVersion{Version: "v" + verinfo.Version} - if latest.Version == cur.Version || cur.Version == "vLocal" { + if latest.Version == cur.Version || (UpdateVersionFlag == "" && cur.Version == "vLocal") { return } else { if UpdateCheckFlag { @@ -73,7 +76,7 @@ var UpdateCmd = &cobra.Command{ } } - err = InstallLatest() + err = InstallUpdate() if err != nil { fmt.Println(err) os.Exit(-1) @@ -100,11 +103,16 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { cur := ProgramVersion{Version: "v" + verinfo.Version} checkURL := "https://api.github.com/repos/hofstadter-io/mvs/releases/latest" + if UpdateVersionFlag != "" { + checkURL = "https://api.github.com/repos/hofstadter-io/mvs/releases/tags/" + UpdateVersionFlag + manual = true + } - req := gorequest.New().Get(checkURL). - Query("current=" + cur.Version). - Query("manual=" + fmt.Sprint(manual)) - resp, b, errs := req.EndBytes() + req := gorequest.New() + if os.Getenv("GITHUB_TOKEN") != "" { + req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN")) + } + resp, b, errs := req.Get(checkURL).EndBytes() UpdateErrored = true check := "http2: server sent GOAWAY and closed the connection" @@ -118,6 +126,7 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { } if resp.StatusCode >= 400 { if resp.StatusCode == 404 { + fmt.Println("404?!", checkURL) return ver, fmt.Errorf("No releases available :[") } return ver, fmt.Errorf("Bad Request: " + string(b)) @@ -157,7 +166,7 @@ func CheckUpdate(manual bool) (ver ProgramVersion, err error) { UpdateChecked = true // Semver Check? - if ver.Version != cur.Version && cur.Version != "vLocal" { + if ver.Version != cur.Version && (manual || cur.Version != "vLocal") { UpdateAvailable = &ver aI, ok := gh["assets"] if ok { @@ -184,7 +193,7 @@ func PrintUpdateAvailable() { } } -func InstallLatest() (err error) { +func InstallUpdate() (err error) { fmt.Printf("Installing mvs@%s\n", UpdateAvailable.Version) if UpdateData == nil { @@ -263,8 +272,12 @@ func InstallLatest() (err error) { } func downloadAndInstall(url string) error { - req := gorequest.New().Get(url) - resp, content, errs := req.EndBytes() + req := gorequest.New() + if os.Getenv("GITHUB_TOKEN") != "" { + req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN")) + } + + resp, content, errs := req.Get(url).EndBytes() check := "http2: server sent GOAWAY and closed the connection" if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) { diff --git a/cue.mods b/cue.mods index 8b4a986..74962c0 100644 --- a/cue.mods +++ b/cue.mods @@ -3,5 +3,5 @@ module github.com/hofstadter-io/mvs cue master require ( - github.com/hofstadter-io/hofmod-cli v0.5.4 + github.com/hofstadter-io/hofmod-cli v0.5.5 ) diff --git a/cue.sums b/cue.sums index 7b570d0..33f2127 100644 --- a/cue.sums +++ b/cue.sums @@ -32,3 +32,5 @@ github.com/hofstadter-io/hofmod-cli v0.5.3 h1:9QcITvHncO6lWHezRnM1YXkVTula6sfsHE github.com/hofstadter-io/hofmod-cli v0.5.3/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU= github.com/hofstadter-io/hofmod-cli v0.5.4 h1:bO72JKO70dnmRBOV/c9IbTXQZuhsParhuwEhtL7+EdU= github.com/hofstadter-io/hofmod-cli v0.5.4/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU= +github.com/hofstadter-io/hofmod-cli v0.5.5 h1:U39psc0wrKEEdYiNZ5+9b+50PyxFLCeQ7b3mC64SBas= +github.com/hofstadter-io/hofmod-cli v0.5.5/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU=