Skip to content

Commit dce201d

Browse files
committed
cli-plugins/hooks: move template utils separate from render code
These utilities are used by CLI-plugins; separate them from the render code, which is used by teh CLI-plugin manager. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent e26f94d commit dce201d

2 files changed

Lines changed: 58 additions & 54 deletions

File tree

cli-plugins/hooks/hook_utils.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package hooks
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
const (
9+
hookTemplateCommandName = `{{.Name}}`
10+
hookTemplateFlagValue = `{{flag . "%s"}}`
11+
hookTemplateArg = `{{arg . %s}}`
12+
)
13+
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+
}

cli-plugins/hooks/template.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,12 @@ package hooks
33
import (
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-
6012
func 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

7426
var ErrHookTemplateParse = errors.New("failed to parse hook template")
7527

76-
const (
77-
hookTemplateCommandName = "{{.Name}}"
78-
hookTemplateFlagValue = `{{flag . "%s"}}`
79-
hookTemplateArg = "{{arg . %s}}"
80-
)
81-
8228
var commandFunctions = template.FuncMap{
8329
"flag": getFlagValue,
8430
"arg": getArgValue,

0 commit comments

Comments
 (0)