Skip to content

Commit

Permalink
Add includeSelf template function
Browse files Browse the repository at this point in the history
  • Loading branch information
uvegla committed Oct 9, 2023
1 parent 9e439cb commit 2fba726
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ func (g Generator) renderTemplate(ctx context.Context, templateText string, temp

funcMap := sprig.TxtFuncMap()
funcMap["include"] = g.include
funcMap["includeSelf"] = g.includeSelf

t, err := template.New("main").Funcs(funcMap).Option("missingkey=error").Parse(templateText)
if err != nil {
Expand All @@ -400,7 +401,15 @@ func (g Generator) renderTemplate(ctx context.Context, templateText string, temp
}

func (g Generator) include(templateName string, templateData interface{}) (string, error) {
contents, err := g.fs.ReadFile(path.Join("include", templateName+".yaml.template"))
return g.includeFromRoot("include", templateName, templateData)
}

func (g Generator) includeSelf(templateName string, templateData interface{}) (string, error) {
return g.includeFromRoot("include-self", templateName, templateData)
}

func (g Generator) includeFromRoot(root string, templateName string, templateData interface{}) (string, error) {
contents, err := g.fs.ReadFile(path.Join(root, templateName+".yaml.template"))
if err != nil {
return "", microerror.Mask(err)
}
Expand Down

0 comments on commit 2fba726

Please sign in to comment.