allocate writeFile and backupFile just once
This commit is contained in:
committed by
Jonathon Anderson
parent
983a359c00
commit
0bbab7ac3c
@@ -91,8 +91,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
|||||||
wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1])
|
wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1])
|
||||||
if foundFileComment {
|
if foundFileComment {
|
||||||
if !Quiet {
|
if !Quiet {
|
||||||
wwlog.Info("backupFile: %v", backupFile)
|
wwlog.Info("backupFile: %v", *backupFile)
|
||||||
wwlog.Info("writeFile: %v", writeFile)
|
wwlog.Info("writeFile: %v", *writeFile)
|
||||||
wwlog.Info("Filename: %s", destFileName)
|
wwlog.Info("Filename: %s", destFileName)
|
||||||
}
|
}
|
||||||
wwlog.Output("%s", outBuffer.String())
|
wwlog.Output("%s", outBuffer.String())
|
||||||
@@ -105,8 +105,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !Quiet {
|
if !Quiet {
|
||||||
wwlog.Info("backupFile: %v", backupFile)
|
wwlog.Info("backupFile: %v", *backupFile)
|
||||||
wwlog.Info("writeFile: %v", writeFile)
|
wwlog.Info("writeFile: %v", *writeFile)
|
||||||
wwlog.Info("Filename: %s", destFileName)
|
wwlog.Info("Filename: %s", destFileName)
|
||||||
}
|
}
|
||||||
wwlog.Output("%s", outBuffer.String())
|
wwlog.Output("%s", outBuffer.String())
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func Hostfile() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if writeFile {
|
if *writeFile {
|
||||||
err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, backupFile, info.Mode())
|
err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, *backupFile, info.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write file from template: %w", err)
|
return fmt.Errorf("could not write file from template: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -284,11 +284,7 @@ func (overlay Overlay) ParseVars(file string) []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
funcMap, _, _ := getTemplateFuncMap(fullPath, TemplateStruct{})
|
||||||
writeFile bool
|
|
||||||
backupFile bool
|
|
||||||
)
|
|
||||||
funcMap := getTemplateFuncMap(fullPath, TemplateStruct{}, &writeFile, &backupFile)
|
|
||||||
tmpl, err := template.New(path.Base(fullPath)).Option("missingkey=default").Funcs(funcMap).ParseFiles(fullPath)
|
tmpl, err := template.New(path.Base(fullPath)).Option("missingkey=default").Funcs(funcMap).ParseFiles(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wwlog.Error("Could not parse template file %s: %s", fullPath, err)
|
wwlog.Error("Could not parse template file %s: %s", fullPath, err)
|
||||||
@@ -651,7 +647,7 @@ func BuildOverlayIndir(nodeData node.Node, allNodes []node.Node, overlayNames []
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to render template %s: %w", walkPath, err)
|
return fmt.Errorf("failed to render template %s: %w", walkPath, err)
|
||||||
}
|
}
|
||||||
if !writeFile {
|
if !*writeFile {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var fileBuffer bytes.Buffer
|
var fileBuffer bytes.Buffer
|
||||||
@@ -676,7 +672,7 @@ func BuildOverlayIndir(nodeData node.Node, allNodes []node.Node, overlayNames []
|
|||||||
} else if len(filenameFromTemplate) != 0 {
|
} else if len(filenameFromTemplate) != 0 {
|
||||||
wwlog.Debug("Writing file %s", filenameFromTemplate[0][1])
|
wwlog.Debug("Writing file %s", filenameFromTemplate[0][1])
|
||||||
if writingToNamedFile && !isLink {
|
if writingToNamedFile && !isLink {
|
||||||
err = CarefulWriteBuffer(outputPath, fileBuffer, backupFile, info.Mode())
|
err = CarefulWriteBuffer(outputPath, fileBuffer, *backupFile, info.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write file from template: %w", err)
|
return fmt.Errorf("could not write file from template: %w", err)
|
||||||
}
|
}
|
||||||
@@ -709,7 +705,7 @@ func BuildOverlayIndir(nodeData node.Node, allNodes []node.Node, overlayNames []
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isLink {
|
if !isLink {
|
||||||
err = CarefulWriteBuffer(outputPath, fileBuffer, backupFile, info.Mode())
|
err = CarefulWriteBuffer(outputPath, fileBuffer, *backupFile, info.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write file from template: %w", err)
|
return fmt.Errorf("could not write file from template: %w", err)
|
||||||
}
|
}
|
||||||
@@ -785,9 +781,13 @@ func CarefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, p
|
|||||||
|
|
||||||
// getTemplateFuncMap returns a template.FuncMap with all the functions
|
// getTemplateFuncMap returns a template.FuncMap with all the functions
|
||||||
// for warewulf templates.
|
// for warewulf templates.
|
||||||
func getTemplateFuncMap(fileName string, data TemplateStruct, writeFile *bool, backupFile *bool) template.FuncMap {
|
func getTemplateFuncMap(fileName string, data TemplateStruct) (funcMap template.FuncMap, writeFile, backupFile *bool) {
|
||||||
// Build our FuncMap
|
// Build our FuncMap
|
||||||
funcMap := template.FuncMap{
|
_writeFile := true
|
||||||
|
_backupFile := true
|
||||||
|
writeFile = &_writeFile
|
||||||
|
backupFile = &_backupFile
|
||||||
|
funcMap = template.FuncMap{
|
||||||
"Include": templateFileInclude,
|
"Include": templateFileInclude,
|
||||||
"IncludeFrom": templateImageFileInclude,
|
"IncludeFrom": templateImageFileInclude,
|
||||||
"IncludeBlock": templateFileBlock,
|
"IncludeBlock": templateFileBlock,
|
||||||
@@ -820,7 +820,7 @@ func getTemplateFuncMap(fileName string, data TemplateStruct, writeFile *bool, b
|
|||||||
for key, value := range sprig.TxtFuncMap() {
|
for key, value := range sprig.TxtFuncMap() {
|
||||||
funcMap[key] = value
|
funcMap[key] = value
|
||||||
}
|
}
|
||||||
return funcMap
|
return funcMap, writeFile, backupFile
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -829,15 +829,11 @@ parsed template as bytes.Buffer, and the bool variables for backupFile and write
|
|||||||
If something goes wrong an error is returned.
|
If something goes wrong an error is returned.
|
||||||
*/
|
*/
|
||||||
func RenderTemplateFile(fileName string, data TemplateStruct) (
|
func RenderTemplateFile(fileName string, data TemplateStruct) (
|
||||||
buffer bytes.Buffer,
|
buffer bytes.Buffer, backupFile, writeFile *bool,
|
||||||
backupFile bool,
|
|
||||||
writeFile bool,
|
|
||||||
err error,
|
err error,
|
||||||
) {
|
) {
|
||||||
backupFile = true
|
|
||||||
writeFile = true
|
|
||||||
|
|
||||||
funcMap := getTemplateFuncMap(fileName, data, &writeFile, &backupFile)
|
funcMap, writeFile, backupFile := getTemplateFuncMap(fileName, data)
|
||||||
|
|
||||||
// Create the template with the merged FuncMap
|
// Create the template with the merged FuncMap
|
||||||
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(funcMap).ParseGlob(fileName)
|
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(funcMap).ParseGlob(fileName)
|
||||||
|
|||||||
Reference in New Issue
Block a user