-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (80 loc) · 1.63 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
86
87
88
89
90
91
92
/*
Package main is the main entrypoint to the datawell application, including
configuration loading.
*/
package main
import (
"flag"
"fmt"
"os"
"strings"
"text/template"
)
var commands = []*Command{
greetCmd,
&serveCmd.Command,
userListCmd,
&userAddCmd.Command,
&userEditCmd.Command,
&userDeleteCmd.Command,
demoCmd,
echoCmd,
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 || args[0] == "-h" || args[0] == "-help" || args[0] == "--help" || args[0] == "help" {
flag.Usage()
return
}
var cmd *Command
var prefixCmds []*Command
name := args[0]
for _, c := range commands {
if c.Name == name {
cmd = c
break
} else if strings.HasPrefix(c.Name, name) {
prefixCmds = append(prefixCmds, c)
}
}
if cmd == nil {
if len(prefixCmds) == 1 {
cmd = prefixCmds[0]
} else {
if len(prefixCmds) > 1 {
names := []string{}
for _, c := range prefixCmds {
names = append(names, c.Name)
}
fmt.Printf("error: %q prefix-matches multiple commands: %v\n", name, names)
} else {
fmt.Printf("error: unknown command %q\n", name)
}
fmt.Printf("run 'datawell help' for usage\n")
os.Exit(1)
}
}
cmd.Exec(args[1:])
}
func usage() {
fmt.Print(usagePrefix)
flag.PrintDefaults()
err := usageTmpl.Execute(os.Stdout, commands)
if err != nil {
fmt.Println(err)
}
}
var usagePrefix = `
datawell is what EventLog became.
Usage:
datawell [options] <subcommand> [subcommand options]
Options:
`
var usageTmpl = template.Must(template.New("usage").Parse(
`
Subcommands:{{range .}}
{{.Name | printf "%-10s"}} {{.Summary}}
{{.SprintFlagDefaults}}{{end}}
`))