-
Notifications
You must be signed in to change notification settings - Fork 4
/
help.go
194 lines (165 loc) · 4.37 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
package usage
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
// HelpCommandAction is the action function of the overwritten help command.
var HelpCommandAction = cli.ActionFunc(helpAction)
// HelpCommand overwrites default urfvafe/cli help command to support one or
// multiple subcommands like:
// step help
// step help crypto
// step help crypto jwt
// step help crypto jwt sign
// ...
func HelpCommand() cli.Command {
return cli.Command{
Name: "help",
Aliases: []string{"h"},
Usage: "display help for the specified command or command group",
UsageText: "**step help** <command>",
Description: `**step help** command displays help for a command or command group.
## EXAMPLES
Display help for **step ca certificate**:
'''
$ step help ca certificate
'''
Display help for **step ssh**:
'''
$ step help ssh
'''`,
Action: HelpCommandAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "http",
Usage: "HTTP service address (e.g., ':8080')",
},
cli.StringFlag{
Name: "html",
Usage: "The export <directory> for HTML docs.",
},
cli.StringFlag{
Name: "markdown",
Usage: "The export <directory> for Markdown docs.",
},
cli.BoolFlag{
Name: "report",
Usage: "Writes a JSON report to the HTML docs directory.",
},
cli.BoolFlag{
Name: "hugo",
Usage: "Writes hugo (vs jekyll) compatible markdown files",
},
},
}
}
func helpAction(ctx *cli.Context) error {
// use html version
if ctx.IsSet("http") {
return httpHelpAction(ctx)
}
if ctx.IsSet("html") {
return htmlHelpAction(ctx)
}
if ctx.IsSet("markdown") {
return markdownHelpAction(ctx)
}
args := ctx.Args()
if args.Present() {
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) {
continue
}
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 {
ctx.App = createCliApp(ctx, parent)
return cli.ShowCommandHelp(ctx, lastName)
}
ctx.App = createCliApp(ctx, cmd)
return cli.ShowCommandHelp(ctx, "")
}
return cli.NewExitError(fmt.Sprintf("No help topic for '%s %s'", ctx.App.Name, strings.Join(args, " ")), 3)
}
cli.ShowAppHelp(ctx)
return nil
}
// createParentCommand returns a command representation of the app.
func createParentCommand(ctx *cli.Context) cli.Command {
return cli.Command{
Name: ctx.App.Name,
HelpName: ctx.App.HelpName,
Usage: ctx.App.Usage,
UsageText: ctx.App.UsageText,
ArgsUsage: ctx.App.ArgsUsage,
Description: ctx.App.Description,
Subcommands: ctx.App.Commands,
Flags: ctx.App.Flags,
}
}
// createCliApp is re-implementation of urfave/cli method (in command.go):
//
// func (c Command) startApp(ctx *Context) error
//
// It lets us show the subcommands when help is executed like:
//
// step help foo
// step help foo bar
// ...
func createCliApp(ctx *cli.Context, cmd cli.Command) *cli.App {
app := cli.NewApp()
app.Metadata = ctx.App.Metadata
// set the name and usage
app.Name = cmd.HelpName
app.HelpName = cmd.HelpName
app.Usage = cmd.Usage
app.UsageText = cmd.UsageText
app.Description = cmd.Description
app.ArgsUsage = cmd.ArgsUsage
// set CommandNotFound
app.CommandNotFound = ctx.App.CommandNotFound
app.CustomAppHelpTemplate = cmd.CustomHelpTemplate
// set the flags and commands
app.Commands = cmd.Subcommands
app.Flags = cmd.Flags
app.Version = ctx.App.Version
app.Compiled = ctx.App.Compiled
app.Author = ctx.App.Author
app.Email = ctx.App.Email
app.Writer = ctx.App.Writer
app.ErrWriter = ctx.App.ErrWriter
// Do not show help or version on subcommands
app.HideHelp = true
app.HideVersion = true
// bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion
if cmd.BashComplete != nil {
app.BashComplete = cmd.BashComplete
}
// set the actions
app.Before = cmd.Before
app.After = cmd.After
if cmd.Action != nil {
app.Action = cmd.Action
} else {
app.Action = helpAction
}
app.OnUsageError = cmd.OnUsageError
app.Setup()
return app
}