This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathversion.go
113 lines (102 loc) · 2.24 KB
/
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
112
113
package pi
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/hyperhq/client-go/tools/clientcmd"
"github.com/golang/glog"
"github.com/google/go-github/github"
"log"
)
var (
Version = ""
Commit = ""
Build = ""
)
var upcktimePath = "cktime.json"
type updateTimeRecord struct {
LastUpdate time.Time `json:"lastUpdate"`
}
type CheckUpdate struct {
Config string
}
func NewCheckUpdate() *CheckUpdate {
return &CheckUpdate{
Config: fmt.Sprintf("%s/%s", clientcmd.RecommendedConfigDir, upcktimePath),
}
}
func (c *CheckUpdate) ReadTime() time.Time {
now := time.Now()
p, err := os.Open(c.Config)
if os.IsNotExist(err) {
now = now.Add(-25 * time.Hour)
c.WriteTime(now)
return now
}
if err != nil {
if os.Getenv("HYPER_DEBUG") == "true" {
log.Fatalf("read lastUpdate time from %v error: %v", c.Config, err)
}
now = now.Add(-25 * time.Hour)
return now
}
var update updateTimeRecord
if err = json.NewDecoder(p).Decode(&update); err != nil {
return now
}
return update.LastUpdate
}
func (c *CheckUpdate) WriteTime(t time.Time) bool {
data, err := json.Marshal(updateTimeRecord{t})
if err != nil {
return false
}
err = ioutil.WriteFile(c.Config, data, 0600)
if err != nil {
if os.Getenv("HYPER_DEBUG") == "true" {
log.Fatalf("write lastUpdate time to %v error: %v", c.Config, err)
}
return false
}
return true
}
func CheckRelease() {
client := github.NewClient(nil)
opt := &github.ListOptions{}
var (
releases []*github.RepositoryRelease
err error
latest string
)
if releases, _, err = client.Repositories.ListReleases(context.Background(), "hyperhq", "pi", opt); err != nil {
glog.V(4).Info("failed to list repo from github")
} else {
for _, r := range releases {
if *r.TagName == "latest" {
latest = strings.TrimSpace(strings.Split(*r.Body, "\n")[0])
if latest == Version {
return
} else {
fmt.Printf("\nThere is a new version: %v\n", latest)
}
break
}
}
for _, r := range releases {
if latest == *r.TagName {
preRelease := ""
for _, a := range r.Assets {
if *r.Prerelease {
preRelease = "(Pre-release) "
}
fmt.Printf("- %v%v\n", preRelease, *a.BrowserDownloadURL)
}
return
}
}
}
}