@@ -3,6 +3,7 @@ package hooks
33import (
44 "bytes"
55 "errors"
6+ "fmt"
67 "strings"
78 "text/template"
89
@@ -13,13 +14,18 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
1314 out := hookTemplate
1415 if strings .Contains (hookTemplate , "{{" ) {
1516 // Message may be a template.
16- tmpl := template .New ("" ).Funcs (commandFunctions )
17- tmpl , err := tmpl .Parse (hookTemplate )
17+ msgContext := commandInfo {cmd : cmd }
18+
19+ tmpl , err := template .New ("" ).Funcs (template.FuncMap {
20+ // 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 ) },
23+ }).Parse (hookTemplate )
1824 if err != nil {
1925 return nil , err
2026 }
2127 var b bytes.Buffer
22- err = tmpl .Execute (& b , cmd )
28+ err = tmpl .Execute (& b , msgContext )
2329 if err != nil {
2430 return nil , err
2531 }
@@ -30,23 +36,43 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
3036
3137var ErrHookTemplateParse = errors .New ("failed to parse hook template" )
3238
33- var commandFunctions = template.FuncMap {
34- "flag" : getFlagValue ,
35- "arg" : getArgValue ,
39+ // commandInfo provides info about the command for which the hook was invoked.
40+ // It is used for templated hook-messages.
41+ type commandInfo struct {
42+ cmd * cobra.Command
43+ }
44+
45+ // Name returns the name of the (sub)command for which the hook was invoked.
46+ //
47+ // It's used for backward-compatibility with old templates.
48+ func (c commandInfo ) Name () string {
49+ if c .cmd == nil {
50+ return ""
51+ }
52+ return c .cmd .Name ()
3653}
3754
38- func getFlagValue (cmd * cobra.Command , flag string ) (string , error ) {
39- cmdFlag := cmd .Flag (flag )
40- if cmdFlag == nil {
41- return "" , ErrHookTemplateParse
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 ) {
57+ if c .cmd == nil {
58+ return "" , fmt .Errorf ("%w: flagValue: cmd is nil" , ErrHookTemplateParse )
59+ }
60+ f := c .cmd .Flag (flagName )
61+ if f == nil {
62+ return "" , fmt .Errorf ("%w: flagValue: no flags found" , ErrHookTemplateParse )
4263 }
43- return cmdFlag .Value .String (), nil
64+ return f .Value .String (), nil
4465}
4566
46- func getArgValue (cmd * cobra.Command , i int ) (string , error ) {
47- flags := cmd .Flags ()
48- if flags == nil {
49- return "" , ErrHookTemplateParse
67+ // Arg returns the value of the nth argument.
68+ func (c commandInfo ) Arg (n int ) (string , error ) {
69+ if c .cmd == nil {
70+ return "" , fmt .Errorf ("%w: arg: cmd is nil" , ErrHookTemplateParse )
71+ }
72+ flags := c .cmd .Flags ()
73+ v := flags .Arg (n )
74+ if v == "" && n >= flags .NArg () {
75+ return "" , fmt .Errorf ("%w: arg: %dth argument not set" , ErrHookTemplateParse , n )
5076 }
51- return flags . Arg ( i ) , nil
77+ return v , nil
5278}
0 commit comments