-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
80 lines (63 loc) · 1.75 KB
/
cli.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
const LOCAL_BIN_PATH = "/usr/local/bin"
const usage = `usage: %s [OPTIONS] <URL>
skill -dest bin https://dl.bintray......package.zip
skill - teach your system a new trick.
It downloads (in-memory) the archive at the given url and unpack its content
where you decide.
OPTIONS:
`
type ShortParser func(string) string
func parseRawGithub(shortcut string) string {
partials := strings.Split(shortcut, "@")
user_project := partials[0]
version := partials[1]
// usage: -short rawgh "rupa/z@master/z.sh"
return fmt.Sprintf("https://raw.githubusercontent.com/%s/%s", user_project, version)
}
func parseGithub(shortcut string) string {
partials := strings.Split(shortcut, "@")
user_project := partials[0]
version := partials[1]
return fmt.Sprintf("https://github.com/%s/releases/download/%s", user_project, version)
}
// TODO -rename ? or detect from -out ?
type Options struct {
URL string
Shortcut string
Out string
// TODO "github.com/jbenet/go-multihash"
Checksum string
}
func getOpts() *Options {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage, os.Args[0])
flag.PrintDefaults()
}
var opts = new(Options)
flag.StringVar(&opts.Out, "out", LOCAL_BIN_PATH, "Directory to unpack archive files")
flag.StringVar(&opts.Checksum, "checksum", "", "Archive checksum; leave blank to skip this step")
flag.StringVar(&opts.Shortcut, "short", "", "extrapolate the given url (gh|rawgh)")
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
switch opts.Shortcut {
case "gh":
opts.URL = parseGithub(flag.Arg(0))
case "rawgh":
opts.URL = parseRawGithub(flag.Arg(0))
default:
log.Printf("no shortcut provided, using as is")
opts.URL = flag.Arg(0)
}
return opts
}