-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcheck_updates.go
65 lines (53 loc) · 1.28 KB
/
check_updates.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 ui
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/NHAS/wag/internal/config"
"github.com/NHAS/wag/internal/data"
)
type githubResponse struct {
Body string
Prerelease bool `json:"prerelease"`
TagName string `json:"tag_name"`
Published string `json:"published_at"`
Url string `json:"html_url"`
}
type Update struct {
New bool
UpdateVersion string
UpdateMessage []string
Url string
Released string
}
var (
mostRecentUpdate *Update
lastChecked time.Time
)
func getUpdate() Update {
should, err := data.CheckUpdates()
if err != nil || !should {
return Update{}
}
if time.Now().After(lastChecked.Add(15*time.Minute)) || mostRecentUpdate == nil {
resp, err := http.Get("https://api.github.com/repos/NHAS/wag/releases/latest")
if err != nil {
return Update{}
}
defer resp.Body.Close()
var gr githubResponse
err = json.NewDecoder(resp.Body).Decode(&gr)
if err != nil {
return Update{}
}
mostRecentUpdate = &Update{
UpdateVersion: gr.TagName,
UpdateMessage: strings.Split(gr.Body, "\r\n"),
Url: gr.Url,
Released: gr.Published,
}
}
mostRecentUpdate.New = strings.Split(config.Version, "-")[0] != mostRecentUpdate.UpdateVersion
return *mostRecentUpdate
}