-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheckForUpdates.go
65 lines (53 loc) · 1.5 KB
/
checkForUpdates.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"golang.org/x/mod/semver"
)
type Release struct {
TagName string `json:"tag_name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
HtmlUrl string `json:"html_url"`
}
func checkForUpdates() {
currentSemVer := "v5.1.1"
logM(1, "Checking for Updates...")
endpointUrl := "https://api.github.com/repos/Matamata-Animator/Matamata/releases?per_page=1"
client := &http.Client{}
req, err := http.NewRequest("GET", endpointUrl, nil)
if err != nil {
fmt.Println("Errored when creating request object", err)
fmt.Println("Continuing...")
return
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Errored when checking for updates.")
fmt.Println(err)
fmt.Println("Continuing...")
return
}
defer resp.Body.Close()
resp_body, _ := io.ReadAll(resp.Body)
res_str := string(resp_body)
res_str = res_str[1 : len(res_str)-1]
release := Release{}
err = json.Unmarshal([]byte(res_str), &release)
if err != nil {
fmt.Printf("Error while parsing data: %s", err)
fmt.Println("Continuing...")
return
}
if release.Draft || release.Prerelease {
return
}
if semver.Compare(currentSemVer, release.TagName) < 0 {
colorReset := "\033[0m"
colorRed := "\033[31m"
fmt.Println(string(colorRed)+"New version available! ("+currentSemVer+" -> "+release.TagName+") You can download it from", release.HtmlUrl, string(colorReset))
}
}