forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest_version.go
111 lines (102 loc) · 2.68 KB
/
latest_version.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
108
109
110
111
package kr
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/blang/semver"
"github.com/op/go-logging"
"github.com/youtube/vitess/go/ioutil2"
)
var VERSIONS_S3_BUCKET = "https://s3.amazonaws.com/kr-versions/versions"
type Versions struct {
IOS string `json:"iOS"`
OSX string `json:"osx"`
Linux string `json:"linux"`
}
func GetLatestVersions() (versions Versions, err error) {
httpClient := http.Client{
Timeout: 5 * time.Second,
}
resp, err := httpClient.Get(VERSIONS_S3_BUCKET)
if err != nil {
return
}
defer resp.Body.Close()
versionsJson, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(versionsJson, &versions)
if err != nil {
return
}
cacheLatestVersions(versionsJson)
return
}
func cacheLatestVersions(versionsJson []byte) {
file, fileErr := KrDirFile("latest_versions_cache")
if fileErr != nil {
log.Error("Error finding home directory:", fileErr.Error())
return
}
if writeErr := ioutil2.WriteFileAtomic(file, versionsJson, 0700); writeErr != nil {
log.Error("Error writing update log file:", writeErr.Error())
}
}
func GetCachedLatestVersions() (versions Versions, err error) {
cacheFile, err := KrDirFile("latest_versions_cache")
if err != nil {
return
}
cacheBytes, err := ioutil.ReadFile(cacheFile)
if err != nil {
return
}
err = json.Unmarshal(cacheBytes, &versions)
return
}
func CheckedForUpdateRecently(log *logging.Logger) bool {
file, fileErr := KrDirFile("last_update_check")
if fileErr != nil {
log.Error("Error finding home directory:", fileErr.Error())
} else {
lastUpdateUnixSecondsBytes, readErr := ioutil.ReadFile(file)
if readErr == nil {
var lastUpdateUnixSeconds int64
parseErr := json.Unmarshal(lastUpdateUnixSecondsBytes, &lastUpdateUnixSeconds)
if parseErr == nil && (time.Now().Unix()-lastUpdateUnixSeconds) < int64(3600*5) {
return true
}
}
nowUnixSecondsBytes, marshalErr := json.Marshal(time.Now().Unix())
if marshalErr != nil {
log.Error("Error serializing current time:", marshalErr.Error())
} else {
if writeErr := ioutil2.WriteFileAtomic(file, nowUnixSecondsBytes, 0700); writeErr != nil {
log.Error("Error writing update log file:", writeErr.Error())
}
}
}
return false
}
func CheckIfUpdateAvailable(log *logging.Logger) bool {
var latest semver.Version
var verErr error
if CheckedForUpdateRecently(log) {
log.Notice("Checked for update recently, falling back to latest version cache.")
var cacheErr error
latest, cacheErr = GetCachedLatestVersion()
if cacheErr != nil {
return false
}
} else {
latest, verErr = GetLatestVersion()
}
if verErr == nil {
if CURRENT_VERSION.LT(latest) {
return true
}
}
return false
}