-
Notifications
You must be signed in to change notification settings - Fork 362
/
update-check.go
107 lines (86 loc) · 2.63 KB
/
update-check.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package commands
import (
"context"
"fmt"
"strings"
"time"
"github.com/activecm/rita/config"
"github.com/activecm/rita/resources"
"github.com/blang/semver"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
//Strings used for informing the user of a new version.
var informFmtStr = "\nTheres a new %s version of RITA %s available at:\nhttps://github.com/activecm/rita/releases\n"
var versions = []string{"Major", "Minor", "Patch"}
//GetVersionPrinter prints the version info
func GetVersionPrinter() func(*cli.Context) {
return func(c *cli.Context) {
fmt.Printf("%s version %s\n", c.App.Name, c.App.Version)
fmt.Println(updateCheck(c.String("config")))
}
}
// UpdateCheck Performs a check for the new version of RITA against the git repository and
//returns a string indicating the new version if available
func updateCheck(configFile string) string {
res := resources.InitResources(configFile)
delta := res.Config.S.UserConfig.UpdateCheckFrequency
var newVersion semver.Version
var err error
var timestamp time.Time
if delta <= 0 {
return ""
}
//Check Logs for Versioning
m := res.MetaDB
timestamp, newVersion = m.LastCheck()
days := time.Now().Sub(timestamp).Hours() / 24
if days > float64(delta) {
newVersion, err = getRemoteVersion()
if err != nil {
return ""
}
//Log checked version.
res.Log.WithFields(log.Fields{
"Message": "Checking versions...",
"LastUpdateCheck": time.Now(),
"NewestVersion": fmt.Sprint(newVersion),
}).Info("Checking for new version")
}
configVersion, err := semver.ParseTolerant(config.Version)
if err != nil {
return ""
}
if newVersion.GT(configVersion) {
return informUser(configVersion, newVersion)
}
return ""
}
// Returns the first index where v1 is greater than v2
func versionDiffIndex(v1 semver.Version, v2 semver.Version) int {
if v1.Major > v2.Major {
return 0
}
if v1.Minor > v2.Minor {
return 1
}
return 2
}
func getRemoteVersion() (semver.Version, error) {
client := github.NewClient(nil)
refs, _, err := client.Git.GetRefs(context.Background(), "activecm", "rita", "refs/tags/v")
if err == nil {
s := strings.TrimPrefix(*refs[len(refs)-1].Ref, "refs/tags/")
return semver.ParseTolerant(s)
}
return semver.Version{}, err
}
// Assembles a notice for the user informing them of an upgrade.
// The return value is printed regardless so, "" is returned on errror.
//func informUser( verStr string, index int ) string {
func informUser(local semver.Version, remote semver.Version) string {
return fmt.Sprintf(informFmtStr,
versions[versionDiffIndex(remote, local)],
fmt.Sprint(remote))
}