This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
340 lines (260 loc) · 7.66 KB
/
html.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
package usage
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"github.com/smallstep/cli/errs"
"github.com/urfave/cli"
)
func httpHelpAction(ctx *cli.Context) error {
addr := ctx.String("http")
if addr == "" {
return errs.RequiredFlag(ctx, "http")
}
fmt.Printf("Serving HTTP on %s ...\n", addr)
return http.ListenAndServe(addr, &htmlHelpHandler{
cliApp: ctx.App,
})
}
func markdownHelpAction(ctx *cli.Context) error {
dir := path.Clean(ctx.String("markdown"))
if err := os.MkdirAll(dir, 0755); err != nil {
return errs.FileError(err, dir)
}
// app index
index := path.Join(dir, "step.md")
w, err := os.Create(index)
if err != nil {
return errs.FileError(err, index)
}
markdownHelpPrinter(w, mdAppHelpTemplate, ctx.App)
if err := w.Close(); err != nil {
return errs.FileError(err, index)
}
// Subcommands
for _, cmd := range ctx.App.Commands {
if err := markdownHelpCommand(ctx.App, cmd, path.Join(dir, cmd.Name)); err != nil {
return err
}
}
return nil
}
func markdownHelpCommand(app *cli.App, cmd cli.Command, base string) error {
if err := os.MkdirAll(base, 0755); err != nil {
return errs.FileError(err, base)
}
index := path.Join(base, "index.md")
w, err := os.Create(index)
if err != nil {
return errs.FileError(err, index)
}
if len(cmd.Subcommands) == 0 {
markdownHelpPrinter(w, mdCommandHelpTemplate, cmd)
return errs.FileError(w.Close(), index)
}
ctx := cli.NewContext(app, nil, nil)
ctx.App = createCliApp(ctx, cmd)
markdownHelpPrinter(w, mdSubcommandHelpTemplate, ctx.App)
if err := w.Close(); err != nil {
return errs.FileError(err, index)
}
for _, sub := range cmd.Subcommands {
sub.HelpName = fmt.Sprintf("%s %s", cmd.HelpName, sub.Name)
if err := markdownHelpCommand(app, sub, path.Join(base, sub.Name)); err != nil {
return err
}
}
return nil
}
func htmlHelpAction(ctx *cli.Context) error {
dir := path.Clean(ctx.String("html"))
if err := os.MkdirAll(dir, 0755); err != nil {
return errs.FileError(err, dir)
}
// app index
index := path.Join(dir, "index.html")
w, err := os.Create(index)
if err != nil {
return errs.FileError(err, index)
}
tophelp := htmlHelpPrinter(w, mdAppHelpTemplate, ctx.App)
var report *Report
if ctx.IsSet("report") {
report = NewReport(ctx.App.Name, tophelp)
}
if err := w.Close(); err != nil {
return errs.FileError(err, index)
}
// css style
cssFile := path.Join(dir, "style.css")
if err := ioutil.WriteFile(cssFile, []byte(css), 0666); err != nil {
return errs.FileError(err, cssFile)
}
// Subcommands
for _, cmd := range ctx.App.Commands {
if err := htmlHelpCommand(ctx.App, cmd, path.Join(dir, cmd.Name), report); err != nil {
return err
}
}
// report
if report != nil {
repjson := path.Join(dir, "report.json")
rjw, err := os.Create(repjson)
if err != nil {
return errs.FileError(err, repjson)
}
if err := report.Write(rjw); err != nil {
return err
}
if err := rjw.Close(); err != nil {
return errs.FileError(err, repjson)
}
}
return nil
}
func htmlHelpCommand(app *cli.App, cmd cli.Command, base string, report *Report) error {
if err := os.MkdirAll(base, 0755); err != nil {
return errs.FileError(err, base)
}
index := path.Join(base, "index.html")
w, err := os.Create(index)
if err != nil {
return errs.FileError(err, index)
}
if len(cmd.Subcommands) == 0 {
cmdhelp := htmlHelpPrinter(w, mdCommandHelpTemplate, cmd)
if report != nil {
report.Process(cmd.HelpName, cmdhelp)
}
return errs.FileError(w.Close(), index)
}
ctx := cli.NewContext(app, nil, nil)
ctx.App = createCliApp(ctx, cmd)
subhelp := htmlHelpPrinter(w, mdSubcommandHelpTemplate, ctx.App)
if report != nil {
report.Process(cmd.HelpName, subhelp)
}
if err := w.Close(); err != nil {
return errs.FileError(err, index)
}
for _, sub := range cmd.Subcommands {
sub.HelpName = fmt.Sprintf("%s %s", cmd.HelpName, sub.Name)
if err := htmlHelpCommand(app, sub, path.Join(base, sub.Name), report); err != nil {
return err
}
}
return nil
}
type htmlHelpHandler struct {
cliApp *cli.App
}
func (h *htmlHelpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := cli.NewContext(h.cliApp, nil, nil)
// clean request URI
requestURI := path.Clean(req.RequestURI)
if requestURI == "/" {
htmlHelpPrinter(w, mdAppHelpTemplate, ctx.App)
return
}
if requestURI == "/style.css" {
w.Header().Set("Content-Type", `text/css; charset="utf-8"`)
w.Write([]byte(css))
return
}
args := strings.Split(requestURI, "/")
last := len(args) - 1
lastName := args[last]
subcmd := ctx.App.Commands
parent := createParentCommand(ctx)
for _, name := range args[:last] {
for _, cmd := range subcmd {
if cmd.HasName(name) {
parent = cmd
subcmd = cmd.Subcommands
break
}
}
}
for _, cmd := range subcmd {
if cmd.HasName(lastName) {
cmd.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args, " "))
parent.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args[:last], " "))
ctx.Command = cmd
if len(cmd.Subcommands) == 0 {
htmlHelpPrinter(w, mdCommandHelpTemplate, cmd)
return
}
ctx.App = createCliApp(ctx, cmd)
htmlHelpPrinter(w, mdSubcommandHelpTemplate, ctx.App)
return
}
}
http.NotFound(w, req)
}
// AppHelpTemplate contains the modified template for the main app
var mdAppHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .Commands}} <command>{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}{{if .VisibleCommands}}
## COMMANDS
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
|||
|---|---|{{range .VisibleCommands}}
| **[{{join .Names ", "}}]({{.Name}}/)** | {{.Usage}} |{{end}}
{{end}}{{if .VisibleFlags}}{{end}}
## OPTIONS
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}
{{end}}{{end}}{{if .Copyright}}{{if len .Authors}}
## AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
{{end}}{{$author}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
## ONLINE
This documentation is available online at https://smallstep.com/docs/cli
## PRINTING
This documentation can be typeset for printing by running ...
A version of this document typeset for printing is available online at ...pdf
## VERSION
{{.Version}}{{end}}{{end}}
## COPYRIGHT
{{.Copyright}}
{{end}}
`
// SubcommandHelpTemplate contains the modified template for a sub command
// Note that the weird "|||\n|---|---|" syntax sets up a markdown table with empty headers.
var mdSubcommandHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}** <command>{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}
## COMMANDS
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
|||
|---|---|{{range .VisibleCommands}}
| **[{{join .Names ", "}}]({{.Name}}/)** | {{.Usage}} |{{end}}
{{end}}{{if .VisibleFlags}}
## OPTIONS
{{range .VisibleFlags}}
{{.}}
{{end}}{{end}}
`
// CommandHelpTemplate contains the modified template for a command
var mdCommandHelpTemplate = `## NAME
**{{.HelpName}}** -- {{.Usage}}
## USAGE
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Category}}
## CATEGORY
{{.Category}}{{end}}{{if .Description}}
## DESCRIPTION
{{.Description}}{{end}}{{if .VisibleFlags}}
## OPTIONS
{{range .VisibleFlags}}
{{.}}
{{end}}{{end}}
`