Skip to content

Commit 28020f2

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 4da4902 commit 28020f2

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
@@ -17,9 +17,13 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
1717
msgContext := commandInfo{cmd: cmd}
1818

1919
tmpl, err := template.New("").Funcs(template.FuncMap{
20+
"command": msgContext.command,
21+
"flagValue": msgContext.flagValue,
22+
"argValue": msgContext.argValue,
23+
2024
// kept for backward-compatibility with old templates.
21-
"flag": func(_ any, flagName string) (string, error) { return msgContext.FlagValue(flagName) },
22-
"arg": func(_ any, i int) (string, error) { return msgContext.Arg(i) },
25+
"flag": func(_ any, flagName string) (string, error) { return msgContext.flagValue(flagName) },
26+
"arg": func(_ any, i int) (string, error) { return msgContext.argValue(i) },
2327
}).Parse(hookTemplate)
2428
if err != nil {
2529
return nil, err
@@ -46,14 +50,19 @@ type commandInfo struct {
4650
//
4751
// It's used for backward-compatibility with old templates.
4852
func (c commandInfo) Name() string {
53+
return c.command()
54+
}
55+
56+
// command returns the name of the (sub)command for which the hook was invoked.
57+
func (c commandInfo) command() string {
4958
if c.cmd == nil {
5059
return ""
5160
}
5261
return c.cmd.Name()
5362
}
5463

55-
// FlagValue returns the value that was set for the given flag when the hook was invoked.
56-
func (c commandInfo) FlagValue(flagName string) (string, error) {
64+
// flagValue returns the value that was set for the given flag when the hook was invoked.
65+
func (c commandInfo) flagValue(flagName string) (string, error) {
5766
if c.cmd == nil {
5867
return "", fmt.Errorf("%w: flagValue: cmd is nil", ErrHookTemplateParse)
5968
}
@@ -64,8 +73,8 @@ func (c commandInfo) FlagValue(flagName string) (string, error) {
6473
return f.Value.String(), nil
6574
}
6675

67-
// Arg returns the value of the nth argument.
68-
func (c commandInfo) Arg(n int) (string, error) {
76+
// argValue returns the value of the nth argument.
77+
func (c commandInfo) argValue(n int) (string, error) {
6978
if c.cmd == nil {
7079
return "", fmt.Errorf("%w: arg: cmd is nil", ErrHookTemplateParse)
7180
}

0 commit comments

Comments
 (0)