forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
368 lines (333 loc) · 10.2 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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package update
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"time"
"github.com/deanishe/awgo/util"
)
var (
// UpdateInterval is how often to check for updates.
UpdateInterval = 24 * time.Hour
// HTTPTimeout is the timeout for establishing an HTTP(S) connection.
HTTPTimeout = 60 * time.Second
// HTTP client used to talk to APIs
client *http.Client
)
// Mockable functions
var (
// Run command
runCommand = func(name string, arg ...string) error {
return exec.Command(name, arg...).Run()
}
// save a URL to a filepath.
download = func(URL, path string) error {
res, err := openURL(URL)
if err != nil {
return err
}
defer res.Body.Close()
util.MustExist(filepath.Dir(path))
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
n, err := io.Copy(out, res.Body)
if err != nil {
return err
}
log.Printf("wrote %q (%d bytes)", util.PrettyPath(path), n)
return nil
}
)
// Source provides workflow files that can be downloaded.
// This is what concrete updaters (e.g. GitHub, Gitea) should implement.
// Source is called by the Updater after every updater interval.
type Source interface {
// Downloads returns all available workflow files.
Downloads() ([]Download, error)
}
// byVersion sorts downloads by version.
type byVersion []Download
// Len implements sort.Interface.
func (s byVersion) Len() int { return len(s) }
func (s byVersion) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byVersion) Less(i, j int) bool {
// Compare workflow versions first, compatible Alfred version second.
if s[i].Version.Ne(s[j].Version) {
return s[i].Version.Lt(s[j].Version)
}
return s[i].AlfredVersion().Lt(s[j].AlfredVersion())
}
// Download is an Alfred workflow available for download & installation.
// It is the primary update data structure, returned by all Sources.
type Download struct {
URL string // Where the workflow file can be downloaded from
// Filename for downloaded file.
// Must have extension .alfredworkflow or .alfredXworkflow where X is a number,
// otherwise the Download will be ignored.
Filename string
Version SemVer // Semantic version no.
Prerelease bool // Whether this version is a pre-release
}
// AlfredVersion returns minimum compatible version of Alfred based on file extension.
// For example, Workflow.alfred4workflow has version 4, while
// Workflow.alfred3workflow has version 3.
// The standard .alfredworkflow extension returns a zero version.
func (dl Download) AlfredVersion() SemVer {
m := rxWorkflowFile.FindStringSubmatch(dl.Filename)
if len(m) == 2 {
if v, err := NewSemVer(m[1]); err == nil {
return v
}
}
return SemVer{}
}
// Updater checks for newer version of the workflow. Available versions are
// provided by a Source, such as the built-in GitHub source, which
// reads the releases in a GitHub repo. It is a concrete implementation
// of aw.Updater.
//
// CheckForUpdate() retrieves the list of available downloads from the
// source and caches them. UpdateAvailable() reads the cache and returns
// true if there is a download with a higher version than the running workflow.
// Install() downloads the latest version and asks Alfred to install it.
//
// Because downloading releases is slow and workflows need to run fast,
// you should not run CheckForUpdate() in a Script Filter.
//
// If an Updater is set on a Workflow struct, a magic action will be set for
// updates, so you can just add an Item that autocompletes to the update
// magic argument ("workflow:update" by default), and AwGo will check for an
// update and install it if available.
//
// See ../examples/update for a full example implementation of updates.
type Updater struct {
Source Source // Provides downloads
CurrentVersion SemVer // Version of the installed workflow
Prereleases bool // Include pre-releases when checking for updates
// AlfredVersion is the version of the running Alfred application.
// Read from $alfred_version environment variable.
AlfredVersion SemVer
// When the remote release list was last checked (and possibly cached)
LastCheck time.Time
updateInterval time.Duration // How often to check for an update
downloads []Download // Available workflow files
// Cache paths
cacheDir string // Directory to store cache files in
pathLastCheck string // Cache path for check time
pathDownloads string // Cache path for available downloads
}
// NewUpdater creates a new Updater for Source. `currentVersion` is the workflow's
// version number and `cacheDir` is a directory where the Updater can cache
// a list of available releases.
func NewUpdater(src Source, currentVersion, cacheDir string) (*Updater, error) {
v, err := NewSemVer(currentVersion)
if err != nil {
return nil, fmt.Errorf("invalid version %q: %w", currentVersion, err)
}
if cacheDir == "" {
return nil, errors.New("empty cacheDir")
}
u := &Updater{
CurrentVersion: v,
LastCheck: time.Time{},
Source: src,
cacheDir: cacheDir,
updateInterval: UpdateInterval,
pathLastCheck: filepath.Join(cacheDir, "LastCheckTime.txt"),
pathDownloads: filepath.Join(cacheDir, "Downloads.json"),
}
if s := os.Getenv("alfred_version"); s != "" {
if v, err := NewSemVer(s); err == nil {
u.AlfredVersion = v
}
}
// Load LastCheck
if data, err := ioutil.ReadFile(u.pathLastCheck); err == nil {
t, err := time.Parse(time.RFC3339, string(data))
if err != nil {
log.Printf("error: load last update check: %v", err)
} else {
u.LastCheck = t
}
}
return u, nil
}
// UpdateAvailable returns true if an update is available. Retrieves
// the list of releases from the cache written by CheckForUpdate.
func (u *Updater) UpdateAvailable() bool {
dl := u.latest()
if dl == nil {
log.Println("no downloads available")
return false
}
log.Printf("latest version: %v", dl.Version)
return dl.Version.Gt(u.CurrentVersion)
}
// CheckDue returns true if the time since the last check is greater than
// Updater.UpdateInterval.
func (u *Updater) CheckDue() bool {
if u.LastCheck.IsZero() {
// log.Println("never checked for updates")
return true
}
elapsed := time.Since(u.LastCheck)
log.Printf("%s since last check for update", elapsed)
return elapsed > u.updateInterval
}
// CheckForUpdate fetches the list of releases from remote (via Releaser)
// and caches it locally.
func (u *Updater) CheckForUpdate() error {
// If update fails, don't try again for at least an hour
u.LastCheck = time.Now().Add(-u.updateInterval).Add(time.Hour)
defer u.cacheLastCheck()
var (
dls []Download
data []byte
err error
)
if dls, err = u.Source.Downloads(); err != nil {
return err
}
u.downloads = dls
if data, err = json.Marshal(dls); err != nil {
return err
}
u.clearCache()
if err := ioutil.WriteFile(u.pathDownloads, data, 0600); err != nil {
return err
}
u.LastCheck = time.Now()
return nil
}
// Install downloads and installs the latest available version.
// After the workflow file is downloaded, Install calls Alfred to
// install the update.
func (u *Updater) Install() error {
dl := u.latest()
if dl == nil {
return errors.New("no downloads available")
}
log.Printf("downloading version %s ...", dl.Version)
p := filepath.Join(u.cacheDir, dl.Filename)
if err := download(dl.URL, p); err != nil {
return err
}
return runCommand("open", p)
}
// clearCache removes the update cache.
func (u *Updater) clearCache() {
if err := util.ClearDirectory(u.cacheDir); err != nil {
log.Printf("error: clear cache: %v", err)
}
util.MustExist(u.cacheDir)
}
// cacheLastCheck saves time to cache.
func (u *Updater) cacheLastCheck() {
data, err := u.LastCheck.MarshalText()
if err != nil {
log.Printf("error: marshal time: %s", err)
return
}
if err := ioutil.WriteFile(u.pathLastCheck, data, 0600); err != nil {
log.Printf("error: cache update time: %s", err)
}
}
// Returns latest version that is compatible with the Updater's
// Alfred version & pre-release preference.
func (u *Updater) latest() *Download {
if u.downloads == nil {
u.downloads = []Download{}
if !util.PathExists(u.pathDownloads) {
log.Println("no cached releases")
return nil
}
// Load from cache
data, err := ioutil.ReadFile(u.pathDownloads)
if err != nil {
log.Printf("error: read cached releases: %s", err)
return nil
}
if err := json.Unmarshal(data, &u.downloads); err != nil {
log.Printf("error: unmarshal cached releases: %s", err)
return nil
}
sort.Sort(sort.Reverse(byVersion(u.downloads)))
}
if len(u.downloads) == 0 {
return nil
}
for _, dl := range u.downloads {
dl := dl
if dl.Prerelease && !u.Prereleases {
continue
}
if !u.AlfredVersion.IsZero() && dl.AlfredVersion().Gt(u.AlfredVersion) {
log.Printf("incompatible: %q: current=%v, required=%v", dl.Filename, u.AlfredVersion, dl.AlfredVersion())
continue
}
return &dl
}
return nil
}
// // Mockable function to run commands
// type commandRunner func(name string, arg ...string) error
//
// // Run command via exec.Command
// func runCommand(name string, arg ...string) error {
// return exec.Command(name, arg...).Run()
// }
// makeHTTPClient returns an http.Client with a sensible configuration.
func makeHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: HTTPTimeout,
KeepAlive: HTTPTimeout,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
},
}
}
// getURL returns the contents of a URL.
func getURL(url string) ([]byte, error) {
res, err := openURL(url)
if err != nil {
return []byte{}, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// openURL returns an http.Response. It will return an error if the
// HTTP status code > 299.
func openURL(url string) (*http.Response, error) {
log.Printf("fetching %s ...", url)
if client == nil {
client = makeHTTPClient()
}
r, err := client.Get(url)
if err != nil {
return nil, err
}
log.Printf("[%d] %s", r.StatusCode, url)
if r.StatusCode > 299 {
r.Body.Close()
return nil, errors.New(r.Status)
}
return r, nil
}