/
checkVersion.go
67 lines (53 loc) · 1.36 KB
/
checkVersion.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
package utils
import (
"io/ioutil"
"net/http"
"regexp"
"github.com/CarlFlo/blacklisterBot/src/config"
"github.com/CarlFlo/malm"
)
func CheckVersion(currentVersion string) {
// Handles checking if there is an update available for the bot
upToDate, githubVersion, err := botVersonHandler(currentVersion)
if err != nil {
malm.Error("%s", err)
}
if upToDate {
malm.Debug("Version %s", currentVersion)
} else {
malm.Info("New version available at '%s'! New version: '%s'; Your version: '%s'",
config.CONFIG.BotInfo.DepositURL,
githubVersion,
currentVersion)
}
}
// Return true or false if the version is up to date
// Return version on system
// Return version on github
// return error
func botVersonHandler(current string) (bool, string, error) {
githubVersion, err := githubVersion()
if err != nil {
return false, "", err
}
upToDate := current == githubVersion
return upToDate, githubVersion, nil
}
// Returns the online version or the error
func githubVersion() (string, error) {
// get URL
resp, err := http.Get(config.CONFIG.BotInfo.VersionURL)
if err != nil {
return "", err
}
// read response
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
// regex to find version
pattern := regexp.MustCompile(`\d+-\d+-\d+`)
version := pattern.FindString(string(body))
return version, nil
}