cli-plugins/hooks: simplify templating formats

This allows for slighly cleaner / more natural placeholders, as it
doesn't require the context (`.`) to be specified;

- `{{command}}` instead of `{{.Command}}` or `{{command .}}`
- `{{flagValue "my-flag"}}` instead of `{{.FlagValue "my-flag"}} or `{{flagValue . "my-flag"}}`

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-03-19 12:24:35 +01:00
parent 9243240346
commit dd1f7f5856
3 changed files with 22 additions and 13 deletions
+3 -3
View File
@@ -5,9 +5,9 @@ import (
)
const (
hookTemplateCommandName = `{{.Name}}`
hookTemplateFlagValue = `{{.FlagValue %q}}`
hookTemplateArg = `{{.Arg %d}}`
hookTemplateCommandName = `{{command}}`
hookTemplateFlagValue = `{{flagValue %q}}`
hookTemplateArg = `{{argValue %d}}`
)
// TemplateReplaceSubcommandName returns a hook template string
+4 -4
View File
@@ -15,28 +15,28 @@ func TestTemplateHelpers(t *testing.T) {
{
doc: "subcommand name",
got: hooks.TemplateReplaceSubcommandName,
want: `{{.Name}}`,
want: `{{command}}`,
},
{
doc: "flag value",
got: func() string {
return hooks.TemplateReplaceFlagValue("name")
},
want: `{{.FlagValue "name"}}`,
want: `{{flagValue "name"}}`,
},
{
doc: "arg",
got: func() string {
return hooks.TemplateReplaceArg(0)
},
want: `{{.Arg 0}}`,
want: `{{argValue 0}}`,
},
{
doc: "arg",
got: func() string {
return hooks.TemplateReplaceArg(3)
},
want: `{{.Arg 3}}`,
want: `{{argValue 3}}`,
},
}
+15 -6
View File
@@ -17,9 +17,13 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
msgContext := commandInfo{cmd: cmd}
tmpl, err := template.New("").Funcs(template.FuncMap{
"command": msgContext.command,
"flagValue": msgContext.flagValue,
"argValue": msgContext.argValue,
// kept for backward-compatibility with old templates.
"flag": func(_ any, flagName string) (string, error) { return msgContext.FlagValue(flagName) },
"arg": func(_ any, i int) (string, error) { return msgContext.Arg(i) },
"flag": func(_ any, flagName string) (string, error) { return msgContext.flagValue(flagName) },
"arg": func(_ any, i int) (string, error) { return msgContext.argValue(i) },
}).Parse(hookTemplate)
if err != nil {
return nil, err
@@ -46,14 +50,19 @@ type commandInfo struct {
//
// It's used for backward-compatibility with old templates.
func (c commandInfo) Name() string {
return c.command()
}
// command returns the name of the (sub)command for which the hook was invoked.
func (c commandInfo) command() string {
if c.cmd == nil {
return ""
}
return c.cmd.Name()
}
// FlagValue returns the value that was set for the given flag when the hook was invoked.
func (c commandInfo) FlagValue(flagName string) (string, error) {
// flagValue returns the value that was set for the given flag when the hook was invoked.
func (c commandInfo) flagValue(flagName string) (string, error) {
if c.cmd == nil {
return "", fmt.Errorf("%w: flagValue: cmd is nil", ErrHookTemplateParse)
}
@@ -64,8 +73,8 @@ func (c commandInfo) FlagValue(flagName string) (string, error) {
return f.Value.String(), nil
}
// Arg returns the value of the nth argument.
func (c commandInfo) Arg(n int) (string, error) {
// argValue returns the value of the nth argument.
func (c commandInfo) argValue(n int) (string, error) {
if c.cmd == nil {
return "", fmt.Errorf("%w: arg: cmd is nil", ErrHookTemplateParse)
}