cfgen/handle_template.go

43 lines
650 B
Go
Raw Normal View History

2022-09-18 19:28:34 +02:00
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"
)
func HandleTemplate(tmpl *template.Template) error {
b := &bytes.Buffer{}
if err := tmpl.Execute(b, ConfigFile.Globals); err != nil {
return err
}
if len(strings.TrimSpace(b.String())) == 0 {
return nil
}
tmplPath := filepath.Join(ConfigFile.TargetDir, tmpl.Name())
if err := os.MkdirAll(filepath.Dir(tmplPath), 0755); err != nil {
return err
}
file, err := os.Create(tmplPath)
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(file, b); err != nil {
return err
}
fmt.Println(tmplPath)
return nil
}