This repository has been archived by the owner on Aug 8, 2021. It is now read-only.
forked from Bios-Marcel/cordless
-
Notifications
You must be signed in to change notification settings - Fork 6
/
check.go
60 lines (51 loc) · 2.02 KB
/
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
package version
import (
"context"
"fmt"
"github.com/google/go-github/v29/github"
)
var latestRemoteVersion string
// CheckForUpdate checks whether the Version-string saved at version.Version
// is different to the name of the latest tagged GitHub release. If it is, and
// the release name isn't equal to the `donRemindFor`-parameter, we return
// true, which stands for "update available". This allows the user to manually
// say "Don't remind me again for this version". The actual return value is
// supplied via a channel that's closed after one value has been read.
func CheckForUpdate(dontRemindFor string) chan bool {
//Note, this isn't buffered, so that we can safely close the channel
//when it has been read by the caller.
updateAvailableChannel := make(chan bool)
go func() {
remoteVersion := GetLatestRemoteVersion()
if remoteVersion == "" || remoteVersion == dontRemindFor {
//Error retrieving version or user wishes to ignore update.
updateAvailableChannel <- false
} else {
updateAvailableChannel <- isLocalOlderThanRemote(Version, remoteVersion)
}
close(updateAvailableChannel)
}()
return updateAvailableChannel
}
func isLocalOlderThanRemote(local, remote string) bool {
yearRemote, monthRemote, dayRemote := parseTag(remote)
yearLocal, monthLocal, dayLocal := parseTag(local)
return !(yearLocal >= yearRemote && monthLocal >= monthRemote && dayLocal >= dayRemote)
}
func parseTag(tag string) (year int, month int, day int) {
fmt.Sscanf(tag, "%04d-%02d-%02d", &year, &month, &day)
return
}
// GetLatestRemoteVersion queries GitHub for the latest Release-Tag and caches
// it. This value will never be updated during runtime.
func GetLatestRemoteVersion() string {
if latestRemoteVersion != "" {
return latestRemoteVersion
}
repositoryRelease, _, lookupError := github.NewClient(nil).Repositories.GetLatestRelease(context.Background(), "yellowsink", "gord")
if lookupError != nil || repositoryRelease == nil {
return ""
}
latestRemoteVersion = *repositoryRelease.TagName
return latestRemoteVersion
}