refined logging with verbose flag to reduce log output for rendering templates
This commit is contained in:
parent
462088a015
commit
413904c457
3 changed files with 73 additions and 56 deletions
|
|
@ -1,60 +1,71 @@
|
|||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"specCon18/bubblewand/internal/logger"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"specCon18/bubblewand/internal/logger"
|
||||
)
|
||||
|
||||
// ProgramData holds user-supplied template values
|
||||
type ProgramData struct {
|
||||
ModName string
|
||||
PackageName string
|
||||
ProgramVersion string
|
||||
ProgramDesc string
|
||||
OutputDir string
|
||||
ModName string
|
||||
PackageName string
|
||||
ProgramVersion string
|
||||
ProgramDesc string
|
||||
OutputDir string
|
||||
}
|
||||
|
||||
// RenderTemplates renders all .tmpl files from the templates/ directory into outputDir
|
||||
func RenderTemplates(data ProgramData, outputDir string) error {
|
||||
return filepath.Walk("templates", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() || !strings.HasSuffix(info.Name(), ".tmpl") {
|
||||
return nil
|
||||
}
|
||||
func RenderTemplates(data ProgramData, outputDir string, verbose bool) error {
|
||||
var renderedFiles int
|
||||
|
||||
// Create relative output path (preserving subdirs)
|
||||
relPath, err := filepath.Rel("templates", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl"))
|
||||
err := filepath.Walk("templates", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() || !strings.HasSuffix(info.Name(), ".tmpl") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ensure parent directories exist
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
relPath, err := filepath.Rel("templates", path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl"))
|
||||
|
||||
// Parse and execute template
|
||||
tmpl, err := template.ParseFiles(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
logString := fmt.Sprintf("Rendering %s → %s\n", path, outputPath)
|
||||
logger.Log.Info(logString)
|
||||
return tmpl.Execute(outFile, data)
|
||||
})
|
||||
tmpl, err := template.ParseFiles(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
if verbose {
|
||||
logger.Log.Infof("Rendering %s → %s", path, outputPath)
|
||||
}
|
||||
|
||||
renderedFiles++
|
||||
return tmpl.Execute(outFile, data)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !verbose && renderedFiles > 0 {
|
||||
logger.Log.Info("Rendering templates")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue