Introduce helpTopics type and reduce duplication across commands (#7414)

This commit is contained in:
William Martin 2023-05-11 14:59:12 +02:00 committed by GitHub
parent 07d3a7e302
commit 07ed1e4e8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 118 additions and 86 deletions

View file

@ -10,10 +10,18 @@ import (
"github.com/spf13/cobra"
)
var HelpTopics = map[string]map[string]string{
"mintty": {
"short": "Information about using gh with MinTTY",
"long": heredoc.Doc(`
type helpTopic struct {
name string
short string
long string
example string
}
var HelpTopics = []helpTopic{
{
name: "mintty",
short: "Information about using gh with MinTTY",
long: heredoc.Doc(`
MinTTY is the terminal emulator that comes by default with Git
for Windows. It has known issues with gh's ability to prompt a
user for input.
@ -30,9 +38,10 @@ var HelpTopics = map[string]map[string]string{
NOTE: this can lead to some UI bugs.
`),
},
"environment": {
"short": "Environment variables that can be used with gh",
"long": heredoc.Doc(`
{
name: "environment",
short: "Environment variables that can be used with gh",
long: heredoc.Doc(`
GH_TOKEN, GITHUB_TOKEN (in order of precedence): an authentication token for github.com
API requests. Setting this avoids being prompted to authenticate and takes precedence over
previously stored credentials.
@ -91,12 +100,14 @@ var HelpTopics = map[string]map[string]string{
its own path such as in the cygwin terminal.
`),
},
"reference": {
"short": "A comprehensive reference of all gh commands",
{
name: "reference",
short: "A comprehensive reference of all gh commands",
},
"formatting": {
"short": "Formatting options for JSON data exported from gh",
"long": heredoc.Docf(`
{
name: "formatting",
short: "Formatting options for JSON data exported from gh",
long: heredoc.Docf(`
By default, the result of %[1]sgh%[1]s commands are output in line-based plain text format.
Some commands support passing the %[1]s--json%[1]s flag, which converts the output to JSON format.
Once in JSON, the output can be further formatted according to a required formatting string by
@ -132,7 +143,7 @@ var HelpTopics = map[string]map[string]string{
To learn more about Go templates, see: <https://golang.org/pkg/text/template/>.
`, "`"),
"example": heredoc.Doc(`
example: heredoc.Doc(`
# default output format
$ gh pr list
Showing 23 of 23 open pull requests in cli/cli
@ -242,9 +253,10 @@ var HelpTopics = map[string]map[string]string{
mislav COMMENTED This is going along great! Thanks for working on this
`),
},
"exit-codes": {
"short": "Exit codes used by gh",
"long": heredoc.Doc(`
{
name: "exit-codes",
short: "Exit codes used by gh",
long: heredoc.Doc(`
gh follows normal conventions regarding exit codes.
- If a command completes successfully, the exit code will be 0
@ -262,30 +274,31 @@ var HelpTopics = map[string]map[string]string{
},
}
func NewHelpTopic(ios *iostreams.IOStreams, topic string) *cobra.Command {
func NewCmdHelpTopic(ios *iostreams.IOStreams, ht helpTopic) *cobra.Command {
cmd := &cobra.Command{
Use: topic,
Short: HelpTopics[topic]["short"],
Long: HelpTopics[topic]["long"],
Example: HelpTopics[topic]["example"],
Use: ht.name,
Short: ht.short,
Long: ht.long,
Example: ht.example,
Hidden: true,
Annotations: map[string]string{
"markdown:generate": "true",
"markdown:basename": "gh_help_" + topic,
"markdown:basename": "gh_help_" + ht.name,
},
}
cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
helpTopicHelpFunc(ios.Out, c, args)
})
cmd.SetUsageFunc(func(c *cobra.Command) error {
return helpTopicUsageFunc(ios.ErrOut, c)
})
cmd.SetHelpFunc(func(c *cobra.Command, _ []string) {
helpTopicHelpFunc(ios.Out, c)
})
return cmd
}
func helpTopicHelpFunc(w io.Writer, command *cobra.Command, args []string) {
func helpTopicHelpFunc(w io.Writer, command *cobra.Command) {
fmt.Fprint(w, command.Long)
if command.Example != "" {
fmt.Fprintf(w, "\n\nEXAMPLES\n")