forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
212 lines (195 loc) · 5.07 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/ansel1/merry"
"github.com/dghubble/sling"
"github.com/dickeyxxx/golock"
)
func init() {
Topics = append(Topics, &Topic{
Name: "update",
Description: "update heroku-cli",
Commands: CommandSet{
{
Topic: "update",
Hidden: true,
Description: "updates the Heroku CLI",
DisableAnalytics: true,
Args: []Arg{{Name: "channel", Optional: true}},
Run: func(ctx *Context) {
channel := ctx.Args.(map[string]string)["channel"]
if channel == "" {
channel = Channel
}
Update(channel)
},
},
},
})
}
// Autoupdate is a flag to enable/disable CLI autoupdating
var Autoupdate = "no"
var updateLockPath = filepath.Join(CacheHome, "updating.lock")
var autoupdateFile = filepath.Join(CacheHome, "autoupdate")
// Update updates the CLI and plugins
func Update(channel string) {
touchAutoupdateFile()
SubmitAnalytics()
updateCLI(channel)
UserPlugins.Update()
truncate(ErrLogPath, 1000)
cleanTmpDirs()
}
func updateCLI(channel string) {
if Autoupdate != "yes" {
return
}
manifest := GetUpdateManifest(channel)
binExists, _ := FileExists(expectedBinPath())
if binExists && manifest.Version == Version && manifest.Channel == Channel {
return
}
DownloadCLI(channel, filepath.Join(DataHome, "cli"), manifest)
loadNewCLI()
}
// DownloadCLI downloads a CLI update to a given path
func DownloadCLI(channel, path string, manifest *Manifest) {
locked, err := golock.IsLocked(updateLockPath)
LogIfError(err)
if locked {
must(merry.Errorf("Update in progress"))
}
LogIfError(golock.Lock(updateLockPath))
unlock := func() {
golock.Unlock(updateLockPath)
}
defer unlock()
hideCursor()
downloadingMessage := fmt.Sprintf("heroku-cli: Updating to %s...", manifest.Version)
if manifest.Channel != "stable" {
downloadingMessage = fmt.Sprintf("%s (%s)", downloadingMessage, manifest.Channel)
}
Logln(downloadingMessage)
build := manifest.Builds[runtime.GOOS+"-"+runtime.GOARCH]
if build == nil {
must(merry.Errorf("no build for %s", manifest.Channel))
}
reader, getSha, err := downloadXZ(build.URL, downloadingMessage)
must(err)
tmp := tmpDir(DataHome)
must(extractTar(reader, tmp))
sha := getSha()
if sha != build.Sha256 {
must(merry.Errorf("SHA mismatch: expected %s to be %s", sha, build.Sha256))
}
exists, _ := FileExists(path)
if exists {
must(os.Rename(path, filepath.Join(tmpDir(DataHome), "heroku")))
}
Errln(path)
must(os.Rename(filepath.Join(tmp, "heroku"), path))
Debugf("updated to %s\n", manifest.Version)
}
// IsUpdateNeeded checks if an update is available
func IsUpdateNeeded() bool {
if exists, _ := FileExists(expectedBinPath()); !exists {
return true
}
f, err := os.Stat(autoupdateFile)
if err != nil {
if os.IsNotExist(err) {
return true
}
LogIfError(err)
return true
}
return time.Since(f.ModTime()) > 4*time.Hour
}
func touchAutoupdateFile() {
out, err := os.OpenFile(autoupdateFile, os.O_WRONLY|os.O_CREATE, 0644)
must(err)
_, err = out.WriteString(time.Now().String())
must(err)
err = out.Close()
must(err)
}
// Manifest is the manifest.json for releases
type Manifest struct {
ReleasedAt string `json:"released_at"`
Version string `json:"version"`
Channel string `json:"channel"`
Builds map[string]*Build `json:"builds"`
}
// Build is a part of a Manifest
type Build struct {
URL string `json:"url"`
Sha256 string `json:"sha256"`
Bytes int64 `json:"bytes"`
}
// GetUpdateManifest loads the manifest.json for a channel
func GetUpdateManifest(channel string) *Manifest {
var m Manifest
url := "https://cli-assets.heroku.com/branches/" + channel + "/manifest.json"
rsp, err := sling.New().Get(url).ReceiveSuccess(&m)
must(err)
must(getHTTPError(rsp))
return &m
}
// TriggerBackgroundUpdate will trigger an update to the client in the background
func TriggerBackgroundUpdate() {
if IsUpdateNeeded() {
Debugln("triggering background update")
touchAutoupdateFile()
exec.Command(BinPath, "update").Start()
}
}
func cleanTmpDirs() {
clean := func(base string) {
dir := filepath.Join(base, "tmp")
if exists, _ := FileExists(dir); !exists {
return
}
files, err := ioutil.ReadDir(dir)
LogIfError(err)
for _, file := range files {
if time.Since(file.ModTime()) > 24*time.Hour {
path := filepath.Join(dir, file.Name())
Debugf("removing old tmp dir %s", path)
LogIfError(os.RemoveAll(path))
}
}
}
clean(DataHome)
clean(CacheHome)
}
func expectedBinPath() string {
bin := filepath.Join(DataHome, "cli", "bin", "heroku")
if runtime.GOOS == WINDOWS {
bin = bin + ".exe"
}
return bin
}
func loadNewCLI() {
if Autoupdate == "no" {
return
}
expected := expectedBinPath()
if BinPath == expected {
return
}
if exists, _ := FileExists(expected); !exists {
if exists, _ = FileExists(npmBinPath()); !exists {
// uh oh, npm isn't where it should be.
// The CLI probably isn't installed right so force an update
Update(Channel)
}
return
}
execBin(expected, Args...)
}