feat(templater): Added support for file include
Adds a 'fileContent' template function to insert the literal contents of a file specified in the template. Signed-off-by: Niklas Wik <niklas.wik@nokia.com>
This commit is contained in:
parent
850fdcf3e0
commit
bafb792339
4 changed files with 30 additions and 2 deletions
|
@ -72,10 +72,11 @@ right.
|
||||||
|
|
||||||
Some template functions come from Go's standard library and are listed in the
|
Some template functions come from Go's standard library and are listed in the
|
||||||
[Go documentation][]. In addition the functions declared by [sprig][] are
|
[Go documentation][]. In addition the functions declared by [sprig][] are
|
||||||
available in kontemplate, as well as two custom functions:
|
available in kontemplate, as well as three custom functions:
|
||||||
|
|
||||||
`json`: Encodes any supplied data structure as JSON.
|
`json`: Encodes any supplied data structure as JSON.
|
||||||
`passLookup`: Looks up the supplied key in [pass][]
|
`passLookup`: Looks up the supplied key in [pass][].
|
||||||
|
`fromFile`: Insert the contents of the given file from the resource set folder.
|
||||||
|
|
||||||
## Examples:
|
## Examples:
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ spec:
|
||||||
value: {{ .importantFeature }}
|
value: {{ .importantFeature }}
|
||||||
- name: SOME_GLOBAL_VAR
|
- name: SOME_GLOBAL_VAR
|
||||||
value: {{ .globalVar }}
|
value: {{ .globalVar }}
|
||||||
|
- name: FILE_VAR
|
||||||
|
value: {{ fileContent "some-api/filevar.txt" }}
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
|
|
24
templater/fromfile.go
Normal file
24
templater/fromfile.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
// Copyright (C) 2017 Niklas Wik <niklas.wik@nokia.com>
|
||||||
|
//
|
||||||
|
// This file is part of Kontemplate.
|
||||||
|
//
|
||||||
|
// Kontemplate is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
|
||||||
|
package templater
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
//GetFromFile returns file content as string
|
||||||
|
func GetFromFile(file string) (string, error) {
|
||||||
|
|
||||||
|
data, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(data), nil
|
||||||
|
}
|
|
@ -193,6 +193,7 @@ func templateFuncs() template.FuncMap {
|
||||||
}
|
}
|
||||||
m["passLookup"] = GetFromPass
|
m["passLookup"] = GetFromPass
|
||||||
m["lookupIPAddr"] = GetIPsFromDNS
|
m["lookupIPAddr"] = GetIPsFromDNS
|
||||||
|
m["fileContent"] = GetFromFile
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue