43 lines
696 B
Go
43 lines
696 B
Go
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 ErrTemplateExecutionFailed.Wrap(err, tmpl.Name())
|
|
}
|
|
|
|
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
|
|
}
|