-
Notifications
You must be signed in to change notification settings - Fork 215
/
command.go
169 lines (152 loc) · 4.1 KB
/
command.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
163
164
165
166
167
168
169
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// Command represents a CLI command.
// For example, in the command `heroku apps:create` the command would be `create`.
// They must have a Topic name that links to a real topic's name.
type Command struct {
Topic string `json:"topic"`
Command string `json:"command,omitempty"`
Plugin string `json:"plugin"`
Usage string `json:"usage"`
Description string `json:"description"`
Default bool `json:"default"`
Help string `json:"help"`
FullHelp string `json:"fullHelp"`
Hidden bool `json:"hidden"`
NeedsApp bool `json:"needsApp"`
WantsApp bool `json:"wantsApp"`
NeedsAuth bool `json:"needsAuth"`
VariableArgs bool `json:"variableArgs"`
Args []Arg `json:"args"`
Flags []Flag `json:"flags"`
Run func(ctx *Context) `json:"-"`
}
func (c *Command) String() string {
if c.Command == "" {
return c.Topic
}
return c.Topic + ":" + c.Command
}
func commandUsage(c *Command) string {
return c.String() + argsString(c.Args)
}
func (c *Command) buildFullHelp() string {
flags := c.Flags
if c.NeedsApp || c.WantsApp {
flags = append(flags, *appFlag, *remoteFlag)
}
if len(flags) == 0 {
return c.Help
}
lines := make([]string, 0, len(flags))
for _, flag := range flags {
if flag.Hidden {
continue
}
if flag.Description == "" {
lines = append(lines, flag.String())
} else {
lines = append(lines, fmt.Sprintf("%-20s # %s", flag.String(), flag.Description))
}
}
return strings.Join(lines, "\n") + "\n\n" + c.Help
}
// CommandSet is a slice of Command structs with some helper methods.
type CommandSet []*Command
// ByTopicAndCommand returns a command that matches the passed topic and command.
func (commands CommandSet) ByTopicAndCommand(topic, command string) *Command {
for _, c := range commands {
if c.Topic == topic {
if c.Command == command || c.Default && command == "" {
return c
}
}
}
return nil
}
func (commands CommandSet) loadUsages() {
for _, c := range commands {
if c.Usage == "" {
c.Usage = commandUsage(c)
}
}
}
func (commands CommandSet) loadFullHelp() {
for _, c := range commands {
if c.FullHelp == "" {
c.FullHelp = c.buildFullHelp()
}
}
}
func (commands CommandSet) Len() int {
return len(commands)
}
func (commands CommandSet) Less(i, j int) bool {
return commands[i].Command < commands[j].Command
}
func (commands CommandSet) Swap(i, j int) {
commands[i], commands[j] = commands[j], commands[i]
}
// Arg defines an argument for a command.
// These will be parsed in Go and passed to the Run method in the Context struct.
type Arg struct {
Name string `json:"name"`
Optional bool `json:"optional"`
Hidden bool `json:"hidden"`
}
func (a *Arg) String() string {
if a.Optional {
return "[" + strings.ToUpper(a.Name) + "]"
}
return strings.ToUpper(a.Name)
}
func argsString(args []Arg) string {
var buffer bytes.Buffer
for _, arg := range args {
if arg.Hidden {
continue
}
if arg.Optional {
buffer.WriteString(" [" + strings.ToUpper(arg.Name) + "]")
} else {
buffer.WriteString(" " + strings.ToUpper(arg.Name))
}
}
return buffer.String()
}
var commandsTopic = &Topic{
Name: "commands",
Description: "list all commands",
Hidden: true,
}
var commandsListCmd = &Command{
Topic: "commands",
Description: "list all commands",
Flags: []Flag{{Name: "json"}},
Run: func(ctx *Context) {
cli.LoadPlugins(GetPlugins())
if ctx.Flags["json"] == true {
cli.Commands.loadUsages()
cli.Commands.loadFullHelp()
doc := map[string]interface{}{"topics": cli.Topics, "commands": cli.Commands}
s, _ := json.Marshal(doc)
Println(string(s))
return
}
for _, command := range cli.Commands {
if command.Hidden {
continue
}
if command.Command == "" {
Printf("%s\n", command.Topic)
} else {
Printf("%s:%s\n", command.Topic, command.Command)
}
}
},
}