forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatest_version.go
63 lines (56 loc) · 1.31 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
package kr
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"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
}