mirror of https://github.com/rwxrob/bonzai
parent
8366d286b8
commit
a9acfa8a91
@ -1,44 +0,0 @@
|
||||
package Z
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/rwxrob/json"
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
// CompareUpdated compares the current isosec integer (second at GMT) to
|
||||
// that retrieved from the URL (usually pointing to a file called
|
||||
// UPDATED) which must return nothing but a single isosec integer (which
|
||||
// is unmarshaled as a JSON number). Returns 1 if current is more
|
||||
// recent, 0 if they are equal, -1 if current is older, and -2 if unable
|
||||
// to determine (which will also log any error encountered).
|
||||
func CompareUpdated(current int, remoteURL string) int {
|
||||
var remote int
|
||||
if err := json.Req(`GET`, remoteURL, nil, nil, &remote); err != nil {
|
||||
log.Print(err)
|
||||
return -2
|
||||
}
|
||||
if remote == current {
|
||||
return 0
|
||||
}
|
||||
if remote > current {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// CompareVersions compares the current semantic version string to that
|
||||
// retrieved from the URL (usually pointing to a file called VERSION)
|
||||
// which must return nothing but a single semantic version. Like
|
||||
// semver.Compare returns 0 if they are the same, 1 if current is more
|
||||
// recent, -1 if current is behind remote, and -2 if unable to retrieve
|
||||
// the remote semver string (in which case an error is also logged).
|
||||
func CompareVersions(current, remoteURL string) int {
|
||||
var remote string
|
||||
if err := json.Req(`GET`, remoteURL, nil, nil, &remote); err != nil {
|
||||
log.Print(err)
|
||||
return -2
|
||||
}
|
||||
return semver.Compare(current, remote)
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package Z_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
ht "net/http/httptest"
|
||||
|
||||
Z "github.com/rwxrob/bonzai/z"
|
||||
)
|
||||
|
||||
func ExampleCompareUpdated() {
|
||||
|
||||
handler := http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `20220322080540`)
|
||||
})
|
||||
older := ht.NewServer(handler)
|
||||
defer older.Close()
|
||||
|
||||
handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `20220322080546`)
|
||||
})
|
||||
newer := ht.NewServer(handler)
|
||||
defer newer.Close()
|
||||
|
||||
handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `20220322080542`)
|
||||
})
|
||||
same := ht.NewServer(handler)
|
||||
defer same.Close()
|
||||
|
||||
fmt.Println(Z.CompareUpdated(20220322080542, older.URL))
|
||||
fmt.Println(Z.CompareUpdated(20220322080542, newer.URL))
|
||||
fmt.Println(Z.CompareUpdated(20220322080542, same.URL))
|
||||
fmt.Println(Z.CompareUpdated(20220322080542, "foobar"))
|
||||
|
||||
// Output:
|
||||
// -1
|
||||
// 1
|
||||
// 0
|
||||
// -2
|
||||
}
|
||||
|
||||
func ExampleCompareVersions() {
|
||||
|
||||
handler := http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `"v0.0.1"`)
|
||||
})
|
||||
older := ht.NewServer(handler)
|
||||
defer older.Close()
|
||||
|
||||
handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `"v0.1.0"`)
|
||||
})
|
||||
newer := ht.NewServer(handler)
|
||||
defer newer.Close()
|
||||
|
||||
handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, `"v0.0.2"`)
|
||||
})
|
||||
same := ht.NewServer(handler)
|
||||
defer same.Close()
|
||||
|
||||
fmt.Println(Z.CompareVersions(`v0.0.2`, older.URL))
|
||||
fmt.Println(Z.CompareVersions(`v0.0.2`, newer.URL))
|
||||
fmt.Println(Z.CompareVersions(`v0.0.2`, same.URL))
|
||||
fmt.Println(Z.CompareVersions(`v0.0.2`, "foobar"))
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// -1
|
||||
// 0
|
||||
// -2
|
||||
}
|
Loading…
Reference in New Issue