forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
283 lines (262 loc) · 8.22 KB
/
help.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package app
import (
"cf/terminal"
"github.com/codegangsta/cli"
"os"
"strings"
"text/tabwriter"
"text/template"
)
var appHelpTemplate = `{{.Title "NAME:"}}
{{.Name}} - {{.Usage}}
{{.Title "USAGE:"}}
[environment variables] {{.Name}} [global options] command [arguments...] [command options]
{{.Title "VERSION:"}}
{{.Version}}
{{.Title "BUILD TIME:"}}
{{.Compiled}}
{{range .Commands}}
{{.SubTitle .Name}}{{range .CommandSubGroups}}
{{range .}} {{.Name}} {{.Description}}
{{end}}{{end}}{{end}}
{{.Title "ENVIRONMENT VARIABLES"}}
CF_COLOR=false Do not colorize output
CF_HOME=path/to/dir/ Override path to default config directory
CF_STAGING_TIMEOUT=15 Max wait time for buildpack staging, in minutes
CF_STARTUP_TIMEOUT=5 Max wait time for app instance startup, in minutes
CF_TRACE=true Print API request diagnostics to stdout
CF_TRACE=path/to/trace.log Append API request diagnostics to a log file
HTTP_PROXY=proxy.example.com:8080 Enable HTTP proxying for API requests
{{.Title "GLOBAL OPTIONS"}}
--version, -v Print the version
--help, -h Show help
`
type groupedCommands struct {
Name string
CommandSubGroups [][]cmdPresenter
}
func (c groupedCommands) SubTitle(name string) string {
return terminal.HeaderColor(name + ":")
}
type cmdPresenter struct {
Name string
Description string
}
func newCmdPresenter(app *cli.App, maxNameLen int, cmdName string) (presenter cmdPresenter) {
cmd := app.Command(cmdName)
presenter.Name = presentCmdName(*cmd)
padding := strings.Repeat(" ", maxNameLen-len(presenter.Name))
presenter.Name = presenter.Name + padding
presenter.Description = cmd.Description
return
}
func presentCmdName(cmd cli.Command) (name string) {
name = cmd.Name
if cmd.ShortName != "" {
name = name + ", " + cmd.ShortName
}
return
}
type appPresenter struct {
cli.App
Commands []groupedCommands
}
func (p appPresenter) Title(name string) string {
return terminal.HeaderColor(name)
}
func getMaxCmdNameLength(app *cli.App) (length int) {
for _, cmd := range app.Commands {
name := presentCmdName(cmd)
if len(name) > length {
length = len(name)
}
}
return
}
func newAppPresenter(app *cli.App) (presenter appPresenter) {
maxNameLen := getMaxCmdNameLength(app)
presenter.Name = app.Name
presenter.Usage = app.Usage
presenter.Version = app.Version
presenter.Name = app.Name
presenter.Flags = app.Flags
presenter.Compiled = app.Compiled
presenter.Commands = []groupedCommands{
{
Name: "GETTING STARTED",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "login"),
newCmdPresenter(app, maxNameLen, "logout"),
newCmdPresenter(app, maxNameLen, "passwd"),
newCmdPresenter(app, maxNameLen, "target"),
}, {
newCmdPresenter(app, maxNameLen, "api"),
newCmdPresenter(app, maxNameLen, "auth"),
},
},
}, {
Name: "APPS",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "apps"),
newCmdPresenter(app, maxNameLen, "app"),
}, {
newCmdPresenter(app, maxNameLen, "push"),
newCmdPresenter(app, maxNameLen, "scale"),
newCmdPresenter(app, maxNameLen, "delete"),
newCmdPresenter(app, maxNameLen, "rename"),
}, {
newCmdPresenter(app, maxNameLen, "start"),
newCmdPresenter(app, maxNameLen, "stop"),
newCmdPresenter(app, maxNameLen, "restart"),
}, {
newCmdPresenter(app, maxNameLen, "events"),
newCmdPresenter(app, maxNameLen, "files"),
newCmdPresenter(app, maxNameLen, "logs"),
}, {
newCmdPresenter(app, maxNameLen, "env"),
newCmdPresenter(app, maxNameLen, "set-env"),
newCmdPresenter(app, maxNameLen, "unset-env"),
}, {
newCmdPresenter(app, maxNameLen, "stacks"),
},
},
}, {
Name: "SERVICES",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "marketplace"),
newCmdPresenter(app, maxNameLen, "services"),
newCmdPresenter(app, maxNameLen, "service"),
}, {
newCmdPresenter(app, maxNameLen, "create-service"),
newCmdPresenter(app, maxNameLen, "delete-service"),
newCmdPresenter(app, maxNameLen, "rename-service"),
}, {
newCmdPresenter(app, maxNameLen, "bind-service"),
newCmdPresenter(app, maxNameLen, "unbind-service"),
}, {
newCmdPresenter(app, maxNameLen, "create-user-provided-service"),
newCmdPresenter(app, maxNameLen, "update-user-provided-service"),
},
},
}, {
Name: "ORGS",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "orgs"),
newCmdPresenter(app, maxNameLen, "org"),
}, {
newCmdPresenter(app, maxNameLen, "create-org"),
newCmdPresenter(app, maxNameLen, "delete-org"),
newCmdPresenter(app, maxNameLen, "rename-org"),
},
},
}, {
Name: "SPACES",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "spaces"),
newCmdPresenter(app, maxNameLen, "space"),
}, {
newCmdPresenter(app, maxNameLen, "create-space"),
newCmdPresenter(app, maxNameLen, "delete-space"),
newCmdPresenter(app, maxNameLen, "rename-space"),
},
},
}, {
Name: "DOMAINS",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "domains"),
newCmdPresenter(app, maxNameLen, "create-domain"),
newCmdPresenter(app, maxNameLen, "delete-domain"),
newCmdPresenter(app, maxNameLen, "create-shared-domain"),
newCmdPresenter(app, maxNameLen, "delete-shared-domain"),
},
},
}, {
Name: "ROUTES",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "routes"),
newCmdPresenter(app, maxNameLen, "create-route"),
newCmdPresenter(app, maxNameLen, "map-route"),
newCmdPresenter(app, maxNameLen, "unmap-route"),
newCmdPresenter(app, maxNameLen, "delete-route"),
},
},
}, {
Name: "BUILDPACKS",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "buildpacks"),
newCmdPresenter(app, maxNameLen, "create-buildpack"),
newCmdPresenter(app, maxNameLen, "update-buildpack"),
newCmdPresenter(app, maxNameLen, "rename-buildpack"),
newCmdPresenter(app, maxNameLen, "delete-buildpack"),
},
},
}, {
Name: "USER ADMIN",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "create-user"),
newCmdPresenter(app, maxNameLen, "delete-user"),
}, {
newCmdPresenter(app, maxNameLen, "org-users"),
newCmdPresenter(app, maxNameLen, "set-org-role"),
newCmdPresenter(app, maxNameLen, "unset-org-role"),
}, {
newCmdPresenter(app, maxNameLen, "space-users"),
newCmdPresenter(app, maxNameLen, "set-space-role"),
newCmdPresenter(app, maxNameLen, "unset-space-role"),
},
},
}, {
Name: "ORG ADMIN",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "quotas"),
newCmdPresenter(app, maxNameLen, "set-quota"),
},
},
}, {
Name: "SERVICE ADMIN",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "service-auth-tokens"),
newCmdPresenter(app, maxNameLen, "create-service-auth-token"),
newCmdPresenter(app, maxNameLen, "update-service-auth-token"),
newCmdPresenter(app, maxNameLen, "delete-service-auth-token"),
}, {
newCmdPresenter(app, maxNameLen, "service-brokers"),
newCmdPresenter(app, maxNameLen, "create-service-broker"),
newCmdPresenter(app, maxNameLen, "update-service-broker"),
newCmdPresenter(app, maxNameLen, "delete-service-broker"),
newCmdPresenter(app, maxNameLen, "rename-service-broker"),
}, {
newCmdPresenter(app, maxNameLen, "migrate-service-instances"),
newCmdPresenter(app, maxNameLen, "purge-service-offering"),
},
},
}, {
Name: "ADVANCED",
CommandSubGroups: [][]cmdPresenter{
{
newCmdPresenter(app, maxNameLen, "curl"),
},
},
},
}
return
}
func showAppHelp(helpTemplate string, appToPrint interface{}) {
app := appToPrint.(*cli.App)
presenter := newAppPresenter(app)
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Parse(helpTemplate))
t.Execute(w, presenter)
w.Flush()
}