forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
248 lines (222 loc) · 6.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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"`
NeedsOrg bool `json:"needsOrg"`
WantsOrg bool `json:"wantsOrg"`
NeedsAuth bool `json:"needsAuth"`
VariableArgs bool `json:"variableArgs"`
DisableAnalytics bool `json:"disableAnalytics"`
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 {
if c.Usage != "" {
return c.Usage
}
return c.String() + argsString(c.Args)
}
func (c *Command) buildFlagHelp() string {
flags := c.Flags
if c.NeedsApp || c.WantsApp {
flags = append(flags, *appFlag, *remoteFlag)
}
if c.NeedsOrg || c.WantsOrg {
flags = append(flags, *orgFlag)
}
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")
}
func (c *Command) buildFullHelp() string {
sections := make([]string, 0, 3)
if c.Description != "" {
sections = append(sections, c.Description)
}
flagHelp := c.buildFlagHelp()
if flagHelp != "" {
sections = append(sections, flagHelp)
}
if c.Help != "" {
sections = append(sections, c.Help)
}
return strings.TrimSuffix(strings.Join(sections, "\n\n"), "\n")
}
func (c *Command) unexpectedFlagErr(flag string) {
flagHelp := c.buildFlagHelp()
cmd := "heroku " + c.String()
if flagHelp == "" {
ExitWithMessage(
`Error: Unexpected flag %s
Usage: %s
This command does not take any flags.
See more information with %s`,
red(flag),
cyan("heroku "+commandUsage(c)),
cyan(cmd+" --help"),
)
}
ExitWithMessage(
`Error: Unexpected flag %s
Usage: %s
This flag is invalid for this command. Here are the accepted flags:
%s
See more information with %s`,
red(flag),
cyan("heroku "+commandUsage(c)),
flagHelp,
cyan(cmd+" --help"),
)
}
func (c *Command) appNeededErr() {
ExitWithMessage(
`Error: No app specified
Usage: %s
We don't know which app to run this on.
Run this command from inside an app folder or specify which app to use with %s
https://devcenter.heroku.com/articles/using-the-cli#app-commands`,
cyan("heroku "+commandUsage(c)+" --app APP"),
cyan("--app APP"),
)
}
func (c *Command) unexpectedArgumentsErr(args []string) {
cmd := "heroku " + c.String()
ExitWithMessage(
`Error: Unexpected %s %s
Usage: %s
You gave this command too many arguments. Try the command again without these extra arguments.
See more information with %s`,
plural("argument", len(args)),
red(strings.Join(args, " ")),
cyan("heroku "+commandUsage(c)),
cyan(cmd+" --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 {
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"}},
DisableAnalytics: true,
Run: func(ctx *Context) {
SetupBuiltinPlugins()
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)
}
}
},
}