1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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/smolbote/version")
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")
}
}
|