Debian Codegraph @

usage/help.go

raw symbols SCIP
1 package usage
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/urfave/cli"
8 )
9
10 // HelpCommandAction is the action function of the overwritten help command.
11 var HelpCommandAction = cli.ActionFunc(helpAction)
12
13 // HelpCommand overwrites default urfvafe/cli help command to support one or
14 // multiple subcommands like:
15 // step help
16 // step help crypto
17 // step help crypto jwt
18 // step help crypto jwt sign
19 // ...
20 func HelpCommand() cli.Command {
21 return cli.Command{
22 Name: "help",
23 Aliases: []string{"h"},
24 Usage: "display help for the specified command or command group",
25 UsageText: "**step help** <command>",
26 Description: `**step help** command displays help for a command or command group.
27
28 ## EXAMPLES
29
30 Display help for **step ca certificate**:
31 '''
32 $ step help ca certificate
33 '''
34
35 Display help for **step ssh**:
36 '''
37 $ step help ssh
38 '''`,
39 Action: HelpCommandAction,
40 Flags: []cli.Flag{
41 cli.StringFlag{
42 Name: "http",
43 Usage: "HTTP service address (e.g., ':8080')",
44 },
45 cli.StringFlag{
46 Name: "html",
47 Usage: "The export <directory> for HTML docs.",
48 },
49 cli.StringFlag{
50 Name: "markdown",
51 Usage: "The export <directory> for Markdown docs.",
52 },
53 cli.BoolFlag{
54 Name: "report",
55 Usage: "Writes a JSON report to the HTML docs directory.",
56 },
57 cli.BoolFlag{
58 Name: "hugo",
59 Usage: "Writes hugo (vs jekyll) compatible markdown files",
60 },
61 },
62 }
63 }
64
65 func helpAction(ctx *cli.Context) error {
66 // use html version
67 if ctx.IsSet("http") {
68 return httpHelpAction(ctx)
69 }
70
71 if ctx.IsSet("html") {
72 return htmlHelpAction(ctx)
73 }
74
75 if ctx.IsSet("markdown") {
76 return markdownHelpAction(ctx)
77 }
78
79 args := ctx.Args()
80 if args.Present() {
81 last := len(args) - 1
82 lastName := args[last]
83 subcmd := ctx.App.Commands
84 parent := createParentCommand(ctx)
85
86 for _, name := range args[:last] {
87 for _, cmd := range subcmd {
88 if cmd.HasName(name) {
89 parent = cmd
90 subcmd = cmd.Subcommands
91 break
92 }
93 }
94 }
95
96 for _, cmd := range subcmd {
97 if cmd.HasName(lastName) {
98 cmd.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args, " "))
99 parent.HelpName = fmt.Sprintf("%s %s", ctx.App.HelpName, strings.Join(args[:last], " "))
100
101 ctx.Command = cmd
102 if len(cmd.Subcommands) == 0 {
103 ctx.App = createCliApp(ctx, parent)
104 return cli.ShowCommandHelp(ctx, lastName)
105 }
106
107 ctx.App = createCliApp(ctx, cmd)
108 return cli.ShowCommandHelp(ctx, "")
109 }
110 }
111
112 return cli.NewExitError(fmt.Sprintf("No help topic for '%s %s'", ctx.App.Name, strings.Join(args, " ")), 3)
113 }
114
115 cli.ShowAppHelp(ctx)
116 return nil
117 }
118
119 // createParentCommand returns a command representation of the app.
120 func createParentCommand(ctx *cli.Context) cli.Command {
121 return cli.Command{
122 Name: ctx.App.Name,
123 HelpName: ctx.App.HelpName,
124 Usage: ctx.App.Usage,
125 UsageText: ctx.App.UsageText,
126 ArgsUsage: ctx.App.ArgsUsage,
127 Description: ctx.App.Description,
128 Subcommands: ctx.App.Commands,
129 Flags: ctx.App.Flags,
130 }
131 }
132
133 // createCliApp is re-implementation of urfave/cli method (in command.go):
134 //
135 // func (c Command) startApp(ctx *Context) error
136 //
137 // It lets us show the subcommands when help is executed like:
138 //
139 // step help foo
140 // step help foo bar
141 // ...
142 func createCliApp(ctx *cli.Context, cmd cli.Command) *cli.App {
143 app := cli.NewApp()
144 app.Metadata = ctx.App.Metadata
145
146 // set the name and usage
147 app.Name = cmd.HelpName
148 app.HelpName = cmd.HelpName
149
150 app.Usage = cmd.Usage
151 app.UsageText = cmd.UsageText
152 app.Description = cmd.Description
153 app.ArgsUsage = cmd.ArgsUsage
154
155 // set CommandNotFound
156 app.CommandNotFound = ctx.App.CommandNotFound
157 app.CustomAppHelpTemplate = cmd.CustomHelpTemplate
158
159 // set the flags and commands
160 app.Commands = cmd.Subcommands
161 app.Flags = cmd.Flags
162
163 app.Version = ctx.App.Version
164 app.Compiled = ctx.App.Compiled
165 app.Author = ctx.App.Author
166 app.Email = ctx.App.Email
167 app.Writer = ctx.App.Writer
168 app.ErrWriter = ctx.App.ErrWriter
169
170 // Do not show help or version on subcommands
171 app.HideHelp = true
172 app.HideVersion = true
173
174 // bash completion
175 app.EnableBashCompletion = ctx.App.EnableBashCompletion
176 if cmd.BashComplete != nil {
177 app.BashComplete = cmd.BashComplete
178 }
179
180 // set the actions
181 app.Before = cmd.Before
182 app.After = cmd.After
183
184 if cmd.Action != nil {
185 app.Action = cmd.Action
186 } else {
187 app.Action = helpAction
188 }
189 app.OnUsageError = cmd.OnUsageError
190
191 app.Setup()
192 return app
193 }