-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
67 lines (58 loc) · 1.56 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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
selfupdate "github.com/staktrace/go-update"
)
var version = ""
var repo = ""
type update struct{}
func (update) Run() error {
if version == "" || repo == "" {
return fmt.Errorf("update not configured")
}
fmt.Fprintf(os.Stderr, "checking for new version in github.com/%s/releases\n", repo)
fmt.Fprintf(os.Stderr, "current version: %s\n", version)
file := "nec-" + runtime.GOOS + "-" + runtime.GOARCH
if runtime.GOOS == "windows" {
file += ".exe"
}
reg := regexp.MustCompile(repo + "/releases/download/([^/]+)/" + file)
latest := ""
c := &http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
m := reg.FindStringSubmatch(r.URL.Path)
if m != nil {
latest = m[1]
}
return nil
},
}
req, err := c.Get("https://github.com/" + repo + "/releases/latest/download/" + file)
if err != nil {
return fmt.Errorf("fetching latest release: %w", err)
}
defer req.Body.Close()
fmt.Fprintf(os.Stderr, "latest version: %s\n", latest)
if version == latest {
return nil
}
opt := selfupdate.Options{}
if runtime.GOOS == "windows" {
opt.OldSavePath = filepath.Join(os.TempDir(), "nec.exe.old")
os.Remove(opt.OldSavePath)
}
err = selfupdate.Apply(req.Body, opt)
if err != nil {
return fmt.Errorf("updating binary: %w", err)
}
fmt.Fprintln(os.Stderr, "successfully updated to", latest)
if runtime.GOOS == "windows" {
fmt.Fprintf(os.Stderr, "old binary remains at %s\nwill be deleted on the next 'nec update'\n", opt.OldSavePath)
}
return nil
}