Skip to content

Commit a2448d5

Browse files
committed
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>
1 parent 1caecb9 commit a2448d5

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

cli-plugins/hooks/hook_utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
const (
8-
hookTemplateCommandName = `{{.Name}}`
9-
hookTemplateFlagValue = `{{.FlagValue %q}}`
10-
hookTemplateArg = `{{.Arg %d}}`
8+
hookTemplateCommandName = `{{command}}`
9+
hookTemplateFlagValue = `{{flagValue %q}}`
10+
hookTemplateArg = `{{argValue %d}}`
1111
)
1212

1313
// TemplateReplaceSubcommandName returns a hook template string

cli-plugins/hooks/hooks_utils_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ func TestTemplateHelpers(t *testing.T) {
1515
{
1616
doc: "subcommand name",
1717
got: hooks.TemplateReplaceSubcommandName,
18-
want: `{{.Name}}`,
18+
want: `{{command}}`,
1919
},
2020
{
2121
doc: "flag value",
2222
got: func() string {
2323
return hooks.TemplateReplaceFlagValue("name")
2424
},
25-
want: `{{.FlagValue "name"}}`,
25+
want: `{{flagValue "name"}}`,
2626
},
2727
{
2828
doc: "arg",
2929
got: func() string {
3030
return hooks.TemplateReplaceArg(0)
3131
},
32-
want: `{{.Arg 0}}`,
32+
want: `{{argValue 0}}`,
3333
},
3434
{
3535
doc: "arg",
3636
got: func() string {
3737
return hooks.TemplateReplaceArg(3)
3838
},
39-
want: `{{.Arg 3}}`,
39+
want: `{{argValue 3}}`,
4040
},
4141
}
4242

cli-plugins/hooks/template.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
1919
msgContext := commandInfo{cmd: cmd}
2020

2121
tmpl, err := template.New("").Funcs(template.FuncMap{
22+
"command": msgContext.command,
23+
"flagValue": msgContext.flagValue,
24+
"argValue": msgContext.argValue,
25+
2226
// kept for backward-compatibility with old templates.
23-
"flag": func(_ any, flagName string) (string, error) { return msgContext.FlagValue(flagName) },
24-
"arg": func(_ any, i int) (string, error) { return msgContext.Arg(i) },
27+
"flag": func(_ any, flagName string) (string, error) { return msgContext.flagValue(flagName) },
28+
"arg": func(_ any, i int) (string, error) { return msgContext.argValue(i) },
2529
}).Parse(hookTemplate)
2630
if err != nil {
2731
return nil, err
@@ -51,14 +55,19 @@ type commandInfo struct {
5155
//
5256
// It's used for backward-compatibility with old templates.
5357
func (c commandInfo) Name() string {
58+
return c.command()
59+
}
60+
61+
// command returns the name of the (sub)command for which the hook was invoked.
62+
func (c commandInfo) command() string {
5463
if c.cmd == nil {
5564
return ""
5665
}
5766
return c.cmd.Name()
5867
}
5968

60-
// FlagValue returns the value that was set for the given flag when the hook was invoked.
61-
func (c commandInfo) FlagValue(flagName string) (string, error) {
69+
// flagValue returns the value that was set for the given flag when the hook was invoked.
70+
func (c commandInfo) flagValue(flagName string) (string, error) {
6271
if c.cmd == nil {
6372
return "", fmt.Errorf("%w: flagValue: cmd is nil", ErrHookTemplateParse)
6473
}
@@ -69,8 +78,8 @@ func (c commandInfo) FlagValue(flagName string) (string, error) {
6978
return f.Value.String(), nil
7079
}
7180

72-
// Arg returns the value of the nth argument.
73-
func (c commandInfo) Arg(n int) (string, error) {
81+
// argValue returns the value of the nth argument.
82+
func (c commandInfo) argValue(n int) (string, error) {
7483
if c.cmd == nil {
7584
return "", fmt.Errorf("%w: arg: cmd is nil", ErrHookTemplateParse)
7685
}

0 commit comments

Comments
 (0)