From 0bbab7ac3cf311c507058338d93cda2dcdefad84 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Tue, 25 Nov 2025 09:12:46 +0100 Subject: [PATCH] allocate writeFile and backupFile just once --- internal/app/wwctl/overlay/show/main.go | 8 +++---- internal/pkg/configure/hostfile.go | 4 ++-- internal/pkg/overlay/overlay.go | 30 +++++++++++-------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/internal/app/wwctl/overlay/show/main.go b/internal/app/wwctl/overlay/show/main.go index 2169beb9..ceeffd3d 100644 --- a/internal/app/wwctl/overlay/show/main.go +++ b/internal/app/wwctl/overlay/show/main.go @@ -91,8 +91,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1]) if foundFileComment { if !Quiet { - wwlog.Info("backupFile: %v", backupFile) - wwlog.Info("writeFile: %v", writeFile) + wwlog.Info("backupFile: %v", *backupFile) + wwlog.Info("writeFile: %v", *writeFile) wwlog.Info("Filename: %s", destFileName) } wwlog.Output("%s", outBuffer.String()) @@ -105,8 +105,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } } if !Quiet { - wwlog.Info("backupFile: %v", backupFile) - wwlog.Info("writeFile: %v", writeFile) + wwlog.Info("backupFile: %v", *backupFile) + wwlog.Info("writeFile: %v", *writeFile) wwlog.Info("Filename: %s", destFileName) } wwlog.Output("%s", outBuffer.String()) diff --git a/internal/pkg/configure/hostfile.go b/internal/pkg/configure/hostfile.go index 1e718a6f..da0814a1 100644 --- a/internal/pkg/configure/hostfile.go +++ b/internal/pkg/configure/hostfile.go @@ -49,8 +49,8 @@ func Hostfile() (err error) { return } - if writeFile { - err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, backupFile, info.Mode()) + if *writeFile { + err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, *backupFile, info.Mode()) if err != nil { return fmt.Errorf("could not write file from template: %w", err) } diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 0206204d..d1a3c467 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -284,11 +284,7 @@ func (overlay Overlay) ParseVars(file string) []string { return nil } - var ( - writeFile bool - backupFile bool - ) - funcMap := getTemplateFuncMap(fullPath, TemplateStruct{}, &writeFile, &backupFile) + funcMap, _, _ := getTemplateFuncMap(fullPath, TemplateStruct{}) tmpl, err := template.New(path.Base(fullPath)).Option("missingkey=default").Funcs(funcMap).ParseFiles(fullPath) if err != nil { 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 { return fmt.Errorf("failed to render template %s: %w", walkPath, err) } - if !writeFile { + if !*writeFile { return nil } var fileBuffer bytes.Buffer @@ -676,7 +672,7 @@ func BuildOverlayIndir(nodeData node.Node, allNodes []node.Node, overlayNames [] } else if len(filenameFromTemplate) != 0 { wwlog.Debug("Writing file %s", filenameFromTemplate[0][1]) if writingToNamedFile && !isLink { - err = CarefulWriteBuffer(outputPath, fileBuffer, backupFile, info.Mode()) + err = CarefulWriteBuffer(outputPath, fileBuffer, *backupFile, info.Mode()) if err != nil { 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 { - err = CarefulWriteBuffer(outputPath, fileBuffer, backupFile, info.Mode()) + err = CarefulWriteBuffer(outputPath, fileBuffer, *backupFile, info.Mode()) if err != nil { 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 // 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 - funcMap := template.FuncMap{ + _writeFile := true + _backupFile := true + writeFile = &_writeFile + backupFile = &_backupFile + funcMap = template.FuncMap{ "Include": templateFileInclude, "IncludeFrom": templateImageFileInclude, "IncludeBlock": templateFileBlock, @@ -820,7 +820,7 @@ func getTemplateFuncMap(fileName string, data TemplateStruct, writeFile *bool, b for key, value := range sprig.TxtFuncMap() { 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. */ func RenderTemplateFile(fileName string, data TemplateStruct) ( - buffer bytes.Buffer, - backupFile bool, - writeFile bool, + buffer bytes.Buffer, backupFile, writeFile *bool, 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 tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(funcMap).ParseGlob(fileName)