@@ -3,60 +3,12 @@ package hooks
33import (
44 "bytes"
55 "errors"
6- "fmt"
7- "strconv"
86 "strings"
97 "text/template"
108
119 "github.com/spf13/cobra"
1210)
1311
14- // TemplateReplaceSubcommandName returns a hook template string
15- // that will be replaced by the CLI subcommand being executed
16- //
17- // Example:
18- //
19- // "you ran the subcommand: " + TemplateReplaceSubcommandName()
20- //
21- // when being executed after the command:
22- // `docker run --name "my-container" alpine`
23- // will result in the message:
24- // `you ran the subcommand: run`
25- func TemplateReplaceSubcommandName () string {
26- return hookTemplateCommandName
27- }
28-
29- // TemplateReplaceFlagValue returns a hook template string
30- // that will be replaced by the flags value.
31- //
32- // Example:
33- //
34- // "you ran a container named: " + TemplateReplaceFlagValue("name")
35- //
36- // when being executed after the command:
37- // `docker run --name "my-container" alpine`
38- // will result in the message:
39- // `you ran a container named: my-container`
40- func TemplateReplaceFlagValue (flag string ) string {
41- return fmt .Sprintf (hookTemplateFlagValue , flag )
42- }
43-
44- // TemplateReplaceArg takes an index i and returns a hook
45- // template string that the CLI will replace the template with
46- // the ith argument, after processing the passed flags.
47- //
48- // Example:
49- //
50- // "run this image with `docker run " + TemplateReplaceArg(0) + "`"
51- //
52- // when being executed after the command:
53- // `docker pull alpine`
54- // will result in the message:
55- // "Run this image with `docker run alpine`"
56- func TemplateReplaceArg (i int ) string {
57- return fmt .Sprintf (hookTemplateArg , strconv .Itoa (i ))
58- }
59-
6012func ParseTemplate (hookTemplate string , cmd * cobra.Command ) ([]string , error ) {
6113 tmpl := template .New ("" ).Funcs (commandFunctions )
6214 tmpl , err := tmpl .Parse (hookTemplate )
@@ -73,12 +25,6 @@ func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
7325
7426var ErrHookTemplateParse = errors .New ("failed to parse hook template" )
7527
76- const (
77- hookTemplateCommandName = "{{.Name}}"
78- hookTemplateFlagValue = `{{flag . "%s"}}`
79- hookTemplateArg = "{{arg . %s}}"
80- )
81-
8228var commandFunctions = template.FuncMap {
8329 "flag" : getFlagValue ,
8430 "arg" : getArgValue ,
0 commit comments