diff options
author | Vincent Ambo <tazjin@gmail.com> | 2018-03-09T13·49+0100 |
---|---|---|
committer | Vincent Ambo <github@tazj.in> | 2018-03-09T13·54+0100 |
commit | b8722ce83bce727d88a61cecff3343d3046e75f7 (patch) | |
tree | d3ebcea70f8944d70093bf4201dc0f15468909ba /templater/templater.go | |
parent | bafb792339b5898a0e6b6219ad54b5f501d727c2 (diff) |
refactor(templater): Pass resource set path to insertFile function
This is actually several refactors in one: * rename "fileContent" function to "insertFile" * pass the resource set path to the "insetFile" function * update docs and example with a pipeline including indentation adjustments for the inserted file
Diffstat (limited to 'templater/templater.go')
-rw-r--r-- | templater/templater.go | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/templater/templater.go b/templater/templater.go index 5bdb2f2e3bb1..fd514a5ac06f 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -119,7 +119,7 @@ func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files } func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) { - tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs()).Option(failOnMissingKeys).ParseFiles(filename) + tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs(rs)).Option(failOnMissingKeys).ParseFiles(filename) if err != nil { return "", meep.New( @@ -185,7 +185,7 @@ func matchesResourceSet(s *[]string, rs *context.ResourceSet) bool { return false } -func templateFuncs() template.FuncMap { +func templateFuncs(rs *context.ResourceSet) template.FuncMap { m := sprig.TxtFuncMap() m["json"] = func(data interface{}) string { b, _ := json.Marshal(data) @@ -193,7 +193,14 @@ func templateFuncs() template.FuncMap { } m["passLookup"] = GetFromPass m["lookupIPAddr"] = GetIPsFromDNS - m["fileContent"] = GetFromFile + m["insertFile"] = func(file string) (string, error) { + data, err := ioutil.ReadFile(path.Join(rs.Path, file)) + if err != nil { + return "", err + } + + return string(data), nil + } return m } |