aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/updater/updater.go145
1 files changed, 145 insertions, 0 deletions
diff --git a/tools/updater/updater.go b/tools/updater/updater.go
new file mode 100644
index 0000000..ec778a3
--- /dev/null
+++ b/tools/updater/updater.go
@@ -0,0 +1,145 @@
+package main
+
+import (
+ "fmt"
+ "strings"
+ "flag"
+ "errors"
+ "net/http"
+ "os"
+ "io"
+ "io/ioutil"
+ "encoding/json"
+)
+
+type VersionMeta struct {
+ Poi VersionData `json:"poi"`
+}
+
+type VersionData struct {
+ Version string `json:"version"`
+ Patch int `json:"patch"`
+ Commit string `json:"commit"`
+}
+
+func parseJson(name string) (ver VersionMeta, err error) {
+ var reader io.Reader
+
+ if strings.HasPrefix(name, "https://") {
+ // Grabbing stuff over https
+ resp, e := http.Get(name)
+ if resp.StatusCode != 200 {
+ e = errors.New("HTTP response not OK")
+ }
+ if e != nil {
+ fmt.Println("| Error encountered getting version.json: ", e)
+ }
+ defer resp.Body.Close()
+ reader = resp.Body
+ err = e
+
+ } else {
+ // FIXME Assuming we're grabbing a local file
+ r, e := os.Open(name)
+ if e != nil {
+ fmt.Println("| Error opening file: ", e)
+ }
+ defer r.Close()
+ reader = r
+ err = e
+ }
+
+ if err == nil {
+ body, err := ioutil.ReadAll(reader)
+ if err != nil {
+ fmt.Println("| Error encountered reading body")
+ }
+
+ err = json.Unmarshal(body, &ver)
+ }
+
+ return
+}
+
+// left > right r -> 1
+// left = right r -> 0
+// left < right r -> -1
+func compareVersion(left, right string) (ret int) {
+ if strings.Compare(left, right) == 0 {
+ ret = 0
+ return
+ }
+
+ l := strings.Split(left, ".")
+ r := strings.Split(right, ".")
+
+ for i := 0; i < 3; i++ {
+ if l[i] > r[i] {
+ ret = 1
+ break
+ } else if l[i] < r[i] {
+ ret = -1
+ break
+ }
+ }
+
+ return
+}
+
+func main() {
+ fmt.Println("[ smolbote updater ]")
+
+ // read local version.json
+ localFlag := flag.Bool("local", true, "Read local version.json")
+ // read remote version.json
+ remoteFlag := flag.Bool("remote", true, "Read remote version.json")
+ // download update
+ downloadFlag := flag.Bool("download", false, "Download update")
+ // install update
+ installFlag := flag.Bool("install", false, "Install update")
+
+ flag.Parse()
+
+ var local VersionMeta
+ var remote VersionMeta
+ var err error
+
+ if *localFlag {
+ fmt.Println("+ Reading local version.json")
+ local, err = parseJson("version.json")
+ if err == nil {
+ fmt.Println(fmt.Sprintf("| Got poi [%s-%d-%s]", local.Poi.Version, local.Poi.Patch, local.Poi.Commit))
+ } else {
+ // there has been an error reading the local version.json
+ *localFlag = false
+ }
+ fmt.Println("\\ done")
+ }
+
+ if *remoteFlag {
+ fmt.Println("+ Reading remote version.json")
+ remote, err = parseJson("https://neueland.iserlohn-fortress.net/version.json")
+ if err == nil {
+ fmt.Println(fmt.Sprintf("| Got poi [%s-%d-%s]", remote.Poi.Version, remote.Poi.Patch, remote.Poi.Commit))
+ }
+
+ comp := compareVersion(local.Poi.Version, remote.Poi.Version)
+ if comp >= 0 {
+ fmt.Println("| poi up to poi")
+ } else { // comp < 0
+ fmt.Println("| poi out of poi")
+ }
+
+ fmt.Println("\\ done")
+ }
+
+ if *downloadFlag {
+ fmt.Println("+ Downloading update")
+ fmt.Println("\\ done")
+ }
+
+ if *installFlag {
+ fmt.Println("+ Installing update")
+ fmt.Println("\\ done")
+ }
+}