golang的私有化定制自动更新插件
zanbin168
2023-05-16 1849336009bf002b2bf936fedbbf0a09444a9d21
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
// 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"
    "io/ioutil"
    "net/http"
    "os"
    "path/filepath"
    "runtime"
    "strconv"
    "strings"
    "time"
 
    "github.com/apex/log"
    "github.com/c4milo/unpackit"
    "github.com/pkg/errors"
)
 
// 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.
}
 
// InstallTo binary to the given dir.
func (m *Manager) InstallTo(path, dir string) error {
    log.Debugf("unpacking %q", path)
 
    f, err := os.Open(path)
    if err != nil {
        return errors.Wrap(err, "opening tarball")
    }
 
    err = unpackit.Unpack(f, "")
    if err != nil {
        f.Close()
        return errors.Wrap(err, "unpacking tarball")
    }
 
    if err := f.Close(); err != nil {
        return errors.Wrap(err, "closing tarball")
    }
 
    bin := filepath.Join(path, m.Command)
 
    if err := os.Chmod(bin, 0755); err != nil {
        return errors.Wrap(err, "chmod")
    }
 
    dst := filepath.Join(dir, m.Command)
    tmp := dst + ".tmp"
 
    log.Debugf("copy %q to %q", bin, tmp)
    if err := copyFile(tmp, bin); err != nil {
        return errors.Wrap(err, "copying")
    }
 
    if runtime.GOOS == "windows" {
        old := dst + ".old"
        log.Debugf("windows workaround renaming %q to %q", dst, old)
        if err := os.Rename(dst, old); err != nil {
            return errors.Wrap(err, "windows renaming")
        }
    }
 
    log.Debugf("renaming %q to %q", tmp, dst)
    if err := os.Rename(tmp, dst); 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 := os.Executable()
    if err != nil {
        return errors.Wrapf(err, "looking up path of %q", m.Command)
    }
 
    dir := filepath.Dir(bin)
    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 := ioutil.TempFile(os.TempDir(), "update-")
    if err != nil {
        return "", errors.Wrap(err, "creating temp file")
    }
 
    log.Debugf("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"))
    log.Debugf("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)
    }
 
    log.Debugf("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")
    }
 
    log.Debugf("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
}