From 1ff1738d30e7328fd5057f1a4e0c4dae0e13d698 Mon Sep 17 00:00:00 2001 From: Adrian Ho Date: Fri, 31 Jul 2020 20:28:53 +0800 Subject: [PATCH] Check for newer release on GitHub Added a command-line flag (--check-update) for scripted use, and prompts in the welcome message in the REPL. Uses https://github.com/tcnksm/go-latest to do the heavy lifting. Closes #334. --- main.go | 10 ++++++++++ repl/repl.go | 9 +++++++++ util/check_update.go | 30 ++++++++++++++++++++++++++++++ util/check_update_test.go | 12 ++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 util/check_update.go create mode 100644 util/check_update_test.go diff --git a/main.go b/main.go index aa8f92b0..c7e6b9e4 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/abs-lang/abs/install" "github.com/abs-lang/abs/repl" + "github.com/abs-lang/abs/util" ) // Version of the ABS interpreter @@ -19,6 +20,15 @@ func main() { return } + if len(args) == 2 && args[1] == "--check-update" { + if newver, update := util.UpdateAvailable(Version); update { + fmt.Printf("Update available: %s (you have %s)\n", newver, Version) + os.Exit(1) + } else { + return + } + } + if len(args) == 3 && args[1] == "get" { install.Install(args[2]) return diff --git a/repl/repl.go b/repl/repl.go index 4d3ddf37..359ec1e7 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -1,9 +1,11 @@ package repl import ( + "crypto/rand" "fmt" "io" "io/ioutil" + "math/big" "os" "os/user" "path/filepath" @@ -228,6 +230,13 @@ func BeginRepl(args []string, version string) { panic(err) } fmt.Printf("Hello %s, welcome to the ABS (%s) programming language!\n", user.Username, version) + // check for new version about 10% of the time, + // to avoid too many hangups + if r, e := rand.Int(rand.Reader, big.NewInt(100)); e == nil && r.Int64() < 10 { + if newver, update := util.UpdateAvailable(version); update { + fmt.Printf("*** Update available: %s ***\n", newver) + } + } fmt.Printf("Type 'quit' when you're done, 'help' if you get lost!\n") Start(os.Stdin, os.Stdout) } else { diff --git a/util/check_update.go b/util/check_update.go new file mode 100644 index 00000000..5fa278d5 --- /dev/null +++ b/util/check_update.go @@ -0,0 +1,30 @@ +package util + +import ( + "io/ioutil" + "net/http" + "strings" +) + +const verUrl = "https://raw.githubusercontent.com/abs-lang/abs/master/VERSION" + +// Returns latest version, plus "new version available?" bool +func UpdateAvailable(version string) (string, bool) { + resp, err := http.Get(verUrl) + if err != nil { + return version, false + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return version, false + } + + latest := strings.TrimSpace(string(body)) + if version != latest { + return latest, true + } + + return version, false +} diff --git a/util/check_update_test.go b/util/check_update_test.go new file mode 100644 index 00000000..6b3294c3 --- /dev/null +++ b/util/check_update_test.go @@ -0,0 +1,12 @@ +package util + +import ( + "testing" +) + +func TestUpdateAvailable(t *testing.T) { + _, outdated := UpdateAvailable("1.0") + if !outdated { + t.Fatalf("expected 1.0 to be outdated") + } +}