Skip to content
Myr

Template Function Injection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This patch enables developers to add one to many template functions that can be used by custom Usage and Help templates. Here is an example that is included in the file cobra_test.go as the test function named TestAddTemplateFunctions: AddTemplateFunc("t", func() bool { return true }) AddTemplateFuncs(template.FuncMap{ "f": func() bool { return false }, "h": func() string { return "Hello," }, "w": func() string { return "world." }}) const usage = "Hello, world." c := &Command{} c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) if us := c.UsageString(); us != usage { t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us) } In the above example four functions are added to the template function map used when the Usage and Help text is generated from the templates that enable custom logic as well as data injection during template execution.

authored by akutz · committed by akutz · parent 4c4f2d9 · Browse files · 2 files+40 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+40 added−6 removed

  • other:+40 added−6 removed

2 files+40 added−6 removed

modified:cobra_test.go+18 added
6 unmodified lines
7 "reflect"8 "runtime"9 "strings"10 "testing"11 "text/template"1213 "github.com/spf13/pflag"14)15
955 unmodified lines
971 r := fullSetupTest("--help")972973 checkResultContains(t, r, flagName)974}975976func TestAddTemplateFunctions(t *testing.T) {977 AddTemplateFunc("t", func() bool { return true })978 AddTemplateFuncs(template.FuncMap{979 "f": func() bool { return false }, 980 "h": func() string { return "Hello," }, 981 "w": func() string { return "world." }})982983 const usage = "Hello, world."984 985 c := &Command{}986 c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)987 988 if us := c.UsageString(); us != usage {989 t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)990 }991}
modified:cobra.go+22 added-6 removed
23 unmodified lines
24 "strings"25 "text/template"26)2728var templateFuncs template.FuncMap = template.FuncMap{29 "trim": strings.TrimSpace,30 "rpad": rpad,31 "gt": Gt,32 "eq": Eq,33}3435var initializers []func()3637// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.38// Set this to true to enable it
6 unmodified lines
4546You need to open cmd.exe and run it from there.47`4849//AddTemplateFunc adds a template function that's available to Usage and Help50//template generation.51func AddTemplateFunc(name string, tmplFunc interface{}) {52 templateFuncs[name] = tmplFunc53}5455//AddTemplateFuncs adds multiple template functions availalble to Usage and56//Help template generation.57func AddTemplateFuncs(tmplFuncs template.FuncMap) {58 for k, v := range tmplFuncs {59 templateFuncs[k] = v60 }61}6263//OnInitialize takes a series of func() arguments and appends them to a slice of func().64func OnInitialize(y ...func()) {65 for _, x := range y {66 initializers = append(initializers, x)
54 unmodified lines
121122// tmpl executes the given template text on data, writing the result to w.123func tmpl(w io.Writer, text string, data interface{}) error {124 t := template.New("top")104 t.Funcs(template.FuncMap{105 "trim": strings.TrimSpace,106 "rpad": rpad,107 "gt": Gt,108 "eq": Eq,109 })125 t.Funcs(templateFuncs)126 template.Must(t.Parse(text))127 return t.Execute(w, data)128}