2024-08-28 12:31:17 +02:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
2024-09-11 11:30:48 +02:00
|
|
|
"gitea.codeblob.work/pk/hoster/internals/conf"
|
|
|
|
|
2024-08-28 12:31:17 +02:00
|
|
|
"github.com/gookit/goutil/fsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Execute(name string, outPath string, context any) error {
|
|
|
|
tplPath := filepath.Join(conf.App.MustString(fmt.Sprintf("templates.%s", name)))
|
|
|
|
|
|
|
|
if !fsutil.IsFile(tplPath) {
|
|
|
|
return fmt.Errorf("template file \"%s\" doesn't exist", tplPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile, err := os.Create(outPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
tpl, err := os.ReadFile(tplPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = template.Must(template.New("tpl").Parse(string(tpl))).Execute(outFile, context)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|