Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for newer release on GitHub #392

Merged
merged 1 commit into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package repl

import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions util/check_update.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions util/check_update_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}