This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
136 lines (110 loc) · 2.63 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
package commands
import (
"bytes"
"fmt"
"strings"
"github.com/github/hub/ui"
flag "github.com/ogier/pflag"
)
var (
NameRe = "[\\w.][\\w.-]*"
OwnerRe = "[a-zA-Z0-9][a-zA-Z0-9-]*"
NameWithOwnerRe = fmt.Sprintf("^(?:%s|%s\\/%s)$", NameRe, OwnerRe, NameRe)
CmdRunner = NewRunner()
)
type Command struct {
Run func(cmd *Command, args *Args)
Flag flag.FlagSet
Key string
Usage string
Short string
Long string
GitExtension bool
subCommands map[string]*Command
}
func (c *Command) Call(args *Args) (err error) {
runCommand, err := c.lookupSubCommand(args)
if err != nil {
ui.Errorln(err)
return
}
if !c.GitExtension {
err = runCommand.parseArguments(args)
if err != nil {
return
}
}
runCommand.Run(runCommand, args)
return
}
func (c *Command) parseArguments(args *Args) (err error) {
c.Flag.SetInterspersed(true)
c.Flag.Init(c.Name(), flag.ContinueOnError)
c.Flag.Usage = c.PrintUsage
if err = c.Flag.Parse(args.Params); err == nil {
for _, arg := range args.Params {
if arg == "--" {
args.Terminator = true
}
}
args.Params = c.Flag.Args()
}
return
}
func (c *Command) Use(subCommand *Command) {
if c.subCommands == nil {
c.subCommands = make(map[string]*Command)
}
c.subCommands[subCommand.Name()] = subCommand
}
func (c *Command) PrintUsage() {
if c.Runnable() {
ui.Printf("usage: %s\n\n", c.FormattedUsage())
}
ui.Println(strings.Trim(c.Long, "\n"))
}
func (c *Command) FormattedUsage() string {
return fmt.Sprintf("git %s", c.Usage)
}
func (c *Command) subCommandsUsage() string {
buffer := bytes.NewBufferString("")
usage := "usage"
usage = printUsageBuffer(c, buffer, usage)
for _, s := range c.subCommands {
usage = printUsageBuffer(s, buffer, usage)
}
return buffer.String()
}
func printUsageBuffer(c *Command, b *bytes.Buffer, usage string) string {
if c.Runnable() {
b.WriteString(fmt.Sprintf("%s: %s\n", usage, c.FormattedUsage()))
usage = " or"
}
return usage
}
func (c *Command) Name() string {
if c.Key != "" {
return c.Key
}
return strings.Split(c.Usage, " ")[0]
}
func (c *Command) Runnable() bool {
return c.Run != nil
}
func (c *Command) List() bool {
return c.Short != ""
}
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\n%s", subCommandName, c.subCommandsUsage())
}
} else {
runCommand = c
}
return
}