rearrange code

This commit is contained in:
Corey Johnson 2020-04-22 10:00:33 -07:00
parent be51d095ea
commit a4429aae41

View file

@ -47,7 +47,7 @@ func init() {
// TODO:
// RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
RootCmd.SetHelpFunc(aTempHelpFuncfunc)
RootCmd.SetHelpFunc(rootHelpFunc)
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
@ -72,12 +72,8 @@ func (fe FlagError) Unwrap() error {
// RootCmd is the entry point of command-line execution
var RootCmd = &cobra.Command{
Use: "gh",
Short: "GitHub CLI",
Long: `Work seamlessly with GitHub from the command line.
GitHub CLI is in early stages of development, and we'd love to hear your
feedback at <https://forms.gle/umxd3h31c7aMQFKG7>`,
Long: `Work seamlessly with GitHub from the command line.`,
SilenceErrors: true,
SilenceUsage: true,
@ -209,28 +205,50 @@ func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interfac
return baseRepo, nil
}
type helpEntry struct {
Title string
Body string
}
func rootHelpFunc(command *cobra.Command, s []string) {
type helpEntry struct {
Title string
Body string
}
func aTempHelpFuncfunc(command *cobra.Command, s []string) {
out := colorableOut(command)
fmt.Fprint(out, "\nWork seamlessly with GitHub from the command line.\n\n")
var commands []string
coreCommandNames := []string{"issue", "pr", "repo"}
var coreCommands []string
var additionalCommands []string
for _, c := range command.Commands() {
s := c.Name() + ":" + strings.Repeat(" ", c.NamePadding()) + c.Short
commands = append(commands, s)
if c.Short == "" {
continue
}
s := " " + rpad(c.Name()+":", c.NamePadding()) + c.Short
if includes(coreCommandNames, c.Name()) {
coreCommands = append(coreCommands, s)
} else {
additionalCommands = append(additionalCommands, s)
}
}
helpEntries := []helpEntry{
{
"",
command.Long},
{"USAGE", `
gh <command> <subcommand> [flags]
Commands are run inside of a GitHub repository.`},
{"CORE COMMANDS", strings.Join(commands, "\n")},
# most gh commands need to be run in a directory containing a GitHub repository.
gh <command> [subcommand] [flags]`},
{"CORE COMMANDS", strings.Join(coreCommands, "\n")},
{"ADDITIONAL COMMANDS", strings.Join(additionalCommands, "\n")},
{"FEEDBACK", `
Fill out our feedback form <https://forms.gle/umxd3h31c7aMQFKG7>
Open an issue using gh issue create -R cli/cli`},
{"FLAGS", strings.TrimRight(command.LocalFlags().FlagUsages(), "\n")},
{"EXAMPLES", `
$ cd your-repository
$ gh pr list
$ gh pr checkout 321`},
{"LEARN MORE", `
Use "gh <command> <subcommand> --help" for more information about a command.
Read the manual at <http://cli.github.com/manual>`},
}
out := colorableOut(command)
for _, e := range helpEntries {
if e.Title != "" {
fmt.Fprintln(out, utils.Bold(e.Title))
@ -238,3 +256,18 @@ func aTempHelpFuncfunc(command *cobra.Command, s []string) {
fmt.Fprintln(out, strings.TrimLeft(e.Body, "\n")+"\n")
}
}
// rpad adds padding to the right of a string.
func rpad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds ", padding)
return fmt.Sprintf(template, s)
}
func includes(a []string, s string) bool {
for _, x := range a {
if x == s {
return true
}
}
return false
}