Error logging for overlay show rendering incorrectly checked the overlay name rather than the path to the file. It also used wwlog (which goes to stderr) to output the render. This commit replaces explicit args[] references with their named equivalents from earlier in the function, correcting those references along the way, and outputs the final render with fmt.Print.
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package show
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/hpcng/warewulf/internal/pkg/node"
|
|
"github.com/hpcng/warewulf/internal/pkg/overlay"
|
|
"github.com/hpcng/warewulf/internal/pkg/util"
|
|
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func CobraRunE(cmd *cobra.Command, args []string) error {
|
|
var overlaySourceDir string
|
|
|
|
overlayName := args[0]
|
|
fileName := args[1]
|
|
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
|
|
|
|
if !util.IsDir(overlaySourceDir) {
|
|
wwlog.Error("Overlay does not exist: %s\n", overlayName)
|
|
os.Exit(1)
|
|
}
|
|
|
|
overlayFile := path.Join(overlaySourceDir, fileName)
|
|
|
|
if !util.IsFile(overlayFile) {
|
|
wwlog.Error("File does not exist within overlay: %s:%s\n", overlayName, fileName)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if NodeName == "" {
|
|
f, err := ioutil.ReadFile(overlayFile)
|
|
if err != nil {
|
|
wwlog.Error("Could not read file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Print(string(f))
|
|
} else {
|
|
if !util.IsFile(overlayFile) {
|
|
wwlog.Debug("%s is not a file\n", overlayFile)
|
|
wwlog.Error("%s:%s is not a file\n", overlayName, fileName)
|
|
os.Exit(1)
|
|
}
|
|
if filepath.Ext(overlayFile) != ".ww" {
|
|
wwlog.Warn("%s lacks the '.ww' suffix, will not be rendered in an overlay\n", fileName)
|
|
}
|
|
|
|
nodeDB, err := node.New()
|
|
if err != nil {
|
|
wwlog.Error("Could not open node configuration: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
nodes, err := nodeDB.FindAllNodes()
|
|
if err != nil {
|
|
wwlog.Error("Could not get node list: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
filteredNodes := node.FilterByName(nodes, []string{NodeName})
|
|
if len(filteredNodes) != 1 {
|
|
wwlog.Error("%v does not identify a single node\n", NodeName)
|
|
os.Exit(1)
|
|
}
|
|
tstruct := overlay.InitStruct(filteredNodes[0])
|
|
tstruct.BuildSource = overlayFile
|
|
buffer, backupFile, writeFile, err := overlay.RenderTemplateFile(overlayFile, tstruct)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var outBuffer bytes.Buffer
|
|
// search for magic file name comment
|
|
bufferScanner := bufio.NewScanner(bytes.NewReader(buffer.Bytes()))
|
|
bufferScanner.Split(overlay.ScanLines)
|
|
reg := regexp.MustCompile(`.*{{\s*/\*\s*file\s*["'](.*)["']\s*\*/\s*}}.*`)
|
|
foundFileComment := false
|
|
destFileName := strings.TrimSuffix(fileName, ".ww")
|
|
for bufferScanner.Scan() {
|
|
line := bufferScanner.Text()
|
|
filenameFromTemplate := reg.FindAllStringSubmatch(line, -1)
|
|
if len(filenameFromTemplate) != 0 {
|
|
wwlog.Debug("Found multifile comment, new filename %s\n", filenameFromTemplate[0][1])
|
|
if foundFileComment {
|
|
if !Quiet {
|
|
wwlog.Info("backupFile: %v\nwriteFile: %v\n", backupFile, writeFile)
|
|
wwlog.Info("Filename: %s\n\n", destFileName)
|
|
}
|
|
wwlog.Info("%s", outBuffer.String())
|
|
outBuffer.Reset()
|
|
}
|
|
destFileName = filenameFromTemplate[0][1]
|
|
foundFileComment = true
|
|
} else {
|
|
_, _ = outBuffer.WriteString(line)
|
|
}
|
|
}
|
|
if !Quiet {
|
|
wwlog.Info("backupFile: %v\nwriteFile: %v\n", backupFile, writeFile)
|
|
wwlog.Info("Filename: %s\n\n", destFileName)
|
|
}
|
|
fmt.Print(outBuffer.String())
|
|
}
|
|
return nil
|
|
}
|