From 70223d1e3235f611e3b916424e40bc1447c5b638 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 8 May 2020 09:19:19 -0700 Subject: [PATCH 1/3] Don't always use the root command help Co-Authored-By: Nate Smith --- command/hackyHelp.go | 98 ++++++++++++++++++++++++++++++++++++++++++++ command/root.go | 5 +++ 2 files changed, 103 insertions(+) create mode 100644 command/hackyHelp.go diff --git a/command/hackyHelp.go b/command/hackyHelp.go new file mode 100644 index 000000000..db2c5b867 --- /dev/null +++ b/command/hackyHelp.go @@ -0,0 +1,98 @@ +package command + +import ( + "fmt" + "io" + "reflect" + "strconv" + "strings" + "text/template" + "unicode" + + "github.com/spf13/cobra" +) + +func hackyHelp(command *cobra.Command) { + trimRightSpace := func(s string) string { + return strings.TrimRightFunc(s, unicode.IsSpace) + } + + appendIfNotPresent := func(s, stringToAppend string) string { + if strings.Contains(s, stringToAppend) { + return s + } + return s + " " + stringToAppend + } + + Gt := func(a interface{}, b interface{}) bool { + var left, right int64 + av := reflect.ValueOf(a) + + switch av.Kind() { + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: + left = int64(av.Len()) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + left = av.Int() + case reflect.String: + left, _ = strconv.ParseInt(av.String(), 10, 64) + } + + bv := reflect.ValueOf(b) + + switch bv.Kind() { + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: + right = int64(bv.Len()) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + right = bv.Int() + case reflect.String: + right, _ = strconv.ParseInt(bv.String(), 10, 64) + } + + return left > right + } + + // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra. + + // Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic. + Eq := func(a interface{}, b interface{}) bool { + av := reflect.ValueOf(a) + bv := reflect.ValueOf(b) + + switch av.Kind() { + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: + panic("Eq called on unsupported type") + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return av.Int() == bv.Int() + case reflect.String: + return av.String() == bv.String() + } + return false + } + + rpad := func(s string, padding int) string { + template := fmt.Sprintf("%%-%ds", padding) + return fmt.Sprintf(template, s) + } + + templateFuncs := template.FuncMap{ + "trim": strings.TrimSpace, + "trimRightSpace": trimRightSpace, + "trimTrailingWhitespaces": trimRightSpace, + "appendIfNotPresent": appendIfNotPresent, + "rpad": rpad, + "gt": Gt, + "eq": Eq, + } + + 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 a2d8fed95..87e44bd29 100644 --- a/command/root.go +++ b/command/root.go @@ -249,6 +249,11 @@ func determineBaseRepo(cmd *cobra.Command, ctx context.Context) (ghrepo.Interfac } func rootHelpFunc(command *cobra.Command, s []string) { + if command != RootCmd { + hackyHelp(command) + return + } + type helpEntry struct { Title string Body string From ba1b3424c19e0b608c638a1206e922d22ff52c01 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 8 May 2020 09:22:50 -0700 Subject: [PATCH 2/3] Remove unused funcs Co-Authored-By: Nate Smith --- command/hackyHelp.go | 58 -------------------------------------------- 1 file changed, 58 deletions(-) diff --git a/command/hackyHelp.go b/command/hackyHelp.go index db2c5b867..0d9d65b61 100644 --- a/command/hackyHelp.go +++ b/command/hackyHelp.go @@ -3,8 +3,6 @@ package command import ( "fmt" "io" - "reflect" - "strconv" "strings" "text/template" "unicode" @@ -17,58 +15,6 @@ func hackyHelp(command *cobra.Command) { return strings.TrimRightFunc(s, unicode.IsSpace) } - appendIfNotPresent := func(s, stringToAppend string) string { - if strings.Contains(s, stringToAppend) { - return s - } - return s + " " + stringToAppend - } - - Gt := func(a interface{}, b interface{}) bool { - var left, right int64 - av := reflect.ValueOf(a) - - switch av.Kind() { - case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: - left = int64(av.Len()) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - left = av.Int() - case reflect.String: - left, _ = strconv.ParseInt(av.String(), 10, 64) - } - - bv := reflect.ValueOf(b) - - switch bv.Kind() { - case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: - right = int64(bv.Len()) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - right = bv.Int() - case reflect.String: - right, _ = strconv.ParseInt(bv.String(), 10, 64) - } - - return left > right - } - - // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra. - - // Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic. - Eq := func(a interface{}, b interface{}) bool { - av := reflect.ValueOf(a) - bv := reflect.ValueOf(b) - - switch av.Kind() { - case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: - panic("Eq called on unsupported type") - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return av.Int() == bv.Int() - case reflect.String: - return av.String() == bv.String() - } - return false - } - rpad := func(s string, padding int) string { template := fmt.Sprintf("%%-%ds", padding) return fmt.Sprintf(template, s) @@ -76,12 +22,8 @@ func hackyHelp(command *cobra.Command) { templateFuncs := template.FuncMap{ "trim": strings.TrimSpace, - "trimRightSpace": trimRightSpace, "trimTrailingWhitespaces": trimRightSpace, - "appendIfNotPresent": appendIfNotPresent, "rpad": rpad, - "gt": Gt, - "eq": Eq, } tmpl := func(w io.Writer, text string, data interface{}) error { 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 3/3] 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 }