-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
162 lines (134 loc) · 3.54 KB
/
commands.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
package commands
import (
"fmt"
"regexp"
"strings"
"github.com/github/hub/utils"
)
var (
NameRe = `[\w.-]+`
OwnerRe = "[a-zA-Z0-9][a-zA-Z0-9-]*"
NameWithOwnerRe = fmt.Sprintf(`^(%s/)?%s$`, OwnerRe, NameRe)
CmdRunner = NewRunner()
)
type Command struct {
Run func(cmd *Command, args *Args)
Key string
Usage string
Long string
KnownFlags string
GitExtension bool
subCommands map[string]*Command
parentCommand *Command
}
func (c *Command) Call(args *Args) (err error) {
runCommand, err := c.lookupSubCommand(args)
if err != nil {
return
}
if !c.GitExtension {
err = runCommand.parseArguments(args)
if err != nil {
return
}
}
runCommand.Run(runCommand, args)
return
}
type ErrHelp struct {
err string
}
func (e ErrHelp) Error() string {
return e.err
}
func (c *Command) parseArguments(args *Args) error {
knownFlags := c.KnownFlags
if knownFlags == "" {
knownFlags = c.Long
}
args.Flag = utils.NewArgsParserWithUsage("-h, --help\n" + knownFlags)
if rest, err := args.Flag.Parse(args.Params); err == nil {
if args.Flag.Bool("--help") {
return &ErrHelp{err: c.Synopsis()}
}
args.Params = rest
args.Terminator = args.Flag.HasTerminated
return nil
} else {
return fmt.Errorf("%s\n%s", err, c.Synopsis())
}
}
func (c *Command) Use(subCommand *Command) {
if c.subCommands == nil {
c.subCommands = make(map[string]*Command)
}
c.subCommands[subCommand.Name()] = subCommand
subCommand.parentCommand = c
}
func (c *Command) UsageError(msg string) error {
nl := ""
if msg != "" {
nl = "\n"
}
return fmt.Errorf("%s%s%s", msg, nl, c.Synopsis())
}
func (c *Command) Synopsis() string {
lines := []string{}
usagePrefix := "Usage:"
usageStr := c.Usage
if usageStr == "" && c.parentCommand != nil {
usageStr = c.parentCommand.Usage
}
for _, line := range strings.Split(usageStr, "\n") {
if line != "" {
usage := fmt.Sprintf("%s hub %s", usagePrefix, line)
usagePrefix = " "
lines = append(lines, usage)
}
}
return strings.Join(lines, "\n")
}
func (c *Command) HelpText() string {
usage := strings.Replace(c.Usage, "-^", "`-^`", 1)
usageRe := regexp.MustCompile(`(?m)^([a-z-]+)(.*)$`)
usage = usageRe.ReplaceAllString(usage, "`hub $1`$2 ")
usage = strings.TrimSpace(usage)
var desc string
long := strings.TrimSpace(c.Long)
if lines := strings.Split(long, "\n"); len(lines) > 1 {
desc = lines[0]
long = strings.Join(lines[1:], "\n")
}
long = strings.Replace(long, "'", "`", -1)
headingRe := regexp.MustCompile(`(?m)^(## .+):$`)
long = headingRe.ReplaceAllString(long, "$1")
indentRe := regexp.MustCompile(`(?m)^\t`)
long = indentRe.ReplaceAllLiteralString(long, "")
definitionListRe := regexp.MustCompile(`(?m)^(\* )?([^#\s][^\n]*?):?\n\t`)
long = definitionListRe.ReplaceAllString(long, "$2\n:\t")
return fmt.Sprintf("hub-%s(1) -- %s\n===\n\n## Synopsis\n\n%s\n%s", c.Name(), desc, usage, long)
}
func (c *Command) Name() string {
if c.Key != "" {
return c.Key
}
usageLine := strings.Split(strings.TrimSpace(c.Usage), "\n")[0]
return strings.Split(usageLine, " ")[0]
}
func (c *Command) Runnable() bool {
return c.Run != nil
}
func (c *Command) lookupSubCommand(args *Args) (runCommand *Command, err error) {
if len(c.subCommands) > 0 && args.HasSubcommand() {
subCommandName := args.FirstParam()
if subCommand, ok := c.subCommands[subCommandName]; ok {
runCommand = subCommand
args.Params = args.Params[1:]
} else {
err = fmt.Errorf("error: Unknown subcommand: %s", subCommandName)
}
} else {
runCommand = c
}
return
}