diff --git a/cli-plugins/hooks/hook_utils.go b/cli-plugins/hooks/hook_utils.go index a6345cc1d5..c6babbe469 100644 --- a/cli-plugins/hooks/hook_utils.go +++ b/cli-plugins/hooks/hook_utils.go @@ -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 diff --git a/cli-plugins/hooks/hooks_utils_test.go b/cli-plugins/hooks/hooks_utils_test.go index ba9ad881b4..877909109c 100644 --- a/cli-plugins/hooks/hooks_utils_test.go +++ b/cli-plugins/hooks/hooks_utils_test.go @@ -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}}`, }, } diff --git a/cli-plugins/hooks/template.go b/cli-plugins/hooks/template.go index c92c16d21b..ba77dd3f68 100644 --- a/cli-plugins/hooks/template.go +++ b/cli-plugins/hooks/template.go @@ -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) }