Skip to content

Commit

Permalink
templating uses json, to support complex vars
Browse files Browse the repository at this point in the history
  • Loading branch information
speier committed Dec 11, 2024
1 parent 6febfec commit 5b201e3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions internal/schema/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func (c *Component) ResolveVars(stacks *Stacks, executor Executor) error {
}

// set backend from stack's backend template
c.Backend.Config = tplmap(c.Stack.Backend.Config, tdata, funcMap)
c.Backend.Config = tpl(c.Stack.Backend.Config, tdata, funcMap)

// template vars
c.Inputs = tplmap(c.Inputs, tdata, funcMap)
c.Inputs = tpl(c.Inputs, tdata, funcMap)

// template providers
c.Providers = tplmap(c.Providers, tdata, funcMap)
c.Providers = tpl(c.Providers, tdata, funcMap)

return nil
}
Expand Down
57 changes: 22 additions & 35 deletions internal/schema/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,42 @@ package schema

import (
"bytes"
"fmt"
"encoding/json"
"strings"
"text/template"

"github.com/moonwalker/comet/internal/log"
)

func tplstr(s string, data any, funcMap map[string]any) string {
t := template.New("t").Funcs(funcMap)
func tpl(v any, data any, funcMap map[string]any) map[string]interface{} {
res := make(map[string]interface{})

jb, err := json.Marshal(v)
if err != nil {
log.Error("template json marshal failed", "error", err)
return res
}

// remove escaped quotes
js := strings.ReplaceAll(string(jb), `\"`, `"`)

tmpl, err := t.Parse(s)
t := template.New("t").Funcs(funcMap)
tmpl, err := t.Parse(js)
if err != nil {
log.Debug("template parse error", "text", s, "error", err)
return ""
log.Error("template parse failed", "error", err)
return res
}

var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
log.Debug("template execute error", "data", data, "error", err)
return ""
log.Error("template execute failed", "error", err)
return res
}

return b.String()
}

func tplmap(m map[string]interface{}, data any, funcMap map[string]any) map[string]interface{} {
t := template.New("t").Funcs(funcMap)
res := make(map[string]interface{}, len(m))

for k, v := range m {
if m, ok := v.(map[string]interface{}); ok {
res[k] = tplmap(m, data, funcMap)
continue
}

tmpl, err := t.Parse(fmt.Sprintf("%s", v))
if err != nil {
log.Debug("template parse error", "key", k, "value", v, "error", err)
continue
}

var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
log.Debug("template execute error", "key", k, "value", v, "error", err)
continue
}

res[k] = b.String()
err = json.Unmarshal(b.Bytes(), &res)
if err != nil {
log.Error("template json unmarshal failed", "error", err)
}

return res
Expand Down
13 changes: 13 additions & 0 deletions stacks/dev.stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ const helm = {
}

const metsrv = component('metsrv', 'test/modules/kubernetes', {
inputs: {
values: {
sources: ['ingress', 'gateway-httproute'],
provider: 'cloudflare',
extraArgs: ['--cloudflare-proxied', '123 foo', '456 bar', 'baz'],
env: [
{
name: 'CF_API_TOKEN',
value: env.COMET_TEST
}
]
}
},
providers: {
// google: {},
kubernetes: k8s,
Expand Down

0 comments on commit 5b201e3

Please sign in to comment.