-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
267 lines (220 loc) · 6.38 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
// package goupdate provides tooling to auto-update binary releases
// from GitHub based on the user's current version and operating system.
package goupdate
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/c4milo/unpackit"
"github.com/pkg/errors"
"github.com/zan8in/gologger"
)
// Proxy is used to proxy a reader, for example
// using https://github.com/cheggaaa/pb to provide
// progress updates.
type Proxy func(int, io.ReadCloser) io.ReadCloser
// NopProxy does nothing.
var NopProxy = func(size int, r io.ReadCloser) io.ReadCloser {
return r
}
// Manager is the update manager.
type Manager struct {
Store // Store for releases such as Github or a custom private store.
Command string // Command is the executable's name.
}
// Release represents a project release.
type Release struct {
Version string // Version is the release version.
Notes string // Notes is the markdown release notes.
URL string // URL is the notes url.
PublishedAt time.Time // PublishedAt is the publish time.
Assets []*Asset // Assets is the release assets.
}
// Asset represents a project release asset.
type Asset struct {
Name string // Name of the asset.
Size int // Size of the asset.
URL string // URL of the asset.
Downloads int // Downloads count.
LatestVersion string
}
// InstallTo binary to the given dir.
func (m *Manager) InstallTo(path, dir string) error {
gologger.Debug().Msgf("unpacking %q", path)
f, err := os.Open(path)
if err != nil {
return errors.Wrap(err, "opening tarball")
}
tempdir := filepath.Join(dir, "tmp")
err = unpackit.Unpack(f, tempdir)
if err != nil {
f.Close()
return errors.Wrap(err, "unpacking tarball")
}
defer os.RemoveAll(tempdir)
if err := f.Close(); err != nil {
return errors.Wrap(err, "closing tarball")
}
latestBinary := filepath.Join(tempdir, m.Command)
if err := os.Chmod(latestBinary, 0755); err != nil {
return errors.Wrap(err, "chmod")
}
currentBinary := filepath.Join(dir, m.Command)
latestBinaryTmp := currentBinary + ".tmp"
gologger.Debug().Msgf("copy %q to %q", latestBinary, latestBinaryTmp)
if err := copyFile(latestBinaryTmp, latestBinary); err != nil {
return errors.Wrap(err, "copying")
}
if runtime.GOOS == "windows" {
old := currentBinary + ".old"
gologger.Debug().Msgf("windows workaround renaming %q to %q", currentBinary, old)
if err := os.Rename(currentBinary, old); err != nil {
return errors.Wrap(err, "windows renaming")
}
}
gologger.Debug().Msgf("renaming %q to %q", latestBinaryTmp, currentBinary)
if err := os.Rename(latestBinaryTmp, currentBinary); err != nil {
return errors.Wrap(err, "renaming")
}
return nil
}
// Install binary to replace the current version.
func (m *Manager) Install(path string) error {
// bin, err := exec.LookPath(m.Command) // 获取当前程序的绝对路径
// if err != nil {
// return errors.Wrapf(err, "looking up path of %q", m.Command)
// }
// dir := filepath.Dir(bin)
dir, err := getExecutablePath()
if err != nil {
return errors.Wrapf(err, "looking up path of %q", m.Command)
}
return m.InstallTo(path, dir)
}
// FindTarball returns a tarball matching os and arch, or nil.
func (r *Release) FindTarball(os, arch string) *Asset {
s := fmt.Sprintf("%s_%s", os, arch)
for _, a := range r.Assets {
ext := filepath.Ext(a.Name)
if strings.Contains(a.Name, s) && ext == ".gz" {
return a
}
}
return nil
}
// FindZip returns a zipfile matching os and arch, or nil.
func (r *Release) FindZip(os, arch string) *Asset {
s := fmt.Sprintf("%s_%s", os, arch)
for _, a := range r.Assets {
ext := filepath.Ext(a.Name)
if strings.Contains(a.Name, s) && ext == ".zip" {
return a
}
}
return nil
}
// Download the asset to a tmp directory and return its path.
func (a *Asset) Download() (string, error) {
return a.DownloadProxy(NopProxy)
}
// DownloadProxy the asset to a tmp directory and return its path.
func (a *Asset) DownloadProxy(proxy Proxy) (string, error) {
f, err := os.CreateTemp(os.TempDir(), "update-")
if err != nil {
return "", errors.Wrap(err, "creating temp file")
}
gologger.Debug().Msgf("fetch %q", a.URL)
res, err := http.Get(a.URL)
if err != nil {
return "", errors.Wrap(err, "fetching asset")
}
kind := res.Header.Get("Content-Type")
size, _ := strconv.Atoi(res.Header.Get("Content-Length"))
gologger.Debug().Msgf("response %s – %s (%d KiB)", res.Status, kind, size/1024)
body := proxy(size, res.Body)
if res.StatusCode >= 400 {
body.Close()
return "", errors.Wrap(err, res.Status)
}
gologger.Debug().Msgf("copy to %q", f.Name())
if _, err := io.Copy(f, body); err != nil {
body.Close()
return "", errors.Wrap(err, "copying body")
}
if err := body.Close(); err != nil {
return "", errors.Wrap(err, "closing body")
}
if err := f.Close(); err != nil {
return "", errors.Wrap(err, "closing file")
}
gologger.Debug().Msgf("copied")
return f.Name(), nil
}
// copyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func copyFile(dst, src string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(out, in)
if err != nil {
return
}
err = out.Sync()
if err != nil {
return
}
si, err := os.Stat(src)
if err != nil {
return
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return
}
return
}
func getExecutablePath() (string, error) {
exePath, err := os.Executable()
if err != nil {
return exePath, err
}
// 检查可执行文件是否存在
_, err = os.Stat(exePath)
if os.IsNotExist(err) {
// 可执行文件不存在,采取适当的处理措施
// 例如记录错误信息或提供备用路径
return exePath, err
}
// 处理相对路径
if !filepath.IsAbs(exePath) {
exePath, err = filepath.Abs(exePath)
if err != nil {
// 处理转换为绝对路径时的错误
return exePath, err
}
}
// 获取可执行文件所在目录的绝对路径
exeDir := filepath.Dir(exePath)
return exeDir, err
}