-
Notifications
You must be signed in to change notification settings - Fork 11
/
version.go
149 lines (118 loc) · 2.88 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ion
import (
"bufio"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/hashicorp/go-version"
"github.com/kataras/golog"
)
const (
versionURL = "https://raw.githubusercontent.com/get-ion/ion/master/VERSION"
updateCmd = "go get -u -v -f github.com/get-ion/ion"
)
var checkVersionOnce = sync.Once{}
// CheckVersion checks for any available updates.
func CheckVersion() {
checkVersionOnce.Do(func() {
checkVersion()
})
}
func checkVersion() {
// open connection and read/write timeouts
timeout := time.Duration(10 * time.Second)
transport := http.Transport{
Dial: func(network string, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, timeout)
if err != nil {
golog.Debugf("%v", err)
return nil, err
}
conn.SetDeadline(time.Now().Add(timeout)) // skip error
return conn, nil
},
}
client := http.Client{
Transport: &transport,
}
r, err := client.Get(versionURL)
if err != nil {
golog.Debugf("%v", err)
return
}
defer r.Body.Close()
if r.StatusCode >= 400 {
return
}
b, err := ioutil.ReadAll(r.Body)
if len(b) == 0 || err != nil {
golog.Debugf("%v", err)
return
}
var (
fetchedVersion = string(b)
changelogURL string
)
if idx := strings.IndexByte(fetchedVersion, ':'); idx > 0 {
changelogURL = fetchedVersion[idx+1:]
fetchedVersion = fetchedVersion[0:idx]
}
latestVersion, err := version.NewVersion(fetchedVersion)
if err != nil {
golog.Debugf("while parsing latest version: %v", err)
return
}
currentVersion, err := version.NewVersion(Version)
if err != nil {
golog.Debugf("while parsing current version: %v", err)
return
}
if currentVersion.GreaterThan(latestVersion) {
golog.Debugf("current version is greater than latest version, report as bug")
return
}
if currentVersion.Equal(latestVersion) {
return
}
// currentVersion.LessThan(latestVersion)
var updaterYesInput = [...]string{"y", "yes", "si"}
text := "A more recent version has been found[%s > %s].\n"
if changelogURL != "" {
text += "Release notes: %s\n"
}
text += "Update now?[%s]: "
golog.Warnf(text,
latestVersion.String(), currentVersion.String(),
changelogURL,
updaterYesInput[0]+"/n")
silent := false
sc := bufio.NewScanner(os.Stdin)
shouldUpdate := silent
if !silent {
if sc.Scan() {
inputText := sc.Text()
for _, s := range updaterYesInput {
if inputText == s {
shouldUpdate = true
}
}
}
}
if shouldUpdate {
goget := strings.Split(updateCmd, " ")
// go get -u github.com/:owner/:repo
cmd := exec.Command(goget[0], goget[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
golog.Warnf("unexpected message while trying to go get: %v", err)
return
}
golog.Infof("Update process finished, current version: %s.\nManual app restart is required.\n", latestVersion.String())
}
}