-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
85 lines (79 loc) · 1.65 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
)
var (
ErrNotADir = errors.New("Not a directory")
)
// Command line arguments:
var (
keyringPath = flag.String(
"keyring",
os.Getenv("HOME")+"/.sandstorm-keyring",
"Path to sandstorm keyring",
)
)
// If the error is not nil, display an error message to the user based on
// `context` and `err`, and exit the with a failing status.
func chkfatal(context string, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", context, err)
os.Exit(1)
}
}
// Report a usage error to the user. Displays the string `info` and the
// documentation for the command line arguments, and exits with a failing
// status.
func usageErr(info string) {
fmt.Fprintln(os.Stderr, info)
fmt.Fprintln(os.Stderr)
flag.Usage()
os.Exit(1)
}
func main() {
subCommands := map[string]func(){
"pack": packCmd,
"init": initCmd,
"build": buildCmd,
}
flag.Usage = func() {
keys := []string{}
for k, _ := range subCommands {
keys = append(keys, k)
}
fmt.Fprintf(os.Stderr,
"Usage: %s ( %s ) <flags>\n"+
"where <flags> =\n",
os.Args[0],
strings.Join(keys, " | "),
)
flag.PrintDefaults()
os.Exit(1)
}
if len(os.Args) < 2 {
flag.Usage()
}
cmd := os.Args[1]
fn, ok := subCommands[cmd]
if ok {
arg0 := os.Args[0]
// We have to chop of the subcommand or the parser gets confused later:
os.Args = os.Args[1:]
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s %s:\n", arg0, cmd)
flag.PrintDefaults()
}
fn()
return
}
switch cmd {
case "-h", "-help", "--help", "help":
default:
fmt.Fprintf(os.Stderr, "Unknown subcommand: %s\n", os.Args[1])
}
flag.Usage()
}