moved overlay parsing to own function

This commit is contained in:
Christian Goll
2022-05-24 19:41:31 +02:00
parent a641647ba3
commit 1b02c89ca7

View File

@@ -333,39 +333,8 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir
tstruct.BuildSource = path.Join(overlaySourceDir, location)
wwlog.Verbose("Evaluating overlay template file: %s", location)
destFile := strings.TrimSuffix(location, ".ww")
backupFile := true
writeFile := true
tmpl, err := template.New(path.Base(location)).Option("missingkey=default").Funcs(template.FuncMap{
// TODO: Fix for missingkey=zero
"Include": templateFileInclude,
"IncludeFrom": templateContainerFileInclude,
"IncludeBlock": templateFileBlock,
"inc": func(i int) int { return i + 1 },
"dec": func(i int) int { return i - 1 },
"file": func(str string) string { return fmt.Sprintf("{{ /* file \"%s\" */ }}", str) },
"abort": func() string {
wwlog.Debug("abort file called in %s", location)
writeFile = false
return ""
},
"nobackup": func() string {
wwlog.Debug("not backup for %s", location)
backupFile = false
return ""
},
"split": func(s string, d string) []string {
return strings.Split(s, d)
},
// }).ParseGlob(path.Join(OverlayDir, destFile+".ww*"))
}).ParseGlob(location)
if err != nil {
return errors.Wrap(err, "could not parse template "+location)
}
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, tstruct)
if err != nil {
return errors.Wrap(err, "could not execute template")
}
buffer, backupFile, writeFile, err := RenderTemplateFile(location, tstruct)
if writeFile {
destFileName := destFile
var fileBuffer bytes.Buffer
@@ -464,6 +433,49 @@ func carefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, p
return err
}
/*
Parses the template with the given filename, variables must be in data. Returns the
parsed template as bytes.Buffer, and the bool variables for backupFile and writeFile.
If something goes wrong an error is returned.
*/
func RenderTemplateFile(fileName string, data TemplateStruct) (buffer bytes.Buffer, backupFile bool, writeFile bool, err error) {
backupFile = true
writeFile = true
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(template.FuncMap{
// TODO: Fix for missingkey=zero
"Include": templateFileInclude,
"IncludeFrom": templateContainerFileInclude,
"IncludeBlock": templateFileBlock,
"inc": func(i int) int { return i + 1 },
"dec": func(i int) int { return i - 1 },
"file": func(str string) string { return fmt.Sprintf("{{ /* file \"%s\" */ }}", str) },
"abort": func() string {
wwlog.Printf(wwlog.DEBUG, "abort file called in %s\n", fileName)
writeFile = false
return ""
},
"nobackup": func() string {
wwlog.Printf(wwlog.DEBUG, "not backup for %s\n", fileName)
backupFile = false
return ""
},
"split": func(s string, d string) []string {
return strings.Split(s, d)
},
// }).ParseGlob(path.Join(OverlayDir, destFile+".ww*"))
}).ParseGlob(fileName)
if err != nil {
err = errors.Wrap(err, "could not parse template "+fileName)
return
}
err = tmpl.Execute(&buffer, data)
if err != nil {
err = errors.Wrap(err, "could not execute template")
return
}
return
}
// Simple version of ScanLines, but include the line break
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {