This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathupdate.go
119 lines (104 loc) · 2.83 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
package main
import (
"fmt"
"net/http"
"net/url"
"runtime"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/stackerr"
"github.com/inconshreveable/go-update"
"github.com/kardianos/osext"
"github.com/spf13/cobra"
)
const (
macDownload = "parse"
windowsDownload = "parse.exe"
linuxDownload = "parse_linux"
linuxArmDownload = "parse_linux_arm"
downloadURLFormat = "https://github.com/ParsePlatform/parse-cli/releases/download/release_%s/%s"
)
type updateCmd struct{}
func (u *updateCmd) latestVersion(e *parsecli.Env) (string, error) {
v := make(url.Values)
v.Set("version", "latest")
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: "supported", RawQuery: v.Encode()},
}
var res struct {
Version string `json:"version"`
}
if _, err := e.ParseAPIClient.Do(req, nil, &res); err != nil {
return "", stackerr.Wrap(err)
}
return res.Version, nil
}
func (u *updateCmd) getDownloadURL(e *parsecli.Env) (string, error) {
ostype := runtime.GOOS
arch := runtime.GOARCH
latestVersion, err := u.latestVersion(e)
if err != nil {
return "", err
}
if latestVersion == "" || latestVersion == parsecli.Version {
return "", nil
}
var downloadURL string
switch ostype {
case "darwin":
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, macDownload)
case "windows":
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, windowsDownload)
case "linux":
if arch == "arm" {
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, linuxArmDownload)
} else {
downloadURL = fmt.Sprintf(downloadURLFormat, latestVersion, linuxDownload)
}
}
return downloadURL, nil
}
func (u *updateCmd) updateCLI(e *parsecli.Env) (bool, error) {
downloadURL, err := u.getDownloadURL(e)
if err != nil {
return false, err
}
if downloadURL == "" {
return false, nil
}
exec, err := osext.Executable()
if err != nil {
return false, stackerr.Wrap(err)
}
fmt.Fprintf(e.Out, "Downloading binary from %s.\n", downloadURL)
resp, err := http.Get(downloadURL)
if err != nil {
return false, stackerr.Newf("Update failed with error: %v", err)
}
defer resp.Body.Close()
err = update.Apply(resp.Body, update.Options{TargetPath: exec})
if err != nil {
return false, stackerr.Newf("Update failed with error: %v", err)
}
fmt.Fprintf(e.Out, "Successfully updated binary at: %s\n", exec)
return true, nil
}
func (u *updateCmd) run(e *parsecli.Env) error {
updated, err := u.updateCLI(e)
if err != nil {
return err
}
if !updated {
fmt.Fprintf(e.Out, "Already using the latest cli version: %s\n", parsecli.Version)
}
return nil
}
func NewUpdateCmd(e *parsecli.Env) *cobra.Command {
var u updateCmd
return &cobra.Command{
Use: "update",
Short: "Updates this tool to the latest version",
Long: "Updates this tool to the latest version.",
Run: parsecli.RunNoArgs(e, u.run),
}
}