From ab32625e7acb4e05e5f818ddbae9fb92c759cdff Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Tue, 29 Mar 2022 10:26:34 +0200 Subject: [PATCH 1/2] refactored template processing functions --- internal/pkg/overlay/overlay.go | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index a1e054b0..5ba07819 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -44,6 +44,12 @@ func FindRuntimeOverlays() ([]string, error) { } */ +type testVar struct { + calledOnce bool + calledTwice bool + myName string +} + /* Build all overlays (runtime and generic) for a node */ @@ -317,10 +323,9 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir } else if filepath.Ext(location) == ".ww" { tstruct.BuildSource = path.Join(overlaySourceDir, location) wwlog.Printf(wwlog.VERBOSE, "Evaluating overlay template file: %s\n", location) - destFile := strings.TrimSuffix(location, ".ww") - ErrorAbort := errors.New("abort_template") - ErrorNoBackup := errors.New("nobackup_template") + backupFile := true + writeFile := true tmpl, err := template.New(path.Base(location)).Option("missingkey=default").Funcs(template.FuncMap{ // TODO: Fix for missingkey=zero "Include": templateFileInclude, @@ -329,27 +334,26 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir "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, error) { return "", ErrorAbort }, - "nobackup": func() (string, error) { return "", ErrorNoBackup }, + "abort": func() string { + wwlog.Printf(wwlog.DEBUG, "abort file called in %s\n", location) + writeFile = false + return "" + }, + "nobackup": func() string { + wwlog.Printf(wwlog.DEBUG, "not backup for %s\n", location) + backupFile = false + return "" + }, // }).ParseGlob(path.Join(OverlayDir, destFile+".ww*")) }).ParseGlob(location) if err != nil { return errors.Wrap(err, "could not parse template "+location) } var buffer bytes.Buffer - backupFile := true - writeFile := true err = tmpl.Execute(&buffer, tstruct) if err != nil { - // complicated workaround as error is not exported correctly: https://github.com/golang/go/issues/34201 - if strings.Contains(fmt.Sprint(err), "abort_template") { - wwlog.Printf(wwlog.VERBOSE, "Aborting template file due to abort call in template: %s\n", location) - writeFile = false - } else if strings.Contains(fmt.Sprint(err), "nobackup_template") { - backupFile = false - } else { - return errors.Wrap(err, "could not execute template") - } + return errors.Wrap(err, "could not execute template") + } if writeFile { destFileName := destFile From 175e387f633f5b0e60b15d75df25533e10bd2072 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 30 Mar 2022 17:54:13 +0200 Subject: [PATCH 2/2] removed unused varaible --- internal/pkg/overlay/overlay.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 5ba07819..9bddc23f 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -44,12 +44,6 @@ func FindRuntimeOverlays() ([]string, error) { } */ -type testVar struct { - calledOnce bool - calledTwice bool - myName string -} - /* Build all overlays (runtime and generic) for a node */