|
1 | 1 | package root |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + |
4 | 6 | "github.com/MakeNowJust/heredoc" |
5 | 7 | "github.com/spf13/cobra" |
6 | 8 | ) |
7 | 9 |
|
8 | 10 | func NewHelpTopic(topic string) *cobra.Command { |
9 | | - return &cobra.Command{ |
10 | | - Use: "environment", |
11 | | - Long: heredoc.Doc(` |
12 | | - GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids |
13 | | - being prompted to authenticate and takes precedence over previously stored credentials. |
| 11 | + topicContent := make(map[string]string) |
| 12 | + |
| 13 | + topicContent["environment"] = heredoc.Doc(` |
| 14 | + GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids |
| 15 | + being prompted to authenticate and takes precedence over previously stored credentials. |
14 | 16 |
|
15 | | - GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise. |
| 17 | + GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise. |
16 | 18 |
|
17 | | - GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands |
18 | | - that otherwise operate on a local repository. |
| 19 | + GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands |
| 20 | + that otherwise operate on a local repository. |
19 | 21 |
|
20 | | - GH_HOST: specify the GitHub hostname for commands that would otherwise assume |
21 | | - the "github.com" host when not in a context of an existing repository. |
| 22 | + GH_HOST: specify the GitHub hostname for commands that would otherwise assume |
| 23 | + the "github.com" host when not in a context of an existing repository. |
22 | 24 |
|
23 | | - GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use |
24 | | - for authoring text. |
| 25 | + GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use |
| 26 | + for authoring text. |
25 | 27 |
|
26 | | - BROWSER: the web browser to use for opening links. |
| 28 | + BROWSER: the web browser to use for opening links. |
27 | 29 |
|
28 | | - DEBUG: set to any value to enable verbose output to standard error. Include values "api" |
29 | | - or "oauth" to print detailed information about HTTP requests or authentication flow. |
| 30 | + DEBUG: set to any value to enable verbose output to standard error. Include values "api" |
| 31 | + or "oauth" to print detailed information about HTTP requests or authentication flow. |
30 | 32 |
|
31 | | - GLAMOUR_STYLE: the style to use for rendering Markdown. See |
32 | | - https://github.com/charmbracelet/glamour#styles |
| 33 | + GLAMOUR_STYLE: the style to use for rendering Markdown. See |
| 34 | + https://github.com/charmbracelet/glamour#styles |
33 | 35 |
|
34 | | - NO_COLOR: avoid printing ANSI escape sequences for color output. |
35 | | - `), |
| 36 | + NO_COLOR: avoid printing ANSI escape sequences for color output. |
| 37 | + `) |
| 38 | + |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: topic, |
| 41 | + Long: topicContent[topic], |
36 | 42 | Hidden: true, |
37 | | - Annotations: map[string]string{ |
38 | | - "helpTopic": "true", |
39 | | - }, |
| 43 | + Args: cobra.NoArgs, |
| 44 | + } |
| 45 | + |
| 46 | + cmd.SetHelpFunc(helpTopicHelpFunc) |
| 47 | + cmd.SetUsageFunc(helpTopicUsageFunc) |
| 48 | + |
| 49 | + return cmd |
| 50 | +} |
| 51 | + |
| 52 | +func helpTopicHelpFunc(command *cobra.Command, args []string) { |
| 53 | + if len(args) >= 2 && args[1] != "--help" && args[1] != "-h" { |
| 54 | + command.Printf("unknown command %q for %q\n", args[1], command.CommandPath()) |
| 55 | + |
| 56 | + if args[1] == "help" { |
| 57 | + command.Print("\nDid you mean this?\n") |
| 58 | + command.Printf("\t%s\n\n", "--help") |
| 59 | + } else { |
| 60 | + command.Printf("\n") |
| 61 | + } |
| 62 | + |
| 63 | + helpTopicUsageFunc(command) |
| 64 | + command.Printf("\n") |
| 65 | + hasFailed = true |
| 66 | + return |
40 | 67 | } |
| 68 | + |
| 69 | + fmt.Fprint(command.OutOrStdout(), command.Long) |
| 70 | +} |
| 71 | + |
| 72 | +func helpTopicUsageFunc(command *cobra.Command) error { |
| 73 | + command.Printf("Usage: gh help %s", command.Use) |
| 74 | + |
| 75 | + return nil |
41 | 76 | } |
0 commit comments