-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate.go
More file actions
71 lines (65 loc) · 1.62 KB
/
update.go
File metadata and controls
71 lines (65 loc) · 1.62 KB
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
package cmd
import (
"fmt"
"io"
"net/http"
"runtime"
"strings"
"github.com/drycc/workflow-cli/version"
"github.com/minio/selfupdate"
)
const WorkflowCliURL = "https://www.drycc.cc/workflow-cli.txt"
func (d *DryccCmd) latestVersion() (string, string, error) {
quit := progress(d.WOut)
resp, err := http.Get(WorkflowCliURL)
quit <- true
<-quit
if err != nil {
return "", "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
prefix := "drycc-"
suffix := fmt.Sprintf("-%s-%s", runtime.GOOS, runtime.GOARCH)
for _, url := range strings.Split(string(body), "\n") {
if strings.HasSuffix(url, suffix) {
names := strings.Split(url, "/")
version := strings.ReplaceAll(strings.ReplaceAll(names[len(names)-1], suffix, ""), prefix, "")
return version, url, nil
}
}
return "", "", fmt.Errorf("unable to obtain version: %s, %s", runtime.GOOS, runtime.GOARCH)
}
// Update workflow-cli to latest release
func (d *DryccCmd) Update(dryRun bool) error {
latestVersion, downloadURL, err := d.latestVersion()
if err != nil {
return err
}
if latestVersion != version.Version {
d.Printf("Update workflow cli from %s to %s... ", version.Version, latestVersion)
if dryRun {
d.Println("skip")
} else {
resp, err := http.Get(downloadURL)
if err != nil {
return err
}
defer resp.Body.Close()
quit := progress(d.WOut)
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
quit <- true
<-quit
if err != nil {
return err
}
d.Println("done")
}
} else {
d.Println("You are already running the most recent version.")
}
return nil
}