Skip to content

Commit

Permalink
Support .tpl extensions for values files (#168)
Browse files Browse the repository at this point in the history
For users who are more accustomed to go templates from helm itself.
  • Loading branch information
michaeljguarino authored Apr 17, 2024
1 parent f11c56b commit 14e2cf0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/manifests/template/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ func (h *helm) valuesFile(svc *console.GetServiceDeploymentForAgent_ServiceDeplo

if strings.HasSuffix(filename, ".liquid") {
data, err = renderLiquid(data, svc)
if err != nil {
return nil, err
}
}

if strings.HasSuffix(filename, ".tpl") {
data, err = renderTpl(data, svc)
}

if err != nil {
return nil, err
}

if err := yaml.Unmarshal(data, &currentMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filename)
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/manifests/template/tpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package template

import (
"bytes"
"text/template"

"github.com/Masterminds/sprig/v3"
console "github.com/pluralsh/console-client-go"
)

func renderTpl(input []byte, svc *console.GetServiceDeploymentForAgent_ServiceDeployment) ([]byte, error) {
bindings := map[string]interface{}{
"Configuration": configMap(svc),
"Cluster": clusterConfiguration(svc.Cluster),
"Contexts": contexts(svc),
}

tpl, err := template.New("gotpl").Funcs(sprig.TxtFuncMap()).Parse(string(input))
if err != nil {
return nil, err
}

var buffer bytes.Buffer
err = tpl.Execute(&buffer, bindings)
return buffer.Bytes(), err
}

0 comments on commit 14e2cf0

Please sign in to comment.