-
Notifications
You must be signed in to change notification settings - Fork 43
/
eget.go
565 lines (497 loc) · 12.7 KB
/
eget.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"github.com/jessevdk/go-flags"
pb "github.com/schollz/progressbar/v3"
)
func fatal(a ...interface{}) {
fmt.Fprintln(os.Stderr, a...)
os.Exit(1)
}
// IsUrl returns true if s is a valid URL.
func IsUrl(s string) bool {
u, err := url.Parse(s)
return err == nil && u.Scheme != "" && u.Host != ""
}
// Cut is strings.Cut
func Cut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
var ghrgx = regexp.MustCompile(`^(http(s)?://)?github\.com/[\w,\-,_]+/[\w,\-,_]+(.git)?(/)?$`)
// IsGithubUrl returns true if s is a URL with github.com as the host.
func IsGithubUrl(s string) bool {
return ghrgx.MatchString(s)
}
func IsLocalFile(s string) bool {
_, err := os.Stat(s)
return err == nil
}
// IsDirectory returns true if the file at 'path' is a directory.
func IsDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
// searches for an asset thaat has the same name as the requested one but
// ending with .sha256 or .sha256sum
func checksumAsset(asset string, assets []string) string {
for _, a := range assets {
if a == asset+".sha256sum" || a == asset+".sha256" {
return a
}
}
return ""
}
// Determine the appropriate Finder to use. If opts.URL is provided, we use
// a DirectAssetFinder. Otherwise we use a GithubAssetFinder. When a Github
// repo is provided, we assume the repo name is the 'tool' name (for direct
// URLs, the tool name is unknown and remains empty).
func getFinder(project string, opts *Flags) (finder Finder, tool string) {
if IsLocalFile(project) || (IsUrl(project) && !IsGithubUrl(project)) {
finder = &DirectAssetFinder{
URL: project,
}
opts.System = "all"
} else {
if IsGithubUrl(project) {
_, after, found := Cut(project, "github.com/")
if found {
project = strings.Trim(after, "/")
} else {
fatal(fmt.Sprintf("invalid GitHub repo URL %s", project))
}
}
repo := project
if strings.Count(repo, "/") != 1 {
fatal("invalid argument (must be of the form `user/repo`)")
}
parts := strings.Split(repo, "/")
if parts[0] == "" || parts[1] == "" {
fatal("invalid argument (must be of the form `user/repo`)")
}
tool = parts[1]
if opts.Source {
tag := "master"
if opts.Tag != "" {
tag = opts.Tag
}
finder = &GithubSourceFinder{
Repo: repo,
Tag: tag,
Tool: tool,
}
} else {
tag := "latest"
if opts.Tag != "" {
tag = fmt.Sprintf("tags/%s", opts.Tag)
}
var mint time.Time
if opts.UpgradeOnly {
parts := strings.Split(project, "/")
last := parts[len(parts)-1]
mint = bintime(last, opts.Output)
}
finder = &GithubAssetFinder{
Repo: repo,
Tag: tag,
Prerelease: opts.Prerelease,
MinTime: mint,
}
}
}
return finder, tool
}
func getVerifier(sumAsset string, opts *Flags) (verifier Verifier, err error) {
if opts.Verify != "" {
verifier, err = NewSha256Verifier(opts.Verify)
} else if sumAsset != "" {
verifier = &Sha256AssetVerifier{
AssetURL: sumAsset,
}
} else if opts.Hash {
verifier = &Sha256Printer{}
} else {
verifier = &NoVerifier{}
}
return verifier, err
}
// Determine the appropriate detector. If the --system is 'all', we use an
// AllDetector, which will just return all assets. Otherwise we use the
// --system pair provided by the user, or the runtime.GOOS/runtime.GOARCH
// pair by default (the host system OS/Arch pair).
func getDetector(opts *Flags) (detector Detector, err error) {
var system Detector
if opts.System == "all" {
system = &AllDetector{}
} else if opts.System != "" {
split := strings.Split(opts.System, "/")
if len(split) < 2 {
fatal("system descriptor must be os/arch")
}
system, err = NewSystemDetector(split[0], split[1])
} else {
system, err = NewSystemDetector(runtime.GOOS, runtime.GOARCH)
}
if len(opts.Asset) >= 1 {
detectors := make([]Detector, len(opts.Asset))
for i, a := range opts.Asset {
anti := strings.HasPrefix(a, "^")
if anti {
a = a[1:]
}
detectors[i] = &SingleAssetDetector{
Asset: a,
Anti: anti,
}
}
detector = &DetectorChain{
detectors: detectors,
system: system,
}
} else {
detector = system
}
return detector, err
}
// Determine which extractor to use. If --download-only is provided, we
// just "extract" the downloaded archive to itself. Otherwise we try to
// extract the literal file provided by --file, or by default we just
// extract a binary with the tool name that was possibly auto-detected
// above.
func getExtractor(url, tool string, opts *Flags) (extractor Extractor, err error) {
if opts.DLOnly {
extractor = &SingleFileExtractor{
Name: path.Base(url),
Rename: path.Base(url),
Decompress: func(r io.Reader) (io.Reader, error) {
return r, nil
},
}
} else if opts.ExtractFile != "" {
gc, err := NewGlobChooser(opts.ExtractFile)
if err != nil {
return nil, err
}
extractor = NewExtractor(path.Base(url), tool, gc)
} else {
extractor = NewExtractor(path.Base(url), tool, &BinaryChooser{
Tool: tool,
})
}
return extractor, nil
}
// Write an extracted file to disk with a new name.
func writeFile(data []byte, rename string, mode fs.FileMode) error {
if rename[0] == '-' {
// if the output is '-', just print it to stdout
_, err := os.Stdout.Write(data)
return err
}
// remove file if it exists already
os.Remove(rename)
// make parent directories if necessary
os.MkdirAll(filepath.Dir(rename), 0755)
f, err := os.OpenFile(rename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
return err
}
// Would really like generics to implement this...
// Make the user select one of the choices and return the index of the
// selection.
func userSelect(choices []interface{}) int {
for i, c := range choices {
fmt.Fprintf(os.Stderr, "(%d) %v\n", i+1, c)
}
var choice int
for {
fmt.Fprint(os.Stderr, "Enter selection number: ")
_, err := fmt.Scanf("%d", &choice)
if err == nil && (choice <= 0 || choice > len(choices)) {
err = fmt.Errorf("%d is out of bounds", choice)
}
if err == nil {
break
}
if errors.Is(err, io.EOF) {
fatal("Error reading selection")
}
fmt.Fprintf(os.Stderr, "Invalid selection: %v\n", err)
}
return choice
}
func bintime(bin string, to string) (t time.Time) {
file := ""
dir := "."
if to != "" && IsDirectory(to) {
// direct directory
dir = to
} else if ebin := os.Getenv("EGET_BIN"); ebin != "" {
dir = ebin
}
if to != "" && !strings.ContainsRune(to, os.PathSeparator) {
// path joined possible with eget bin
bin = to
} else if to != "" && !IsDirectory(to) {
// direct path
file = to
}
if file == "" {
file = filepath.Join(dir, bin)
}
fi, err := os.Stat(file)
if err != nil {
return
}
return fi.ModTime()
}
func downloadConfigRepositories(config *Config) error {
hasError := false
errorList := []error{}
binary, err := os.Executable()
if err != nil {
binary = os.Args[0]
}
for name, _ := range config.Repositories {
cmd := exec.Command(binary, name)
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
hasError = true
errorList = append(errorList, err)
}
}
if hasError {
return fmt.Errorf("one or more errors occurred while downloading: %v", errorList)
}
return nil
}
var opts Flags
func main() {
var cli CliFlags
flagparser := flags.NewParser(&cli, flags.PassDoubleDash|flags.PrintErrors)
flagparser.Usage = "[OPTIONS] TARGET"
args, err := flagparser.Parse()
if err != nil {
os.Exit(1)
}
if cli.Version {
fmt.Println("eget version", Version)
os.Exit(0)
}
if cli.Help {
flagparser.WriteHelp(os.Stdout)
os.Exit(0)
}
config, err := InitializeConfig()
if err != nil {
fatal(err)
}
err = SetGlobalOptionsFromConfig(config, flagparser, &opts, cli)
if err != nil {
fatal(err)
}
if cli.Rate {
rdat, err := GetRateLimit()
if err != nil {
fatal(err)
}
fmt.Println(rdat)
os.Exit(0)
}
target := ""
if len(args) > 0 {
target = args[0]
}
err = SetProjectOptionsFromConfig(config, flagparser, &opts, cli, target)
if err != nil {
fatal(err)
}
if cli.DownloadAll {
err = downloadConfigRepositories(config)
if err != nil {
fatal(err)
}
os.Exit(0)
}
if len(args) <= 0 {
fmt.Println("no target given")
flagparser.WriteHelp(os.Stdout)
os.Exit(0)
}
if opts.DisableSSL {
fmt.Fprintln(os.Stderr, "warning: SSL verification is disabled")
}
if opts.Remove {
ebin := os.Getenv("EGET_BIN")
err := os.Remove(filepath.Join(ebin, target))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("Removed `%s`\n", filepath.Join(ebin, target))
os.Exit(0)
}
// when --quiet is passed, send non-essential output to io.Discard
var output io.Writer = os.Stderr
if opts.Quiet {
output = io.Discard
}
finder, tool := getFinder(target, &opts)
assets, err := finder.Find()
if err != nil {
if errors.Is(err, ErrNoUpgrade) {
fmt.Fprintf(output, "%s: %v\n", target, err)
os.Exit(0)
}
fatal(err)
}
detector, err := getDetector(&opts)
if err != nil {
fatal(err)
}
// get the url and candidates from the detector
url, candidates, err := detector.Detect(assets)
if len(candidates) != 0 && err != nil {
// if multiple candidates are returned, the user must select manually which one to download
fmt.Fprintf(os.Stderr, "%v: please select manually\n", err)
choices := make([]interface{}, len(candidates))
for i := range candidates {
choices[i] = path.Base(candidates[i])
}
choice := userSelect(choices)
url = candidates[choice-1]
} else if err != nil {
fatal(err)
}
// print the URL
fmt.Fprintf(output, "%s\n", url)
// download with progress bar
buf := &bytes.Buffer{}
err = Download(url, buf, func(size int64) *pb.ProgressBar {
var pbout io.Writer = os.Stderr
if opts.Quiet {
pbout = io.Discard
}
return pb.NewOptions64(size,
pb.OptionSetWriter(pbout),
pb.OptionShowBytes(true),
pb.OptionSetWidth(10),
pb.OptionThrottle(65*time.Millisecond),
pb.OptionShowCount(),
pb.OptionSpinnerType(14),
pb.OptionFullWidth(),
pb.OptionSetDescription("Downloading"),
pb.OptionOnCompletion(func() {
fmt.Fprint(pbout, "\n")
}),
pb.OptionSetTheme(pb.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
})
if err != nil {
fatal(fmt.Sprintf("%s (URL: %s)", err, url))
}
body := buf.Bytes()
sumAsset := checksumAsset(url, assets)
verifier, err := getVerifier(sumAsset, &opts)
if err != nil {
fatal(err)
}
err = verifier.Verify(body)
if err != nil {
fatal(err)
} else if opts.Verify == "" && sumAsset != "" {
fmt.Fprintf(output, "Checksum verified with %s\n", path.Base(sumAsset))
} else if opts.Verify != "" {
fmt.Fprintf(output, "Checksum verified\n")
}
extractor, err := getExtractor(url, tool, &opts)
if err != nil {
fatal(err)
}
// get extraction candidates
bin, bins, err := extractor.Extract(body, opts.All)
if len(bins) != 0 && err != nil && !opts.All {
// if there are multiple candidates, have the user select manually
fmt.Fprintf(os.Stderr, "%v: please select manually\n", err)
choices := make([]interface{}, len(bins)+1)
for i := range bins {
choices[i] = bins[i]
}
choices[len(bins)] = "all"
choice := userSelect(choices)
if choice == len(bins)+1 {
opts.All = true
} else {
bin = bins[choice-1]
}
} else if err != nil && len(bins) == 0 {
fatal(err)
}
if len(bins) == 0 {
bins = []ExtractedFile{bin}
}
extract := func(bin ExtractedFile) {
mode := bin.Mode()
// write the extracted file to a file on disk, in the --to directory if
// requested
out := filepath.Base(bin.Name)
if opts.Output == "-" {
out = "-"
} else if opts.Output != "" && IsDirectory(opts.Output) {
out = filepath.Join(opts.Output, out)
} else if opts.Output != "" && opts.All {
os.MkdirAll(opts.Output, 0755)
out = filepath.Join(opts.Output, out)
} else {
if opts.Output != "" {
out = opts.Output
}
// only use $EGET_BIN if all of the following are true
// 1. $EGET_BIN is non-empty
// 2. --to is not a path (not a path if no path separator is found)
// 3. The extracted file is executable
if os.Getenv("EGET_BIN") != "" && !strings.ContainsRune(out, os.PathSeparator) && mode&0111 != 0 && !bin.Dir {
out = filepath.Join(os.Getenv("EGET_BIN"), out)
}
}
err = bin.Extract(out)
if err != nil {
fatal(err)
}
fmt.Fprintf(output, "Extracted `%s` to `%s`\n", bin.ArchiveName, out)
}
if opts.All {
for _, bin := range bins {
extract(bin)
}
} else {
extract(bin)
}
}