From f8b374e1994f0975569abc234f6e74c0c055f74a Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 6 May 2022 12:37:48 -0500 Subject: [PATCH] :sparkles: Add repo and tag template funcs --- pkg/template/funcs.go | 13 +++++++++++++ pkg/template/line_comment.go | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 pkg/template/funcs.go diff --git a/pkg/template/funcs.go b/pkg/template/funcs.go new file mode 100644 index 0000000..10754c0 --- /dev/null +++ b/pkg/template/funcs.go @@ -0,0 +1,13 @@ +package template + +import "strings" + +func DockerRepo(image string) string { + split := strings.SplitN(image, ":", 2) + return split[0] +} + +func DockerTag(image string) string { + split := strings.SplitN(image, ":", 2) + return split[1] +} diff --git a/pkg/template/line_comment.go b/pkg/template/line_comment.go index 862407c..d6c8bef 100644 --- a/pkg/template/line_comment.go +++ b/pkg/template/line_comment.go @@ -8,6 +8,13 @@ import ( "text/template" ) +var funcMap = sprig.TxtFuncMap() + +func init() { + funcMap["repo"] = DockerRepo + funcMap["tag"] = DockerTag +} + func RecurseNode(conf config.Config, node *yaml.Node) error { if len(node.Content) == 0 { if err := TemplateLineComment(conf, node); err != nil { @@ -26,7 +33,7 @@ func RecurseNode(conf config.Config, node *yaml.Node) error { func TemplateLineComment(conf config.Config, node *yaml.Node) error { if node.LineComment != "" && strings.HasPrefix(node.LineComment, conf.Prefix) { tmpl, err := template.New(""). - Funcs(sprig.TxtFuncMap()). + Funcs(funcMap). Delims(conf.LeftDelim, conf.RightDelim). Option("missingkey=error"). Parse(strings.TrimSpace(node.LineComment[len(conf.Prefix):]))