/
update.go
385 lines (311 loc) · 8.06 KB
/
update.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package cmd
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
"github.com/hofstadter-io/hof/cmd/hof/ga"
"github.com/hofstadter-io/hof/cmd/hof/verinfo"
)
var UpdateLong = `Print the build version for hof`
var (
UpdateCheckFlag bool
UpdateVersionFlag string
UpdateStarted bool
UpdateErrored bool
UpdateChecked bool
UpdateAvailable *ProgramVersion
UpdateData []interface{}
)
func init() {
UpdateCmd.Flags().BoolVarP(&UpdateCheckFlag, "check", "", false, "only check for an update")
UpdateCmd.Flags().BoolVarP(&UpdateCheckFlag, "list", "", false, "show local installed version")
UpdateCmd.Flags().StringVarP(&UpdateVersionFlag, "version", "V", "", "the version to update or install")
}
const updateMessage = `
Updates available. v%s -> %s
run 'hof update' to get the latest.
`
// TODO, add a curl to the above? or os specific?
var UpdateCmd = &cobra.Command{
Use: "update",
Short: "update the hof tool",
Long: UpdateLong,
PreRun: func(cmd *cobra.Command, args []string) {
ga.SendCommandPath("update")
},
Run: func(cmd *cobra.Command, args []string) {
latest, err := CheckUpdate(true)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
// Semver Check?
cur := ProgramVersion{Version: verinfo.Version}
if latest.Version == cur.Version || (UpdateVersionFlag == "" && cur.Version == "vLocal") {
return
} else {
if UpdateCheckFlag {
PrintUpdateAvailable()
return
}
}
err = InstallUpdate()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
},
}
func init() {
// disable automatic update checks
// go CheckUpdate(false)
help := UpdateCmd.HelpFunc()
usage := UpdateCmd.UsageFunc()
thelp := func(cmd *cobra.Command, args []string) {
if UpdateCmd.Name() == cmd.Name() {
ga.SendCommandPath("update help")
}
help(cmd, args)
}
tusage := func(cmd *cobra.Command) error {
if UpdateCmd.Name() == cmd.Name() {
ga.SendCommandPath("update usage")
}
return usage(cmd)
}
UpdateCmd.SetHelpFunc(thelp)
UpdateCmd.SetUsageFunc(tusage)
}
type ProgramVersion struct {
Version string
URL string
}
func CheckUpdate(manual bool) (ver ProgramVersion, err error) {
if !manual && os.Getenv("HOF_UPDATES_DISABLED") != "" {
return
}
UpdateStarted = true
cur := ProgramVersion{Version: "v" + verinfo.Version}
checkURL := "https://api.github.com/repos/hofstadter-io/hof/releases/latest"
if UpdateVersionFlag != "" {
checkURL = "https://api.github.com/repos/hofstadter-io/hof/releases/tags/" + UpdateVersionFlag
manual = true
}
req := gorequest.New()
if os.Getenv("GITHUB_TOKEN") != "" {
req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN"))
}
resp, b, errs := req.Get(checkURL).EndBytes()
UpdateErrored = true
check := "http2: server sent GOAWAY and closed the connection"
if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) {
// fmt.Println("errs:", errs)
return ver, errs[0]
}
if len(errs) != 0 || resp.StatusCode >= 500 {
return ver, fmt.Errorf("Internal Error: " + string(b))
}
if resp.StatusCode >= 400 {
if resp.StatusCode == 404 {
fmt.Println("404?!", checkURL)
return ver, fmt.Errorf("No releases available :[")
}
return ver, fmt.Errorf("Bad Request: " + string(b))
}
UpdateErrored = false
// fmt.Println(string(b))
var gh map[string]interface{}
err = json.Unmarshal(b, &gh)
if err != nil {
return ver, err
}
nameI, ok := gh["name"]
if !ok {
return ver, fmt.Errorf("Internal Error: could not find version in update check response")
}
name, ok := nameI.(string)
if !ok {
return ver, fmt.Errorf("Internal Error: version is not a string in update check response")
}
ver.Version = name
if !manual {
UpdateChecked = true
// Semver Check?
if ver.Version != cur.Version && cur.Version != "vLocal" {
UpdateAvailable = &ver
}
return ver, nil
}
// This goes here and signals else where that we got the request back
UpdateChecked = true
// Semver Check?
if ver.Version != cur.Version && (manual || cur.Version != "vLocal") {
UpdateAvailable = &ver
aI, ok := gh["assets"]
if ok {
a, aok := aI.([]interface{})
if aok {
UpdateData = a
}
}
}
return ver, nil
}
func WaitPrintUpdateAvailable() {
if os.Getenv("HOF_UPDATES_DISABLED") != "" {
return
}
for i := 0; i < 20 && !UpdateStarted && !UpdateChecked && !UpdateErrored; i++ {
time.Sleep(50 * time.Millisecond)
}
PrintUpdateAvailable()
}
func PrintUpdateAvailable() {
if UpdateChecked && UpdateAvailable != nil {
fmt.Printf(updateMessage, verinfo.Version, UpdateAvailable.Version)
}
}
func InstallUpdate() (err error) {
fmt.Printf("Installing hof@%s\n", UpdateAvailable.Version)
if UpdateData == nil {
return fmt.Errorf("No update available")
}
/*
vers, err := json.MarshalIndent(UpdateData, "", " ")
if err == nil {
fmt.Println(string(vers))
}
*/
fmt.Println("OS/Arch", verinfo.BuildOS, verinfo.BuildArch)
url := ""
for _, Asset := range UpdateData {
asset := Asset.(map[string]interface{})
U := asset["browser_download_url"].(string)
u := strings.ToLower(U)
osOk, archOk := false, false
switch verinfo.BuildOS {
case "linux":
if strings.Contains(u, "linux") {
osOk = true
}
case "darwin":
if strings.Contains(u, "darwin") {
osOk = true
}
case "windows":
if strings.Contains(u, "windows") {
osOk = true
}
}
switch verinfo.BuildArch {
case "amd64":
if strings.Contains(u, "x86_64") {
archOk = true
}
case "arm64":
if strings.Contains(u, "arm64") {
archOk = true
}
case "arm":
if strings.Contains(u, "arm") && !strings.Contains(u, "arm64") {
archOk = true
}
}
if osOk && archOk {
url = u
break
}
}
fmt.Println("Download URL: ", url)
switch verinfo.BuildOS {
case "linux":
fallthrough
case "darwin":
return downloadAndInstall(url)
case "windows":
fmt.Println("Please download and install manually from the link above.")
return nil
}
fmt.Println()
return nil
}
func downloadAndInstall(url string) error {
req := gorequest.New()
if os.Getenv("GITHUB_TOKEN") != "" {
req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN"))
}
// handle redirects
req.RedirectPolicy(func(r gorequest.Request, via []gorequest.Request) error {
viaURL := []string{}
for _, v := range via {
viaURL = append(viaURL, v.URL.String())
}
return nil
})
resp, content, errs := req.Get(url).EndBytes()
check := "http2: server sent GOAWAY and closed the connection"
if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) {
fmt.Println("errs:", errs)
fmt.Println("resp:", resp)
return errs[0]
}
if len(errs) != 0 || resp.StatusCode >= 400 {
return fmt.Errorf("Error %v - %s", resp.StatusCode, string(content))
}
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
return err
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(content); err != nil {
return err
}
if err := tmpfile.Close(); err != nil {
return err
}
ex, err := os.Executable()
if err != nil {
return err
}
real, err := filepath.EvalSymlinks(ex)
if err != nil {
return err
}
// TODO (hof/update) replace current binary swap with something better
// either a straightup file write or a golang package that handles binary updates already (there's at least 1)
// copy the file
cmdStr := fmt.Sprintf("export OWNER=$(ls -l %s | awk '{ print $3 \":\" $4 }') && mv %s %s-v%s && cp %s %s && chown $OWNER %s && chmod 0755 %s",
real, // get owner
real, real, verinfo.Version, // backup
tmpfile.Name(), real, // cp
real, // chown
real, // chmod
)
// fmt.Println("CMDSTR:", cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)
// prep stdin for password
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
go func() {
defer stdin.Close()
io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
}()
stdoutStderr, err := cmd.CombinedOutput()
fmt.Printf("%s\n", stdoutStderr)
if err != nil {
return err
}
UpdateAvailable = nil
UpdateData = nil
return nil
}