-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade.go
231 lines (199 loc) · 5.03 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
225
226
227
228
229
230
231
package upgrade
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/blang/semver"
"github.com/pkg/errors"
"github.com/termie/go-shutil"
)
func CurrentVersion(version string) (semver.Version, error) {
return semver.Make(version)
}
func AvailableVersion(repo string, filePrefix string, channel string) (semver.Version, error) {
resp, err := http.Get(
fmt.Sprintf("%s/%s%s", repo, filePrefix, channel),
)
if err != nil {
return semver.Version{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return semver.Version{}, fmt.Errorf(
"while getting version; unexpected status %s",
resp.Status,
)
}
versionBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return semver.Version{}, err
}
return semver.Make(string(versionBytes))
}
// CheckAndNotifyIfOutOfDate checks if this utility is out of date compared to
// the repository. It uses a caching strategy to avoid costly
// checks with every run.
func CheckAndNotifyIfOutOfDate(tool string, currentVersion string, repo string, filePrefix string, versionStable string, hoursToCheckForUpdate float64, upgradeCommand string) (bool, error) {
fn := filepath.Join(GetHome(), "."+tool+"-version-check")
file, err := os.OpenFile(fn, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot create file (%s) to cache version checks\n", fn)
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
return false, err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot stat (%s)\n", fn)
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
return false, err
}
diff := time.Now().Sub(fi.ModTime())
if diff.Hours() < hoursToCheckForUpdate {
return false, nil
}
// update mtime
err = file.Truncate(0)
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot update mtime (%s)\n", fn)
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
return false, err
}
curr, err := CurrentVersion(currentVersion)
if err != nil {
return false, err
}
avail, err := AvailableVersion(repo, filePrefix, versionStable)
if err != nil {
return false, err
}
if avail.GT(curr) {
fmt.Fprintf(os.Stderr, "A new version of [%s] is available, run: %s\n", tool, upgradeCommand)
return true, nil
}
return false, nil
}
func Download(repo string, version string, tool string) (string, error) {
// now that we have the version, create the temp file and set perms
tmp, err := ioutil.TempFile("", tool+"_upgrade")
if err != nil {
return "", err
}
if err = tmp.Chmod(0755); err != nil {
if err.(*os.PathError).Err.Error() != "not supported by windows" {
return "", err
}
}
tmpfn := tmp.Name()
defer func() {
tmp.Close()
if err != nil {
os.Remove(tmpfn)
}
}()
// get the fresh bits
resp, err := http.Get(
fmt.Sprintf("%s/%s/%s_%s_%s",
repo,
version,
tool,
runtime.GOOS,
runtime.GOARCH,
),
)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf(
"while getting updated binary; unexpected status %s",
resp.Status,
)
}
if _, err := io.Copy(tmp, resp.Body); err != nil {
return "", err
}
return tmpfn, nil
}
func RemoveBackup() (string, string, error) {
arg0, err := os.Executable()
if err != nil {
return "", "", errors.Wrap(err, "unable to find executable")
}
backup := arg0 + "~"
err = os.RemoveAll(backup)
if err != nil {
return arg0, backup, errors.Wrap(err, fmt.Sprintf(
"unable to remove %s",
backup,
))
}
return arg0, backup, nil
}
func Upgrade(tool string, currentVersion string, repo string, filePrefix string, versionStable string, upgradeForce *bool) error {
curr, err := CurrentVersion(currentVersion)
if err != nil {
return errors.Wrap(err, "unable to get current version")
}
avail, err := AvailableVersion(repo, filePrefix, versionStable)
if err != nil {
return errors.Wrap(err, "unable to get available version")
}
if !*upgradeForce && curr.GTE(avail) {
fmt.Printf("%s is up-to-date. Go forth and be awesome!\n", tool)
return nil
}
arg0, backup, err := RemoveBackup()
if err != nil {
return err
}
fmt.Println("upgrading", arg0, "to", avail)
tmp, err := Download(repo, avail.String(), tool)
if err != nil {
return errors.Wrap(err, fmt.Sprintf(
"unable to upgrade %s",
tool,
))
}
err = os.Rename(arg0, backup)
if err != nil {
return errors.Wrap(err, fmt.Sprintf(
"unable to upgrade %s in-place; move %s to %s",
tool,
tmp,
os.Args[0],
))
}
_, err = shutil.Copy(tmp, arg0, false)
if err != nil {
err = os.Rename(backup, arg0)
if err != nil {
return errors.Wrap(err, fmt.Sprintf(
"upgrade failed and unable to recover from backup;"+
" move %s to %s",
tmp,
os.Args[0],
))
}
return errors.Wrap(err, fmt.Sprintf(
"unable to upgrade %s in-place; move %s to %s",
tool,
tmp,
os.Args[0],
))
}
err = os.Remove(tmp)
if err != nil {
return errors.Wrap(err, fmt.Sprintf(
"unable to clean up temp file; remove %s",
tmp,
))
}
return nil
}