From 1f0db96f2af47a959cf4dd853876c64c037efe1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 11 May 2020 12:38:22 +0200 Subject: [PATCH] Avoid relying on Cobra help internals --- command/hackyHelp.go | 40 ---------------------------------------- command/root.go | 4 +++- 2 files changed, 3 insertions(+), 41 deletions(-) delete mode 100644 command/hackyHelp.go diff --git a/command/hackyHelp.go b/command/hackyHelp.go deleted file mode 100644 index 0d9d65b61..000000000 --- a/command/hackyHelp.go +++ /dev/null @@ -1,40 +0,0 @@ -package command - -import ( - "fmt" - "io" - "strings" - "text/template" - "unicode" - - "github.com/spf13/cobra" -) - -func hackyHelp(command *cobra.Command) { - trimRightSpace := func(s string) string { - return strings.TrimRightFunc(s, unicode.IsSpace) - } - - rpad := func(s string, padding int) string { - template := fmt.Sprintf("%%-%ds", padding) - return fmt.Sprintf(template, s) - } - - templateFuncs := template.FuncMap{ - "trim": strings.TrimSpace, - "trimTrailingWhitespaces": trimRightSpace, - "rpad": rpad, - } - - tmpl := func(w io.Writer, text string, data interface{}) error { - t := template.New("top") - t.Funcs(templateFuncs) - template.Must(t.Parse(text)) - return t.Execute(w, data) - } - - err := tmpl(command.OutOrStdout(), command.HelpTemplate(), command) - if err != nil { - command.Println(err) - } -} diff --git a/command/root.go b/command/root.go index 87e44bd29..ebd5445d2 100644 --- a/command/root.go +++ b/command/root.go @@ -28,6 +28,7 @@ var Version = "DEV" var BuildDate = "" // YYYY-MM-DD var versionOutput = "" +var cobraDefaultHelpFunc func(*cobra.Command, []string) func init() { if Version == "DEV" { @@ -51,6 +52,7 @@ func init() { // TODO: // RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output") + cobraDefaultHelpFunc = RootCmd.HelpFunc() RootCmd.SetHelpFunc(rootHelpFunc) RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { @@ -250,7 +252,7 @@ func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interfac func rootHelpFunc(command *cobra.Command, s []string) { if command != RootCmd { - hackyHelp(command) + cobraDefaultHelpFunc(command, s) return }