/
version.go
82 lines (70 loc) · 1.66 KB
/
version.go
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
)
func versionPrint() error {
logger.Infof("starting Commento %s", version)
return nil
}
func versionCheckStart() error {
go func() {
printedError := false
errorCount := 0
latestSeen := ""
for {
time.Sleep(5 * time.Minute)
data := url.Values{
"version": {version},
}
resp, err := http.Post("https://version.commento.io/api/check", "application/x-www-form-urlencoded", bytes.NewBufferString(data.Encode()))
if err != nil {
errorCount++
// print the error only once; we don't want to spam the logs with this
// every five minutes
if !printedError && errorCount > 5 {
logger.Errorf("error checking version: %v", err)
printedError = true
}
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
errorCount++
if !printedError && errorCount > 5 {
logger.Errorf("error reading body: %s", err)
printedError = true
}
continue
}
type response struct {
Success bool `json:"success"`
Message string `json:"message"`
Latest string `json:"latest"`
NewUpdate bool `json:"newUpdate"`
}
r := response{}
json.Unmarshal(body, &r)
if r.Success == false {
errorCount++
if !printedError && errorCount > 5 {
logger.Errorf("error checking version: %s", r.Message)
printedError = true
}
continue
}
if r.NewUpdate && r.Latest != latestSeen {
logger.Infof("New update available! Latest version: %s", r.Latest)
latestSeen = r.Latest
}
errorCount = 0
printedError = false
}
}()
return nil
}