| 1 |
package usage |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"net/http" |
| 7 |
"os" |
| 8 |
"path" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/smallstep/cli/errs" |
| 12 |
"github.com/urfave/cli" |
| 13 |
) |
| 14 |
|
| 15 |
func httpHelpAction(ctx *cli.Context) error { |
| 16 |
addr := ctx.String("http") |
| 17 |
if addr == "" { |
| 18 |
return errs.RequiredFlag(ctx, "http") |
| 19 |
} |
| 20 |
|
| 21 |
fmt.Printf("Serving HTTP on %s ...\n", addr) |
| 22 |
return http.ListenAndServe(addr, &htmlHelpHandler{ |
| 23 |
cliApp: ctx.App, |
| 24 |
}) |
| 25 |
} |
| 26 |
|
| 27 |
func markdownHelpAction(ctx *cli.Context) error { |
| 28 |
dir := path.Clean(ctx.String("markdown")) |
| 29 |
if err := os.MkdirAll(dir, 0755); err != nil { |
| 30 |
return errs.FileError(err, dir) |
| 31 |
} |
| 32 |
|
| 33 |
isHugo := ctx.Bool("hugo") |
| 34 |
|
| 35 |
|
| 36 |
index := path.Join(dir, "index.md") |
| 37 |
w, err := os.Create(index) |
| 38 |
if err != nil { |
| 39 |
return errs.FileError(err, index) |
| 40 |
} |
| 41 |
markdownHelpPrinter(w, mdAppHelpTemplate, "", ctx.App) |
| 42 |
if err := w.Close(); err != nil { |
| 43 |
return errs.FileError(err, index) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
for _, cmd := range ctx.App.Commands { |
| 48 |
if err := markdownHelpCommand(ctx.App, cmd, cmd, path.Join(dir, cmd.Name), isHugo); err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
} |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func markdownHelpCommand(app *cli.App, cmd cli.Command, parent cli.Command, base string, isHugo bool) error { |
| 56 |
if err := os.MkdirAll(base, 0755); err != nil { |
| 57 |
return errs.FileError(err, base) |
| 58 |
} |
| 59 |
|
| 60 |
fileName := "index.md" |
| 61 |
|
| 62 |
if isHugo && len(cmd.Subcommands) > 0 { |
| 63 |
fileName = "_index.md" |
| 64 |
} |
| 65 |
|
| 66 |
index := path.Join(base, fileName) |
| 67 |
w, err := os.Create(index) |
| 68 |
if err != nil { |
| 69 |
return errs.FileError(err, index) |
| 70 |
} |
| 71 |
|
| 72 |
parentName := parent.HelpName |
| 73 |
if cmd.HelpName == parent.HelpName { |
| 74 |
parentName = "step" |
| 75 |
} |
| 76 |
|
| 77 |
if len(cmd.Subcommands) == 0 { |
| 78 |
markdownHelpPrinter(w, mdCommandHelpTemplate, parentName, cmd) |
| 79 |
return errs.FileError(w.Close(), index) |
| 80 |
} |
| 81 |
|
| 82 |
ctx := cli.NewContext(app, nil, nil) |
| 83 |
ctx.App = createCliApp(ctx, cmd) |
| 84 |
markdownHelpPrinter(w, mdSubcommandHelpTemplate, parentName, ctx.App) |
| 85 |
if err := w.Close(); err != nil { |
| 86 |
return errs.FileError(err, index) |
| 87 |
} |
| 88 |
|
| 89 |
for _, sub := range cmd.Subcommands { |
| 90 |
sub.HelpName = fmt.Sprintf("%s %s", cmd.HelpName, sub.Name) |
| 91 |
if err := markdownHelpCommand(app, sub, cmd, path.Join(base, sub.Name), isHugo); err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
func htmlHelpAction(ctx *cli.Context) error { |
| 100 |
dir := path.Clean(ctx.String("html")) |
| 101 |
|
| 102 |
if err := os.MkdirAll(dir, 0755); err != nil { |
| 103 |
return errs.FileError(err, dir) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
index := path.Join(dir, "index.html") |
| 108 |
w, err := os.Create(index) |
| 109 |
if err != nil { |
| 110 |
return errs.FileError(err, index) |
| 111 |
} |
| 112 |
|
| 113 |
tophelp := htmlHelpPrinter(w, mdAppHelpTemplate, ctx.App) |
| 114 |
var report *Report |
| 115 |
if ctx.IsSet("report") { |
| 116 |
report = NewReport(ctx.App.Name, tophelp) |
| 117 |
} |
| 118 |
|
| 119 |
if err := w.Close(); err != nil { |
| 120 |
return errs.FileError(err, index) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
cssFile := path.Join(dir, "style.css") |
| 125 |
if err := ioutil.WriteFile(cssFile, []byte(css), 0666); err != nil { |
| 126 |
return errs.FileError(err, cssFile) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
for _, cmd := range ctx.App.Commands { |
| 131 |
if err := htmlHelpCommand(ctx.App, cmd, path.Join(dir, cmd.Name), report); err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if report != nil { |
| 138 |
repjson := path.Join(dir, "report.json") |
| 139 |
rjw, err := os.Create(repjson) |
| 140 |
if err != nil { |
| 141 |
return errs.FileError(err, repjson) |
| 142 |
} |
| 143 |
|
| 144 |
if err := report.Write(rjw); err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
|
| 148 |
if err := rjw.Close(); err != nil { |
| 149 |
return errs.FileError(err, repjson) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func htmlHelpCommand(app *cli.App, cmd cli.Command, base string, report *Report) error { |
| 157 |
if err := os.MkdirAll(base, 0755); err != nil { |
| 158 |
return errs.FileError(err, base) |
| 159 |
} |
| 160 |
|
| 161 |
index := path.Join(base, "index.html") |
| 162 |
w, err := os.Create(index) |
| 163 |
if err != nil { |
| 164 |
return errs.FileError(err, index) |
| 165 |
} |
| 166 |
|
| 167 |
if len(cmd.Subcommands) == 0 { |
| 168 |
cmdhelp := htmlHelpPrinter(w, mdCommandHelpTemplate, cmd) |
| 169 |
|
| 170 |
if report != nil { |
| 171 |
report.Process(cmd.HelpName, cmdhelp) |
| 172 |
} |
| 173 |
|
| 174 |
return errs.FileError(w.Close(), index) |
| 175 |
} |
| 176 |
|
| 177 |
ctx := cli.NewContext(app, nil, nil) |
| 178 |
ctx.App = createCliApp(ctx, cmd) |
| 179 |
subhelp := htmlHelpPrinter(w, mdSubcommandHelpTemplate, ctx.App) |
| 180 |
|
| 181 |
if report != nil { |
| 182 |
report.Process(cmd.HelpName, subhelp) |
| 183 |
} |
| 184 |
|
| 185 |
if err := w.Close(); err != nil { |
| 186 |
return errs.FileError(err, index) |
| 187 |
} |
| 188 |
|
| 189 |
for _, sub := range cmd.Subcommands { |
| 190 |
sub.HelpName = fmt.Sprintf("%s %s", cmd.HelpName, sub.Name) |
| 191 |
if err := htmlHelpCommand(app, sub, path.Join(base, sub.Name), report); err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return nil |
| 197 |
} |
| 198 |
|
| 199 |
type htmlHelpHandler struct { |
| 200 |
cliApp *cli.App |
| 201 |
} |
| 202 |
|
| 203 |
func (h *htmlHelpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 204 |
ctx := cli.NewContext(h.cliApp, nil, nil) |
| 205 |
|
| 206 |
|
| 207 |
requestURI := path.Clean(req.RequestURI) |
| 208 |
if requestURI == "/" { |
| 209 |
htmlHelpPrinter(w, mdAppHelpTemplate, ctx.App) |
| 210 |
return |
| 211 |
} |
| 212 |
|
| 213 |
if requestURI == "/style.css" { |
| 214 |
w.Header().Set("Content-Type", `text/css; charset="utf-8"`) |
| 215 |
w.Write([]byte(css)) |
| 216 |
return |
| 217 |
} |
| 218 |
|
| 219 |
args := strings.Split(requestURI, "/") |
| 220 |
last := len(args) - 1 |
| 221 |
lastName := args[last] |
| 222 |
subcmd := ctx.App.Commands |
| 223 |
parent := createParentCommand(ctx) |
| 224 |
for _, name := range args[:last] { |
| 225 |
for _, cmd := range subcmd { |
| 226 |
if cmd.HasName(name) { |
| 227 |
parent = cmd |
| 228 |
subcmd = cmd.Subcommands |
| 229 |
break |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
for _, cmd := range subcmd { |
| 235 |
if cmd.HasName(lastName) { |
| 236 |
cmd.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args, " ")) |
| 237 |
parent.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args[:last], " ")) |
| 238 |
|
| 239 |
ctx.Command = cmd |
| 240 |
if len(cmd.Subcommands) == 0 { |
| 241 |
htmlHelpPrinter(w, mdCommandHelpTemplate, cmd) |
| 242 |
return |
| 243 |
} |
| 244 |
|
| 245 |
ctx.App = createCliApp(ctx, cmd) |
| 246 |
htmlHelpPrinter(w, mdSubcommandHelpTemplate, ctx.App) |
| 247 |
return |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
http.NotFound(w, req) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
var mdAppHelpTemplate = `## NAME |
| 256 |
**{{.HelpName}}** -- {{.Usage}} |
| 257 |
|
| 258 |
## USAGE |
| 259 |
|
| 260 |
'''raw |
| 261 |
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .Commands}} <command>{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments]{{end}}{{end}} |
| 262 |
''' |
| 263 |
{{- if .Description}} |
| 264 |
|
| 265 |
## DESCRIPTION |
| 266 |
{{.Description}}{{end}}{{if .VisibleCommands}} |
| 267 |
|
| 268 |
## COMMANDS |
| 269 |
|
| 270 |
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}} |
| 271 |
| Name | Usage | |
| 272 |
|---|---|{{range .VisibleCommands}} |
| 273 |
| **[{{join .Names ", "}}]({{.Name}}/)** | {{.Usage}} |{{end}} |
| 274 |
{{end}}{{if .VisibleFlags}}{{end}} |
| 275 |
|
| 276 |
## OPTIONS |
| 277 |
|
| 278 |
{{range $index, $option := .VisibleFlags}}{{if $index}} |
| 279 |
{{end}}{{$option}} |
| 280 |
{{end}}{{end}}{{if .Copyright}}{{if len .Authors}} |
| 281 |
|
| 282 |
## AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}: |
| 283 |
|
| 284 |
{{range $index, $author := .Authors}}{{if $index}} |
| 285 |
{{end}}{{$author}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}} |
| 286 |
|
| 287 |
## VERSION |
| 288 |
|
| 289 |
{{.Version}}{{end}}{{end}} |
| 290 |
|
| 291 |
## COPYRIGHT |
| 292 |
|
| 293 |
{{.Copyright}} |
| 294 |
{{end}} |
| 295 |
` |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
var mdSubcommandHelpTemplate = `## NAME |
| 300 |
**{{.HelpName}}** -- {{.Usage}} |
| 301 |
|
| 302 |
## USAGE |
| 303 |
|
| 304 |
'''raw |
| 305 |
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}** <command>{{if .VisibleFlags}} [options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments]{{end}}{{end}} |
| 306 |
''' |
| 307 |
{{- if .Description}} |
| 308 |
|
| 309 |
## DESCRIPTION |
| 310 |
|
| 311 |
{{.Description}}{{end}} |
| 312 |
|
| 313 |
## COMMANDS |
| 314 |
|
| 315 |
{{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}} |
| 316 |
| Name | Usage | |
| 317 |
|---|---|{{range .VisibleCommands}} |
| 318 |
| **[{{join .Names ", "}}]({{.Name}}/)** | {{.Usage}} |{{end}} |
| 319 |
{{end}}{{if .VisibleFlags}} |
| 320 |
|
| 321 |
## OPTIONS |
| 322 |
|
| 323 |
{{range .VisibleFlags}} |
| 324 |
{{.}} |
| 325 |
{{end}}{{end}} |
| 326 |
` |
| 327 |
|
| 328 |
|
| 329 |
var mdCommandHelpTemplate = `## NAME |
| 330 |
**{{.HelpName}}** -- {{.Usage}} |
| 331 |
|
| 332 |
## USAGE |
| 333 |
|
| 334 |
'''raw |
| 335 |
{{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .VisibleFlags}} [options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments]{{end}}{{end}} |
| 336 |
''' |
| 337 |
{{- if .Category}} |
| 338 |
|
| 339 |
## CATEGORY |
| 340 |
|
| 341 |
{{.Category}}{{end}}{{if .Description}} |
| 342 |
|
| 343 |
## DESCRIPTION |
| 344 |
|
| 345 |
{{.Description}}{{end}}{{if .VisibleFlags}} |
| 346 |
|
| 347 |
## OPTIONS |
| 348 |
|
| 349 |
{{range .VisibleFlags}} |
| 350 |
{{.}} |
| 351 |
{{end}}{{end}} |
| 352 |
` |