-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
147 lines (129 loc) · 2.81 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
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
package main
import (
"fmt"
"github.com/bgentry/pflag"
"io"
"io/ioutil"
"os"
"strings"
)
type Command struct {
Usage string
Flag pflag.FlagSet
Short string
Long string
Run func(cmd *Command, args []string)
}
func (c *Command) Name() string {
name := c.Usage
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
func printOverviewUsage(w io.Writer) {
fmt.Fprintf(w, "Usage: zk <command> [options] [arguments]\n")
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Commands:\n")
for _, command := range commands {
fmt.Fprintf(w, " %-8s %s\n", command.Name(), command.Short)
}
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Run 'zk help <command>' for details.\n")
}
func printLongUsage(c *Command) {
fmt.Printf("Usage: zk %s\n\n", c.Usage)
fmt.Println(strings.Trim(c.Long, "\n"))
}
func printAbbrevUsage(c *Command) {
fmt.Fprintf(os.Stderr, "Usage: zk %s\n", c.Usage)
fmt.Fprintf(os.Stderr, "Run 'zk help %s' for details.\n", c.Name())
}
var cmdHelp = &Command{
Usage: "help [<command>]",
Short: "show help",
Long: `
Help shows usage details for a single command if one is given, or
overview usage if no command is specified.`,
Run: runHelp,
}
func runHelp(cmd *Command, args []string) {
if len(args) > 1 {
fmt.Fprintf(os.Stderr, "error: too many arguments")
os.Exit(2)
}
if len(args) == 0 {
printOverviewUsage(os.Stdout)
return
}
for _, c := range commands {
if c.Name() == args[0] {
printLongUsage(c)
return
}
}
fmt.Fprintf(os.Stderr, "error: unrecognized command: %s\n", args[0])
fmt.Fprintf(os.Stderr, "Run 'zk help' for usage.\n")
os.Exit(2)
}
var commands []*Command
func init() {
commands = []*Command{
cmdExists,
cmdStat,
cmdGet,
cmdCreate,
cmdSet,
cmdDelete,
cmdChildren,
cmdHelp,
}
}
func main() {
args := os.Args[1:]
if len(args) < 1 || strings.IndexRune(args[0], '-') == 0 {
printOverviewUsage(os.Stderr)
os.Exit(2)
}
for _, cmd := range commands {
if cmd.Name() == args[0] {
cmd.Flag.SetInterspersed(true)
cmd.Flag.Usage = func() {
printAbbrevUsage(cmd)
}
if err := cmd.Flag.Parse(args[1:]); err == pflag.ErrHelp {
cmdHelp.Run(cmdHelp, args[:1])
return
} else if err != nil {
os.Exit(2)
}
cmd.Run(cmd, cmd.Flag.Args())
return
}
}
fmt.Fprintf(os.Stderr, "error: unrecognized command: %s\n", args[0])
fmt.Fprintf(os.Stderr, "Run 'zk help' for usage.\n")
os.Exit(2)
}
func failUsage(cmd *Command) {
printAbbrevUsage(cmd)
os.Exit(2)
}
func must(err error) {
if err != nil {
errString := strings.TrimPrefix(err.Error(), "zk: ")
fmt.Fprintf(os.Stderr, "error: %s\n", errString)
os.Exit(1)
}
}
func inData() []byte {
data, _ := ioutil.ReadAll(os.Stdin)
return data
}
func outString(p string, args ...interface{}) {
fmt.Fprintf(os.Stdout, p, args...)
}
func outData(d []byte) {
os.Stdout.Write(d)
}