mirror of
https://github.com/Luzifer/korvike.git
synced 2024-12-20 11:11:19 +00:00
Add support for executing sub-templates
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
dcf3eafff0
commit
5a833fa112
4 changed files with 57 additions and 1 deletions
|
@ -47,6 +47,14 @@
|
|||
$ echo '{{ now "2006-01-02 15:04:05" }}' | korvike
|
||||
2017-04-17 16:27:34
|
||||
```
|
||||
- `{{ tplexec (file "my.tpl") }}`
|
||||
Execute the given template with the same function set and variables as the parent template.
|
||||
```console
|
||||
$ export FOO=bar
|
||||
$ echo '{{ env "FOO" }}' >my.tpl
|
||||
$ echo '{{ tplexec (file "my.tpl") }}' | korvike
|
||||
bar
|
||||
```
|
||||
- `{{ vault <path> <key> [default value] }}`
|
||||
Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication.
|
||||
```console
|
||||
|
|
8
app.go
8
app.go
|
@ -77,7 +77,13 @@ func main() {
|
|||
log.Fatalf("Unable to parse template: %s", err)
|
||||
}
|
||||
|
||||
if err := tpl.Execute(out, env.ListToMap(cfg.KeyPairs)); err != nil {
|
||||
vars := map[string]interface{}{}
|
||||
for k, v := range env.ListToMap(cfg.KeyPairs) {
|
||||
vars[k] = v
|
||||
}
|
||||
|
||||
korvike.SetSubTemplateVariables(vars)
|
||||
if err := tpl.Execute(out, vars); err != nil {
|
||||
log.Fatalf("Unable to execute template: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
|
@ -17,6 +18,8 @@ func renderHelper(tpl string, ctx map[string]interface{}) string {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
SetSubTemplateVariables(ctx)
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := t.Execute(buf, ctx); err != nil {
|
||||
panic(err)
|
||||
|
@ -75,3 +78,15 @@ func Test_now(t *testing.T) {
|
|||
t.Errorf("[now] did not produce expected time format")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_tplexec(t *testing.T) {
|
||||
result := randomString()
|
||||
os.Setenv("KORVIKE_TESTING", result)
|
||||
|
||||
if res := renderHelper(`{{ tplexec "{{ .var }}:{{ env \"KORVIKE_TESTING\" }}" }}`, map[string]interface{}{"var": "test"}); res != strings.Join([]string{
|
||||
"test",
|
||||
result,
|
||||
}, ":") {
|
||||
t.Errorf("[template] did not produce expected result %q != test", res)
|
||||
}
|
||||
}
|
||||
|
|
27
functions/func_tplexec.go
Normal file
27
functions/func_tplexec.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package functions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var subTemplateVariables map[string]interface{}
|
||||
|
||||
func SetSubTemplateVariables(m map[string]interface{}) { subTemplateVariables = m }
|
||||
|
||||
func init() {
|
||||
registerFunction("tplexec", func(rawTpl string) (string, error) {
|
||||
tpl, err := template.New("in").Funcs(GetFunctionMap()).Parse(rawTpl)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse template: %w", err)
|
||||
}
|
||||
|
||||
out := new(bytes.Buffer)
|
||||
if err := tpl.Execute(out, subTemplateVariables); err != nil {
|
||||
return "", fmt.Errorf("execute template: %w", err)
|
||||
}
|
||||
|
||||
return out.String(), nil
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue