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
28
cmd/root.go
28
cmd/root.go
|
|
@ -16,6 +16,7 @@ var (
|
||||||
programDesc string
|
programDesc string
|
||||||
outputDir string
|
outputDir string
|
||||||
logLevel string
|
logLevel string
|
||||||
|
verbose bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd renders templates using CLI flags
|
// rootCmd renders templates using CLI flags
|
||||||
|
|
@ -23,19 +24,23 @@ var rootCmd = &cobra.Command{
|
||||||
Use: "bubblewand",
|
Use: "bubblewand",
|
||||||
Short: "A tool to generate a go project template for building a terminal application with bubbletea + cobra + viper + log",
|
Short: "A tool to generate a go project template for building a terminal application with bubbletea + cobra + viper + log",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Fill ProgramData from CLI input
|
// Initialize logging with log level and flags
|
||||||
data := render.ProgramData{
|
initLogging()
|
||||||
ModName: modName,
|
|
||||||
PackageName: packageName,
|
|
||||||
ProgramVersion: programVersion,
|
|
||||||
ProgramDesc: programDesc,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render templates to the specified output directory
|
// Fill ProgramData from CLI input
|
||||||
if err := render.RenderTemplates(data, outputDir); err != nil {
|
data := render.ProgramData{
|
||||||
logger.Log.Fatalf("rendering failed: %v",err)
|
ModName: modName,
|
||||||
}
|
PackageName: packageName,
|
||||||
|
ProgramVersion: programVersion,
|
||||||
|
ProgramDesc: programDesc,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render templates to the specified output directory
|
||||||
|
if err := render.RenderTemplates(data, outputDir, verbose); err != nil {
|
||||||
|
logger.Log.Fatalf("rendering failed: %v", err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -48,6 +53,7 @@ func init() {
|
||||||
rootCmd.Flags().StringVar(&programDesc, "program-desc", "", "Program description")
|
rootCmd.Flags().StringVar(&programDesc, "program-desc", "", "Program description")
|
||||||
rootCmd.Flags().StringVarP(&outputDir, "output", "o", "output", "Output directory for rendered files")
|
rootCmd.Flags().StringVarP(&outputDir, "output", "o", "output", "Output directory for rendered files")
|
||||||
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")
|
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
|
||||||
// Mark required
|
// Mark required
|
||||||
rootCmd.MarkFlagRequired("mod-name")
|
rootCmd.MarkFlagRequired("mod-name")
|
||||||
rootCmd.MarkFlagRequired("package-name")
|
rootCmd.MarkFlagRequired("package-name")
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ var tuiCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render templates with user input
|
// Render templates with user input
|
||||||
if err := render.RenderTemplates(data, data.OutputDir); err != nil {
|
if err := render.RenderTemplates(data, data.OutputDir, verbose); err != nil {
|
||||||
logger.Log.Fatalf("rendering failed: %v", err)
|
logger.Log.Fatalf("rendering failed: %v", err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,60 +1,71 @@
|
||||||
package render
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os"
|
||||||
"os"
|
"path/filepath"
|
||||||
"path/filepath"
|
"strings"
|
||||||
"strings"
|
"text/template"
|
||||||
"text/template"
|
"specCon18/bubblewand/internal/logger"
|
||||||
"specCon18/bubblewand/internal/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProgramData holds user-supplied template values
|
// ProgramData holds user-supplied template values
|
||||||
type ProgramData struct {
|
type ProgramData struct {
|
||||||
ModName string
|
ModName string
|
||||||
PackageName string
|
PackageName string
|
||||||
ProgramVersion string
|
ProgramVersion string
|
||||||
ProgramDesc string
|
ProgramDesc string
|
||||||
OutputDir string
|
OutputDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderTemplates renders all .tmpl files from the templates/ directory into outputDir
|
// RenderTemplates renders all .tmpl files from the templates/ directory into outputDir
|
||||||
func RenderTemplates(data ProgramData, outputDir string) error {
|
func RenderTemplates(data ProgramData, outputDir string, verbose bool) error {
|
||||||
return filepath.Walk("templates", func(path string, info os.FileInfo, err error) error {
|
var renderedFiles int
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if info.IsDir() || !strings.HasSuffix(info.Name(), ".tmpl") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create relative output path (preserving subdirs)
|
err := filepath.Walk("templates", func(path string, info os.FileInfo, err error) error {
|
||||||
relPath, err := filepath.Rel("templates", path)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
if info.IsDir() || !strings.HasSuffix(info.Name(), ".tmpl") {
|
||||||
outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl"))
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure parent directories exist
|
relPath, err := filepath.Rel("templates", path)
|
||||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
outputPath := filepath.Join(outputDir, strings.TrimSuffix(relPath, ".tmpl"))
|
||||||
|
|
||||||
// Parse and execute template
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
|
||||||
tmpl, err := template.ParseFiles(path)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
outFile, err := os.Create(outputPath)
|
tmpl, err := template.ParseFiles(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer outFile.Close()
|
|
||||||
|
outFile, err := os.Create(outputPath)
|
||||||
logString := fmt.Sprintf("Rendering %s → %s\n", path, outputPath)
|
if err != nil {
|
||||||
logger.Log.Info(logString)
|
return err
|
||||||
return tmpl.Execute(outFile, data)
|
}
|
||||||
})
|
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