Skip to content

Commit

Permalink
implement 'include' template function (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanerock authored Oct 25, 2021
1 parent 2eae97c commit 1723031
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
25 changes: 22 additions & 3 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bytes"
"fmt"
"os/exec"
"strings"
"text/template"
Expand All @@ -10,19 +12,36 @@ import (
"gopkg.in/yaml.v2"
)

var funcMap = getFuncMap()

func getFuncMap() template.FuncMap {
func getFuncMap(t *template.Template) template.FuncMap {
f := sprig.TxtFuncMap()
delete(f, "env")
delete(f, "expandenv")

f["include"] = include(t)
f["shell"] = shell
f["toYaml"] = toYaml

return f
}

func include(t *template.Template) func(templateName string, vars ...interface{}) (string, error) {
return func(templateName string, vars ...interface{}) (string, error) {
if len(vars) > 1 {
return "", errors.New(fmt.Sprintf("Call to include may pass zero or one vars structure, got %v.", len(vars)))
}
buf := bytes.NewBuffer(nil)
included := t.Lookup(templateName)
if included == nil {
return "", errors.New(fmt.Sprintf("No such template '%v' found while calling 'include'.", templateName))
}

if err := included.ExecuteTemplate(buf, templateName, vars[0]); err != nil {
return "", err
}
return buf.String(), nil
}
}

func shell(cmd ...string) (string, error) {
out, err := exec.Command("bash", "-c", strings.Join(cmd[:], "")).Output()
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions gucci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ func TestSub(t *testing.T) {
}
}

func TestFuncIncludeNoVars(t *testing.T) {
tpl := `{{ define "a" }}Hi Jane{{ end }}{{ include "a" . }}`
if err := runTest(tpl, "Hi Jane"); err != nil {
t.Error(err)
}
}

func TestFuncIncludeWithVars(t *testing.T) {
tpl := `{{ define "a" }}Hi {{ .name }}{{ end }}{{ $_ := set . "name" "John" }}{{ include "a" . }}`
if err := runTest(tpl, "Hi John"); err != nil {
t.Error(err)
}
}

func TestFuncIncludePipe(t *testing.T) {
tpl := `{{ define "a" }}Hi Jane{{ end }}{{ include "a" . | indent 2 }}`
if err := runTest(tpl, " Hi Jane"); err != nil {
t.Error(err)
}
}

func TestFuncIncludeTooManyVars(t *testing.T) {
tpl := `{{ define "a" }}Hi {{ .name }}{{ end }}{{ $_ := set . "name" "John" }}{{ include "a" . . }}`
if err := runTest(tpl, ""); err == nil {
t.Error("expected error to many vars")
}
}

func TestFuncIncludeBadName(t *testing.T) {
tpl := `{{ define "a" }}Hi Jane{{ end }}{{ include "b" . }}`
if err := runTest(tpl, ""); err == nil {
t.Error("expected error bad template name")
}
}

func TestFuncShell(t *testing.T) {
tpl := `{{ shell "echo hello" }}`
if err := runTest(tpl, "hello"); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func getKeyVal(item string) (key, val string) {

func loadTemplateFile(tplFile string) (*template.Template, error) {
tplName := filepath.Base(tplFile)
tpl, err := template.New(tplName).Funcs(funcMap).ParseFiles(tplFile)
tpl := template.New(tplName)
_, err := tpl.Funcs(getFuncMap(tpl)).ParseFiles(tplFile)
if err != nil {
return nil, fmt.Errorf("Error parsing template(s): %v", err)
}
Expand All @@ -47,7 +48,8 @@ func loadTemplateStream(name string, in io.Reader) (*template.Template, error) {
}

func loadTemplateString(name, s string) (*template.Template, error) {
tpl, err := template.New(name).Funcs(funcMap).Parse(s)
tpl := template.New(name)
tpl, err := tpl.Funcs(getFuncMap(tpl)).Parse(s)
if err != nil {
return nil, fmt.Errorf("Error parsing template(s): %v", err)
}
Expand Down

0 comments on commit 1723031

Please sign in to comment.