-
Notifications
You must be signed in to change notification settings - Fork 76
/
upgrader.go
70 lines (57 loc) · 1.39 KB
/
upgrader.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
package upgrader
import (
"context"
"fmt"
"time"
"github.com/abahmed/kwatch/alertmanager"
"github.com/abahmed/kwatch/config"
"github.com/abahmed/kwatch/constant"
"github.com/abahmed/kwatch/version"
"github.com/google/go-github/v41/github"
"github.com/sirupsen/logrus"
)
type Upgrader struct {
config *config.Upgrader
alertManager *alertmanager.AlertManager
}
// NewUpgrader returns new instance of upgrader
func NewUpgrader(config *config.Upgrader,
alertManager *alertmanager.AlertManager) *Upgrader {
return &Upgrader{
config: config,
alertManager: alertManager,
}
}
// CheckUpdates checks every 24 hours if a newer version of Kwatch is available
func (u *Upgrader) CheckUpdates() {
if u.config.DisableUpdateCheck ||
version.Short() == "dev" {
return
}
// check at startup
u.checkRelease()
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for range ticker.C {
u.checkRelease()
}
}
func (u *Upgrader) checkRelease() {
client := github.NewClient(nil)
r, _, err := client.Repositories.GetLatestRelease(
context.TODO(),
"abahmed",
"kwatch")
if err != nil {
logrus.Warnf("failed to get latest release: %s", err.Error())
return
}
if r.TagName == nil {
logrus.Warnf("failed to get release tag: %+v", r)
return
}
if version.Short() == *r.TagName {
return
}
u.alertManager.Notify(fmt.Sprintf(constant.KwatchUpdateMsg, *r.TagName))
}