-
Notifications
You must be signed in to change notification settings - Fork 3
/
bintool.go
427 lines (356 loc) · 10.8 KB
/
bintool.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package bintool
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/cheggaaa/pb/v3"
"github.com/mattn/go-isatty"
"github.com/princjef/mageutil/shellcmd"
)
type (
// BinTool represents a single binary tool/version combination with the
// information needed to check and/or install the tool if desired.
//
// The struct provides a set of utilities for checking the existence of a
// valid version of the command and installing the command in a way that is
// scoped to the project at hand, if desired.
BinTool struct {
folder string
versionCmd string
url string
tmplData templateData
goInstall bool
}
templateData struct {
GOOS string
GOARCH string
Version string
Cmd string
FullCmd string
ArchiveExt string
BinExt string
}
// Option configures a BinTool with optional settings
Option func(t *BinTool) error
)
// Must provides a utility for asserting that methods returning a BinTool and an
// error have no error. If there is an error, this call will panic.
func Must(t *BinTool, err error) *BinTool {
if err != nil {
panic(err)
}
return t
}
// New initializes a BinTool with the provided command, version, and download
// url. Additional options may be provided to configure things such as the
// version test command, file extensions and folder containing the binary tool.
//
// The command, url, and version command may all use text templates to define
// their formats. If any of these templates fails to compile or evaluate, this
// call will return an error.
func New(command, version, url string, opts ...Option) (*BinTool, error) {
return new(command, version, url, opts...)
}
// NewGo initializes a BinTool with the provided go package and version.
// Additional options may be provided to configure things such as the version
// test command and folder containing the binary tool.
func NewGo(pkg, version string, opts ...Option) (*BinTool, error) {
cmd := fmt.Sprintf("%s{{.BinExt}}", filepath.Base(pkg))
t, err := new(cmd, version, pkg, opts...)
if err != nil {
return nil, err
}
t.goInstall = true
return t, nil
}
func new(command, version, url string, opts ...Option) (*BinTool, error) {
t := &BinTool{
folder: "./bin",
versionCmd: "{{.FullCmd}} --version",
tmplData: templateData{
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Version: version,
ArchiveExt: ".tar.gz",
BinExt: "",
},
}
if runtime.GOOS == "windows" {
t.tmplData.ArchiveExt = ".zip"
t.tmplData.BinExt = ".exe"
}
for _, opt := range opts {
if err := opt(t); err != nil {
return nil, err
}
}
t.folder = filepath.FromSlash(t.folder)
var err error
t.tmplData.Cmd, err = resolveTemplate(command, t.tmplData)
if err != nil {
return nil, err
}
t.tmplData.FullCmd = filepath.Join(t.folder, t.tmplData.Cmd)
t.versionCmd, err = resolveTemplate(t.versionCmd, t.tmplData)
if err != nil {
return nil, err
}
t.url, err = resolveTemplate(url, t.tmplData)
if err != nil {
return nil, err
}
return t, nil
}
// IsInstalled checks whether the correct version of the tool is currently
// installed as defined by the version command.
func (t *BinTool) IsInstalled() bool {
if t.versionCmd == "" {
return t.checkCommand()
}
return t.checkVersion()
}
func (t *BinTool) checkVersion() bool {
args := strings.Split(t.versionCmd, " ")
out, err := exec.Command(args[0], args[1:]...).Output()
if err != nil {
return false
}
byteVersion := []byte(t.tmplData.Version)
i := bytes.Index(out, byteVersion)
switch {
case i == -1:
return false
case i == 0 && len(byteVersion) == len(out):
return true
case i == 0 && !isAlphanumeric(out[len(byteVersion)]):
return true
case i+len(byteVersion) == len(out) && !isAlphanumeric(out[i-1]):
return true
case !isAlphanumeric(out[i-1]) && !isAlphanumeric(out[i+len(byteVersion)]):
return true
default:
return false
}
}
func (t *BinTool) checkCommand() bool {
_, err := os.Stat(t.tmplData.FullCmd)
return err == nil
}
// Install unconditionally downloads and installs the tool to the configured
// folder.
//
// If you don't want to download the tool every time, you may prefer Ensure()
// instead.
func (t *BinTool) Install() error {
if t.goInstall {
return t.installGo()
}
return t.installBinary()
}
// Ensure checks to see if a valid version of the tool is installed, and
// downloads/installs it if it isn't already.
func (t *BinTool) Ensure() error {
if t.IsInstalled() {
return nil
}
return t.Install()
}
// Command generates a runnable command using this binary tool along with the
// provided args.
func (t *BinTool) Command(args string) shellcmd.Command {
return shellcmd.Command(fmt.Sprintf("%s %s", t.tmplData.FullCmd, args))
}
// WithFolder defines a custom folder path where the tool is expected to exist
// and where it should be installed if desired. Paths will be normalized to the
// operating system automatically, so unix-style paths are recommended.
func WithFolder(folder string) Option {
return func(t *BinTool) error {
t.folder = folder
return nil
}
}
// WithArchiveExt defines a custom extension to use when identifying an archive
// via the ArchiveExt template variable. The default archive extension is
// .tar.gz except for Windows, where it is .zip.
func WithArchiveExt(ext string) Option {
return func(t *BinTool) error {
t.tmplData.ArchiveExt = ext
return nil
}
}
// WithBinExt defines a custom extension to use when identifying a binary
// executable via the BinExt template variable. The default binary extension is
// empty for all operating systems except Windows, where it is .exe.
func WithBinExt(ext string) Option {
return func(t *BinTool) error {
t.tmplData.BinExt = ext
return nil
}
}
// WithVersionCmd defines a custom command used to test the version of the
// command for purposes of determining if the command is installed. The provided
// command is a template that can use any of the template parameters that are
// available to the url. If no command is provided, the version check will be
// skipped.
//
// The default test command is "{{.FullCmd}} --version".
func WithVersionCmd(cmd string) Option {
return func(t *BinTool) error {
t.versionCmd = cmd
return nil
}
}
func resolveTemplate(templateString string, tmplData templateData) (string, error) {
tmpl, err := template.New("cmd").Parse(templateString)
if err != nil {
return "", fmt.Errorf("bintool: invalid template: %w", err)
}
var sb strings.Builder
err = tmpl.Execute(&sb, tmplData)
if err != nil {
return "", fmt.Errorf("bintool: unable to execute template: %w", err)
}
return sb.String(), nil
}
func isAlphanumeric(b byte) bool {
return (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9')
}
func (t *BinTool) installBinary() error {
data, err := downloadAndExtract(t.tmplData.Cmd, t.url)
if err != nil {
return err
}
if err := os.MkdirAll(t.folder, 0755); err != nil {
return fmt.Errorf("bintool: unable to create destination folder %s: %w", t.folder, err)
}
if err := ioutil.WriteFile(t.tmplData.FullCmd, data, 0755); err != nil {
return fmt.Errorf("bintool: unable to write executable file %s: %w", t.tmplData.FullCmd, err)
}
return nil
}
func downloadAndExtract(command string, url string) (data []byte, err error) {
fmt.Printf("Downloading %s\n", command)
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("bintool: unable to download executable: %w", err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, fmt.Errorf("bintool: received unexpected response when downloading file: %d", res.StatusCode)
}
r, finish := progress(res.Body, res.ContentLength)
defer finish()
switch {
case strings.HasSuffix(url, ".tar.gz"):
return extractTarFile(r, command)
case strings.HasSuffix(url, ".zip"):
return extractZipFile(r, command)
default:
// Assume non-archive for others
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, fmt.Errorf("bintool: failed to retrieve body: %w", err)
}
return buf.Bytes(), nil
}
}
func progress(r io.Reader, size int64) (io.Reader, func()) {
if !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()) {
return r, func() {}
}
tmpl := `{{string . "prefix"}}{{counters . }}` +
` {{bar . "[" "=" ">" " " "]" }} {{percent . }}` +
` {{speed . "%s/s" }}{{string . "suffix"}}`
bar := pb.New64(size).
SetTemplate(pb.ProgressBarTemplate(tmpl)).
SetRefreshRate(time.Second / 60).
SetMaxWidth(100).
Start()
return bar.NewProxyReader(r), func() { bar.Finish() }
}
func extractTarFile(r io.Reader, filename string) (data []byte, err error) {
var buf bytes.Buffer
zr, err := gzip.NewReader(r)
if err != nil {
return nil, fmt.Errorf("bintool: unable to read as gzip: %w", err)
}
tr := tar.NewReader(zr)
for {
header, err := tr.Next()
if err == io.EOF {
return nil, errors.New("bintool: no executable found in archive")
}
if err != nil {
return nil, fmt.Errorf("bintool: unable to read tar archive: %w", err)
}
name := filepath.Base(header.Name)
if name != filename {
continue
}
if _, err := io.Copy(&buf, tr); err != nil {
return nil, fmt.Errorf("bintool: unable to copy executable out of archive: %w", err)
}
break
}
return buf.Bytes(), nil
}
func extractZipFile(r io.Reader, filename string) (data []byte, err error) {
var rawBuf bytes.Buffer
var buf bytes.Buffer
if _, err := io.Copy(&rawBuf, r); err != nil {
return nil, fmt.Errorf("bintool: unable to copy zip contents into buffer: %w", err)
}
zr, err := zip.NewReader(bytes.NewReader(rawBuf.Bytes()), int64(len(rawBuf.Bytes())))
if err != nil {
return nil, fmt.Errorf("bintool: unable to read as zip: %w", err)
}
var found bool
for _, f := range zr.File {
name := filepath.Base(f.Name)
if name != filename {
continue
}
fc, err := f.Open()
if err != nil {
return nil, fmt.Errorf("bintool: unable to open executable in zip archive: %w", err)
}
defer fc.Close()
if _, err := io.Copy(&buf, fc); err != nil {
return nil, fmt.Errorf("bintool: unable to copy executable out of archive: %w", err)
}
found = true
break
}
if !found {
return nil, errors.New("bintool: no executable found in archive")
}
return buf.Bytes(), nil
}
func (t *BinTool) installGo() error {
path, err := filepath.Abs(t.folder)
if err != nil {
return err
}
if err := os.MkdirAll(t.folder, 0755); err != nil {
return fmt.Errorf("bintool: unable to create destination folder %s: %w", t.folder, err)
}
fmt.Printf("Installing %s\n", t.tmplData.Cmd)
goInstall := fmt.Sprintf("GOBIN=%s go install %s@%s", path, t.url, t.tmplData.Version)
if _, err := shellcmd.Command(goInstall).Output(); err != nil {
return fmt.Errorf("bintool: unable to install executable: %w", err)
}
return nil
}