1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-08 15:30:05 +00:00

Add support for executing sub-templates

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-03-22 16:22:23 +01:00
parent dcf3eafff0
commit 5a833fa112
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
4 changed files with 57 additions and 1 deletions

View file

@ -47,6 +47,14 @@
$ echo '{{ now "2006-01-02 15:04:05" }}' | korvike $ echo '{{ now "2006-01-02 15:04:05" }}' | korvike
2017-04-17 16:27:34 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] }}` - `{{ vault <path> <key> [default value] }}`
Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication. Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication.
```console ```console

8
app.go
View file

@ -77,7 +77,13 @@ func main() {
log.Fatalf("Unable to parse template: %s", err) 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) log.Fatalf("Unable to execute template: %s", err)
} }
} }

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
"strings"
"testing" "testing"
"text/template" "text/template"
"time" "time"
@ -17,6 +18,8 @@ func renderHelper(tpl string, ctx map[string]interface{}) string {
panic(err) panic(err)
} }
SetSubTemplateVariables(ctx)
buf := bytes.NewBufferString("") buf := bytes.NewBufferString("")
if err := t.Execute(buf, ctx); err != nil { if err := t.Execute(buf, ctx); err != nil {
panic(err) panic(err)
@ -75,3 +78,15 @@ func Test_now(t *testing.T) {
t.Errorf("[now] did not produce expected time format") 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
View 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
})
}