-
Notifications
You must be signed in to change notification settings - Fork 7
/
help.go
369 lines (306 loc) · 8.13 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
)
var helpEnviron = &Command{
Usage: "environ",
Category: "emp",
Short: "environment variables used by emp",
Long: `
Several environment variables affect emp's behavior.
EMPIRE_API_URL
The base URL emp will use to make api requests in the format:
https://[username][:password]@host[:port]/
If username and password are present in the URL, they will
override .netrc.
This environment variable is required.
HEROKU_SSL_VERIFY
When set to disable, emp will insecurely skip SSL verification.
HKHEADER
A NL-separated list of fields to set in each API request header.
These override any fields set by emp if they have the same name.
HKDEBUG
When this is set, emp prints the wire representation of each API
request to stderr just before sending the request, and prints the
response. This will most likely include your secret API key in
the Authorization header field, so be careful with the output.
`,
}
var cmdVersion = &Command{
Run: runVersion,
Usage: "version",
Category: "emp",
Short: "show emp version",
Long: `Version shows the emp client version string.`,
}
func runVersion(cmd *Command, args []string) {
fmt.Println(Version)
}
var cmdHelp = &Command{
Usage: "help [<topic>]",
Category: "emp",
Long: `Help shows usage for a command or other topic.`,
}
var helpMore = &Command{
Usage: "more",
Category: "emp",
Short: "additional commands, less frequently used",
Long: "(not displayed; see special case in runHelp)",
}
var helpCommands = &Command{
Usage: "commands",
Category: "emp",
Short: "list all commands with usage",
Long: "(not displayed; see special case in runHelp)",
}
var helpStyleGuide = &Command{
Usage: "styleguide",
Category: "emp",
Short: "generate an html styleguide for all commands with usage",
Long: "(not displayed; see special case in runHelp)",
}
func init() {
cmdHelp.Run = runHelp // break init loop
}
func runHelp(cmd *Command, args []string) {
if len(args) == 0 {
printUsageTo(os.Stdout)
return // not os.Exit(2); success
}
if len(args) != 1 {
printFatal("too many arguments")
}
switch args[0] {
case helpMore.Name():
printExtra()
return
case helpCommands.Name():
printAllUsage()
return
case helpStyleGuide.Name():
printStyleGuide()
return
}
for _, cmd := range commands {
if cmd.Name() == args[0] && !cmd.Hidden {
cmd.PrintLongUsage()
return
}
}
log.Printf("Unknown help topic: %q. Run 'emp help'.\n", args[0])
os.Exit(2)
}
func maxStrLen(strs []string) (strlen int) {
for i := range strs {
if len(strs[i]) > strlen {
strlen = len(strs[i])
}
}
return
}
var usageTemplate = template.Must(template.New("usage").Parse(`
Usage: emp <command> [-a <app or remote>] [options] [arguments]
Commands:
{{range .Commands}}{{if .Runnable}}{{if .List}}
{{.Name | printf (print "%-" $.MaxRunListName "s")}} {{.Short}}{{end}}{{end}}{{end}}
Run 'emp help [command]' for details.
Additional help topics:
{{range .Commands}}{{if not .Runnable}}
{{.Name | printf "%-8s"}} {{.Short}}{{end}}{{end}}
{{if .Dev}}This dev build of emp cannot auto-update itself.
{{end}}`[1:]))
var extraTemplate = template.Must(template.New("usage").Parse(`
Additional commands:
{{range .Commands}}{{if .Runnable}}{{if .ListAsExtra}}
{{.Name | printf (print "%-" $.MaxRunExtraName "s")}} {{.ShortExtra}}{{end}}{{end}}{{end}}
Run 'emp help [command]' for details.
`[1:]))
func printUsageTo(w io.Writer) {
var runListNames []string
for i := range commands {
if commands[i].Runnable() && commands[i].List() {
runListNames = append(runListNames, commands[i].Name())
}
}
usageTemplate.Execute(w, struct {
Commands []*Command
Dev bool
MaxRunListName int
}{
commands,
Version == "dev",
maxStrLen(runListNames),
})
}
func printExtra() {
var runExtraNames []string
for i := range commands {
if commands[i].Runnable() && commands[i].ListAsExtra() {
runExtraNames = append(runExtraNames, commands[i].Name())
}
}
extraTemplate.Execute(os.Stdout, struct {
Commands []*Command
MaxRunExtraName int
}{
commands,
maxStrLen(runExtraNames),
})
}
func printAllUsage() {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
cl := commandList(commands)
sort.Sort(cl)
for i := range cl {
if cl[i].Runnable() {
listRec(w, "emp "+cl[i].FullUsage(), "# "+cl[i].Short)
}
}
}
func printStyleGuide() {
cmap := make(map[string]commandList)
// group by category
for i := range commands {
if _, exists := cmap[commands[i].Category]; !exists {
cmap[commands[i].Category] = commandList{commands[i]}
} else {
cmap[commands[i].Category] = append(cmap[commands[i].Category], commands[i])
}
}
// sort each category
for _, cl := range cmap {
sort.Sort(cl)
}
err := styleGuideTemplate.Execute(os.Stdout, struct {
CommandMap commandMap
}{
cmap,
})
if err != nil {
printFatal(err.Error())
}
}
func (c *Command) UsageJSON() commandJSON {
return commandJSON{Root: c.Name(), Arguments: strings.TrimLeft(c.FullUsage(), c.Name()+" "), Comment: c.Short}
}
type commandJSON struct {
Root string `json:"root"`
Arguments string `json:"arguments"`
Comment string `json:"comment"`
}
type commandList []*Command
func (cl commandList) Len() int { return len(cl) }
func (cl commandList) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] }
func (cl commandList) Less(i, j int) bool { return cl[i].Name() < cl[j].Name() }
func (cl commandList) UsageJSON() []commandJSON {
a := make([]commandJSON, len(cl))
for i := range cl {
a[i] = cl[i].UsageJSON()
}
return a
}
type commandMap map[string]commandList
func (cm commandMap) UsageJSON(prefix string) template.JS {
all := make([]map[string]interface{}, 0)
categories := make([]string, len(cm))
iall := 0
for k := range cm {
categories[iall] = k
iall += 1
}
sort.Strings(categories)
for _, k := range categories {
m := map[string]interface{}{"title": k, "commands": cm[k].UsageJSON()}
all = append(all, m)
}
buf, err := json.MarshalIndent(all, prefix, " ")
if err != nil {
return template.JS(fmt.Sprintf("{\"error\": %q}", err.Error()))
}
resp := strings.Replace(string(buf), "\\u003c", "<", -1)
resp = strings.Replace(resp, "\\u003e", ">", -1)
return template.JS(resp)
}
var styleGuideTemplate = template.Must(template.New("styleguide").Delims("{{{", "}}}").Parse(`
<!DOCTYPE html>
<html>
<head>
<title>emp style guide</title>
<style>
body {
background: #282A36;
color: white;
font-family: Helvetica;
}
#viewing-options {
padding: 0;
}
#viewing-options li {
display: inline-block;
margin-right: 20px;
}
td {
font-family: monospace;
padding-right: 10px;
}
td:first-child {
width: 540px;
}
h2 {
color: #5A5D6E;
}
.prompt,
.comment {
color: #6272A4;
}
.command {
color: white;
}
.root {
color: #FF79C6;
font-weight: bold;
}
.arguments {
color: #66D9D0;
}
</style>
</head>
<body>
<script id="command-structure" type="text/x-handlebars-template">
{{#groups}}
<h2>{{title}}</h2>
<table>
{{#commands}}
<tr>
<td>
<span class='prompt'>$</span>
<span class='command'>emp</span>
<span class='root'>{{root}}</span>
<span class='arguments'>{{arguments}}</span>
</td>
<td class='comment'># {{comment}}</td>
</tr>
{{/commands}}
</table>
{{/groups}}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.1.2/handlebars.min.js"></script>
<script>
var source = $('#command-structure').html();
var template = Handlebars.compile(source);
var data = {{{.CommandMap.UsageJSON " "}}}
$('body').append(template({groups: data}));
</script>
</body>
</html>
`[1:]))