Avoid relying on Cobra help internals

This commit is contained in:
Mislav Marohnić 2020-05-11 12:38:22 +02:00
parent ba1b3424c1
commit 1f0db96f2a
2 changed files with 3 additions and 41 deletions

View file

@ -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)
}
}

View file

@ -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
}