Skip to content

Commit

Permalink
fix: trim space from templates before render
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza committed Mar 10, 2024
1 parent d98d2db commit f4cd85a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/provisioners/templateprov/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"fmt"
"html/template"
"log/slog"
"strings"

"github.com/Masterminds/sprig/v3"
compose "github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -65,6 +67,7 @@ func (p *Provisioner) Match(resUid project.ResourceUid) bool {
}

func renderTemplateAndDecode(raw string, data interface{}, out interface{}) error {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil
}
Expand All @@ -76,9 +79,13 @@ func renderTemplateAndDecode(raw string, data interface{}, out interface{}) erro
if err := prepared.Execute(buff, data); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
if strings.TrimSpace(buff.String()) == "" {
return nil
}
dec := yaml.NewDecoder(buff)
dec.KnownFields(true)
if err := dec.Decode(out); err != nil {
slog.Debug(fmt.Sprintf("template output was '%s'", buff.String()))
return fmt.Errorf("failed to decode output: %w", err)
}
return nil
Expand Down

0 comments on commit f4cd85a

Please sign in to comment.