-
Notifications
You must be signed in to change notification settings - Fork 11
/
binwrapper.go
455 lines (351 loc) · 8.57 KB
/
binwrapper.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Package binwrapper provides executable wrapper that makes command line tools seamlessly available as local golang dependencies.
// Inspired by and partially ported from npm package bin-wrapper: https://github.com/kevva/bin-wrapper
package binwrapper
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/mholt/archiver"
)
// Src defines executable source
type Src struct {
url string
os string
arch string
execPath string
}
// BinWrapper wraps executable and provides convenient methods to interact with
type BinWrapper struct {
src []*Src
dest string
execPath string
strip int
output []byte
autoExe bool
stdErr []byte
stdOut []byte
stdIn io.Reader
stdOutWriter io.Writer
args []string
env []string
debug bool
cmd *exec.Cmd
timeout time.Duration
}
// NewSrc creates new Src instance
func NewSrc() *Src {
return &Src{}
}
// URL sets a url pointing to a file to download.
func (s *Src) URL(value string) *Src {
s.url = value
return s
}
// Os tie the source to a specific OS. Possible values are same as runtime.GOOS
func (s *Src) Os(value string) *Src {
s.os = value
return s
}
// Arch tie the source to a specific arch. Possible values are same as runtime.GOARCH
func (s *Src) Arch(value string) *Src {
s.arch = value
return s
}
// ExecPath tie the src to a specific binary file
func (s *Src) ExecPath(value string) *Src {
s.execPath = value
return s
}
// NewBinWrapper creates BinWrapper instance
func NewBinWrapper() *BinWrapper {
return &BinWrapper{}
}
// Src adds a Src to BinWrapper
func (b *BinWrapper) Src(src *Src) *BinWrapper {
b.src = append(b.src, src)
return b
}
// Timeout sets timeout for the command. By default it's 0 (binary will run till end).
func (b *BinWrapper) Timeout(timeout time.Duration) *BinWrapper {
b.timeout = timeout
return b
}
// Dest accepts a path which the files will be downloaded to
func (b *BinWrapper) Dest(dest string) *BinWrapper {
b.dest = dest
return b
}
// ExecPath define a file to use as the binary
func (b *BinWrapper) ExecPath(execPath string) *BinWrapper {
if b.autoExe && runtime.GOOS == "windows" {
ext := strings.ToLower(filepath.Ext(execPath))
if ext != ".exe" {
execPath += ".exe"
}
}
b.execPath = execPath
return b
}
// AutoExe adds .exe extension for windows executable path
func (b *BinWrapper) AutoExe() *BinWrapper {
b.autoExe = true
return b.ExecPath(b.execPath)
}
// SkipDownload skips downloading a file
func (b *BinWrapper) SkipDownload() *BinWrapper {
b.src = nil
return b
}
// Strip strips a number of leading paths from file names on extraction.
func (b *BinWrapper) Strip(value int) *BinWrapper {
b.strip = value
return b
}
// Arg adds command line argument to run the binary with.
func (b *BinWrapper) Arg(name string, values ...string) *BinWrapper {
values = append([]string{name}, values...)
b.args = append(b.args, values...)
return b
}
// Debug enables debug output
func (b *BinWrapper) Debug() *BinWrapper {
b.debug = true
return b
}
// Args returns arguments were added with Arg method
func (b *BinWrapper) Args() []string {
return b.args
}
// Path returns the full path to the binary
func (b *BinWrapper) Path() string {
src := osFilterObj(b.src)
if src != nil && src.execPath != "" {
b.ExecPath(src.execPath)
}
if b.dest == "." {
return b.dest + string(filepath.Separator) + b.execPath
}
return filepath.Join(b.dest, b.execPath)
}
// StdIn sets reader to read executable's stdin from
func (b *BinWrapper) StdIn(reader io.Reader) *BinWrapper {
b.stdIn = reader
return b
}
// StdOut returns the binary's stdout after Run was called
func (b *BinWrapper) StdOut() []byte {
return b.stdOut
}
// CombinedOutput returns combined executable's stdout and stderr
func (b *BinWrapper) CombinedOutput() []byte {
return append(b.stdOut, b.stdErr...)
}
// SetStdOut set writer to write executable's stdout
func (b *BinWrapper) SetStdOut(writer io.Writer) *BinWrapper {
b.stdOutWriter = writer
return b
}
// Env specifies the environment of the executable.
// If Env is nil, Run uses the current process's environment.
// Elements of env should be in form: "ENV_VARIABLE_NAME=value"
func (b *BinWrapper) Env(env []string) *BinWrapper {
b.env = env
return b
}
// StdErr returns the executable's stderr after Run was called
func (b *BinWrapper) StdErr() []byte {
return b.stdErr
}
// Reset removes all arguments set with Arg method, cleans StdOut and StdErr
func (b *BinWrapper) Reset() *BinWrapper {
b.args = []string{}
b.stdOut = nil
b.stdErr = nil
b.stdIn = nil
b.stdOutWriter = nil
b.env = nil
b.cmd = nil
return b
}
// Run runs the binary with provided arg list.
// Arg list is appended to args set through Arg method
// Returns context.DeadlineExceeded in case of timeout
func (b *BinWrapper) Run(arg ...string) error {
if b.src != nil && len(b.src) > 0 {
err := b.findExisting()
if err != nil {
return err
}
}
arg = append(b.args, arg...)
if b.debug {
fmt.Println("BinWrapper.Run: " + b.Path() + " " + strings.Join(arg, " "))
}
var ctx context.Context
var cancel context.CancelFunc
if b.timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), b.timeout)
} else {
ctx = context.Background()
cancel = func() {
}
}
defer cancel()
b.cmd = exec.CommandContext(ctx, b.Path(), arg...)
if b.env != nil {
b.cmd.Env = b.env
}
if b.stdIn != nil {
b.cmd.Stdin = b.stdIn
}
var stdout io.Reader
if b.stdOutWriter != nil {
b.cmd.Stdout = b.stdOutWriter
} else {
stdout, _ = b.cmd.StdoutPipe()
}
stderr, _ := b.cmd.StderrPipe()
err := b.cmd.Start()
if err != nil {
return err
}
if stdout != nil {
b.stdOut, _ = ioutil.ReadAll(stdout)
}
b.stdErr, _ = ioutil.ReadAll(stderr)
err = b.cmd.Wait()
if ctx.Err() == context.DeadlineExceeded {
return context.DeadlineExceeded
}
return err
}
// Kill terminates the process
func (b *BinWrapper) Kill() error {
if b.cmd != nil && b.cmd.Process != nil {
return b.cmd.Process.Kill()
}
return nil
}
func (b *BinWrapper) findExisting() error {
_, err := os.Stat(b.Path())
if os.IsNotExist(err) {
fmt.Printf("%s not found. Downloading...\n", b.Path())
return b.download()
} else if err != nil {
return err
} else {
return nil
}
}
func (b *BinWrapper) download() error {
src := osFilterObj(b.src)
if src == nil {
return errors.New("No binary found matching your system. It's probably not supported")
}
file, err := b.downloadFile(src.url)
if err != nil {
return err
}
fmt.Printf("%s downloaded. Trying to extract...\n", file)
err = b.extractFile(file)
if err != nil {
return err
}
if src.execPath != "" {
b.ExecPath(src.execPath)
}
return nil
}
func (b *BinWrapper) extractFile(file string) error {
defer os.Remove(file)
err := archiver.Unarchive(file, b.dest)
if err != nil {
fmt.Printf("%s is not an archive or have unsupported archive format\n", file)
return err
}
if b.strip == 0 {
return nil
}
return b.stripDir()
}
func (b *BinWrapper) stripDir() error {
dir := b.dest
var dirsToRemove []string
for i := 0; i < b.strip; i++ {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, v := range files {
if v.IsDir() {
if dir != b.dest {
dirsToRemove = append(dirsToRemove, dir)
}
dir = filepath.Join(dir, v.Name())
break
}
}
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, v := range files {
err := os.Rename(filepath.Join(dir, v.Name()), filepath.Join(b.dest, v.Name()))
if err != nil {
return err
}
}
for _, v := range dirsToRemove {
os.RemoveAll(v)
}
return nil
}
func (b *BinWrapper) downloadFile(value string) (string, error) {
if b.dest == "" {
b.dest = "."
}
err := os.MkdirAll(b.dest, 0755)
if err != nil {
return "", err
}
fileURL, err := url.Parse(value)
if err != nil {
return "", err
}
path := fileURL.Path
segments := strings.Split(path, "/")
fileName := segments[len(segments)-1]
fileName = filepath.Join(b.dest, fileName)
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return "", err
}
defer file.Close()
check := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := check.Get(value)
if err != nil {
return "", err
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode < 400) {
return "", errors.New("Unable to download " + value)
}
_, err = io.Copy(file, resp.Body)
return fileName, err
}