-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
224 lines (198 loc) 路 4.69 KB
/
upgrade.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cmd
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"time"
"github.com/fatih/color"
"github.com/lcian/cf-tool/util"
)
func less(a, b string) bool {
reg := regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`)
x := reg.FindSubmatch([]byte(a))
y := reg.FindSubmatch([]byte(b))
num := func(s []byte) int {
n, _ := strconv.Atoi(string(s))
return n
}
for i := 1; i <= 3; i++ {
if num(x[i]) < num(y[i]) {
return true
} else if num(x[i]) > num(y[i]) {
return false
}
}
return false
}
func getLatest() (version, note, ptime, url string, size uint, err error) {
goos := ""
switch runtime.GOOS {
case "darwin":
goos = "darwin"
case "linux":
goos = "linux"
case "windows":
goos = "windows"
default:
err = fmt.Errorf("Not support %v", runtime.GOOS)
return
}
arch := ""
switch runtime.GOARCH {
case "386":
arch = "32"
case "amd64":
arch = "64"
default:
err = fmt.Errorf("Not support %v", runtime.GOARCH)
return
}
resp, err := http.Get("https://api.github.com/repos/lcian/cf-tool/releases/latest")
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
result := make(map[string]interface{})
json.Unmarshal(body, &result)
version = result["tag_name"].(string)
note = result["body"].(string)
tm, _ := time.Parse("2006-01-02T15:04:05Z", result["published_at"].(string))
ptime = tm.In(time.Local).Format("2006-01-02 15:04")
url = fmt.Sprintf("https://github.com/lcian/cf-tool/releases/download/%v/cf_%v_%v_%v.zip", version, version, goos, arch)
assets, _ := result["assets"].([]interface{})
for _, tmp := range assets {
asset, _ := tmp.(map[string]interface{})
if url == asset["browser_download_url"] {
size = uint(asset["size"].(float64))
break
}
}
return
}
// WriteCounter progress counter
type WriteCounter struct {
Count, Total uint
last uint
}
// Print print progress
func (w *WriteCounter) Print() {
fmt.Printf("\rProgress: %v/%v KB Speed: %v KB/s Remain: %.0f s ",
w.Count/1024, w.Total/1024, (w.Count-w.last)/1024, float64(w.Total-w.Count)/float64(w.Count-w.last))
w.last = w.Count
}
func (w *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
w.Count += uint(n)
return n, nil
}
func upgrade(url, exePath string, size uint) (err error) {
updateDir := filepath.Dir(exePath)
oldPath := filepath.Join(updateDir, fmt.Sprintf(".%s.old", filepath.Base(exePath)))
color.Cyan("Move the old one to %v", oldPath)
if err = os.Rename(exePath, oldPath); err != nil {
return
}
defer func() {
if err != nil {
color.Cyan("Move the old one back")
if e := os.Rename(oldPath, exePath); e != nil {
color.Red(e.Error())
}
} else {
color.Cyan("Remove the old one")
if e := os.Remove(oldPath); e != nil {
color.Red(e.Error() + "\nYou could remove it manually")
}
}
}()
color.Cyan("Download %v", url)
counter := &WriteCounter{Count: 0, Total: size, last: 0}
counter.Print()
ticker := time.NewTicker(time.Second)
go func() {
for range ticker.C {
counter.Print()
}
}()
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(io.TeeReader(resp.Body, counter))
ticker.Stop()
counter.Print()
fmt.Println()
if err != nil {
return
}
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return
}
rc, err := reader.File[0].Open()
if err != nil {
return
}
newData, err := ioutil.ReadAll(rc)
rc.Close()
if err != nil {
return
}
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filepath.Base(exePath)))
color.Cyan("Save the new one to %v", newPath)
if err = ioutil.WriteFile(newPath, newData, 0755); err != nil {
return
}
if err = os.Rename(newPath, exePath); err != nil {
color.Cyan("Delete the new one %v", newPath)
if e := os.Remove(newPath); e != nil {
color.Red(e.Error())
}
}
return
}
// Upgrade itself
func Upgrade() (err error) {
color.Cyan("Checking version")
latest, note, ptime, url, size, err := getLatest()
if err != nil {
return
}
version := Args.Version
if !less(version, latest) {
color.Green("Current version %v is the latest", version)
return
}
color.Red("Current version is %v", version)
color.Green("The latest version is %v, published at %v", latest, ptime)
fmt.Println(note)
if !util.YesOrNo("Do you want to upgrade (y/n)? ") {
return
}
exePath, err := os.Executable()
if err != nil {
return
}
if exePath, err = filepath.EvalSymlinks(exePath); err != nil {
return
}
if err = upgrade(url, exePath, size); err != nil {
return
}
color.Green("Successfully updated to version %v", latest)
return
}