X Tutup
Skip to content

Commit 4933fd9

Browse files
authored
Merge pull request cli#1696 from cli/env-help-topic
Extract environment variables as a separate help topic
2 parents 9f486ef + 7ecb6a4 commit 4933fd9

File tree

4 files changed

+144
-27
lines changed

4 files changed

+144
-27
lines changed

pkg/cmd/root/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func rootHelpFunc(command *cobra.Command, args []string) {
139139
helpEntries = append(helpEntries, helpEntry{"ENVIRONMENT VARIABLES", command.Annotations["help:environment"]})
140140
}
141141
helpEntries = append(helpEntries, helpEntry{"LEARN MORE", `
142-
Use "gh <command> <subcommand> --help" for more information about a command.
142+
Use 'gh <command> <subcommand> --help' for more information about a command.
143143
Read the manual at https://cli.github.com/manual`})
144144
if _, ok := command.Annotations["help:feedback"]; ok {
145145
helpEntries = append(helpEntries, helpEntry{"FEEDBACK", command.Annotations["help:feedback"]})

pkg/cmd/root/help_topic.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package root
2+
3+
import (
4+
"github.com/MakeNowJust/heredoc"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewHelpTopic(topic string) *cobra.Command {
9+
topicContent := make(map[string]string)
10+
11+
topicContent["environment"] = 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.
14+
15+
GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise.
16+
17+
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
18+
that otherwise operate on a local repository.
19+
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+
23+
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
24+
for authoring text.
25+
26+
BROWSER: the web browser to use for opening links.
27+
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+
31+
GLAMOUR_STYLE: the style to use for rendering Markdown. See
32+
https://github.com/charmbracelet/glamour#styles
33+
34+
NO_COLOR: avoid printing ANSI escape sequences for color output.
35+
`)
36+
37+
cmd := &cobra.Command{
38+
Use: topic,
39+
Long: topicContent[topic],
40+
Hidden: true,
41+
Args: cobra.NoArgs,
42+
Run: helpTopicHelpFunc,
43+
}
44+
45+
cmd.SetHelpFunc(helpTopicHelpFunc)
46+
cmd.SetUsageFunc(helpTopicUsageFunc)
47+
48+
return cmd
49+
}
50+
51+
func helpTopicHelpFunc(command *cobra.Command, args []string) {
52+
command.Print(command.Long)
53+
}
54+
55+
func helpTopicUsageFunc(command *cobra.Command) error {
56+
command.Printf("Usage: gh help %s", command.Use)
57+
return nil
58+
}

pkg/cmd/root/help_topic_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package root
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cli/cli/pkg/iostreams"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewHelpTopic(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
topic string
14+
args []string
15+
flags []string
16+
wantsErr bool
17+
}{
18+
{
19+
name: "valid topic",
20+
topic: "environment",
21+
args: []string{},
22+
flags: []string{},
23+
wantsErr: false,
24+
},
25+
{
26+
name: "invalid topic",
27+
topic: "invalid",
28+
args: []string{},
29+
flags: []string{},
30+
wantsErr: false,
31+
},
32+
{
33+
name: "more than zero args",
34+
topic: "environment",
35+
args: []string{"invalid"},
36+
flags: []string{},
37+
wantsErr: true,
38+
},
39+
{
40+
name: "more than zero flags",
41+
topic: "environment",
42+
args: []string{},
43+
flags: []string{"--invalid"},
44+
wantsErr: true,
45+
},
46+
{
47+
name: "help arg",
48+
topic: "environment",
49+
args: []string{"help"},
50+
flags: []string{},
51+
wantsErr: true,
52+
},
53+
{
54+
name: "help flag",
55+
topic: "environment",
56+
args: []string{},
57+
flags: []string{"--help"},
58+
wantsErr: false,
59+
},
60+
}
61+
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
_, _, stdout, stderr := iostreams.Test()
65+
66+
cmd := NewHelpTopic(tt.topic)
67+
cmd.SetArgs(append(tt.args, tt.flags...))
68+
cmd.SetOut(stdout)
69+
cmd.SetErr(stderr)
70+
71+
_, err := cmd.ExecuteC()
72+
if tt.wantsErr {
73+
assert.Error(t, err)
74+
return
75+
}
76+
assert.NoError(t, err)
77+
})
78+
}
79+
}

pkg/cmd/root/root.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,10 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
4141
`),
4242
Annotations: map[string]string{
4343
"help:feedback": heredoc.Doc(`
44-
Open an issue using gh issue create -R cli/cli
44+
Open an issue using 'gh issue create -R cli/cli'
4545
`),
4646
"help:environment": heredoc.Doc(`
47-
GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids
48-
being prompted to authenticate and takes precedence over previously stored credentials.
49-
50-
GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise.
51-
52-
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
53-
that otherwise operate on a local repository.
54-
55-
GH_HOST: specify the GitHub hostname for commands that would otherwise assume
56-
the "github.com" host when not in a context of an existing repository.
57-
58-
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
59-
for authoring text.
60-
61-
BROWSER: the web browser to use for opening links.
62-
63-
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
64-
or "oauth" to print detailed information about HTTP requests or authentication flow.
65-
66-
GLAMOUR_STYLE: the style to use for rendering Markdown. See
67-
https://github.com/charmbracelet/glamour#styles
68-
69-
NO_COLOR: avoid printing ANSI escape sequences for color output.
47+
See 'gh help environment' for the list of supported environment variables.
7048
`),
7149
},
7250
}
@@ -104,15 +82,17 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
10482

10583
cmdutil.DisableAuthCheck(cmd)
10684

107-
// CHILD COMMANDS
108-
85+
// Child commands
10986
cmd.AddCommand(aliasCmd.NewCmdAlias(f))
11087
cmd.AddCommand(authCmd.NewCmdAuth(f))
11188
cmd.AddCommand(configCmd.NewCmdConfig(f))
11289
cmd.AddCommand(creditsCmd.NewCmdCredits(f, nil))
11390
cmd.AddCommand(gistCmd.NewCmdGist(f))
11491
cmd.AddCommand(NewCmdCompletion(f.IOStreams))
11592

93+
// Help topics
94+
cmd.AddCommand(NewHelpTopic("environment"))
95+
11696
// the `api` command should not inherit any extra HTTP headers
11797
bareHTTPCmdFactory := *f
11898
bareHTTPCmdFactory.HttpClient = func() (*http.Client, error) {

0 commit comments

Comments
 (0)
X Tutup