🎨配置文件的初始化

This commit is contained in:
coward
2024-03-07 17:32:38 +08:00
parent 28cdfd1240
commit 15cfa17a5e
12 changed files with 228 additions and 33 deletions

53
utils/template.go Normal file
View File

@@ -0,0 +1,53 @@
package utils
import (
"gitee.ltd/lxh/logger/log"
"html/template"
"os"
)
type templateUtils struct{}
func Template() templateUtils {
return templateUtils{}
}
// Parse
// @description: 解析指定模板文件
// @receiver templateUtils
// @param filepath
// @return parseTemplate
// @return err
func (templateUtils) Parse(filepath string) (parseTemplate *template.Template, err error) {
file, err := os.ReadFile(filepath)
if err != nil {
return
}
parseTemplate, err = template.New("wg0.conf").Parse(string(file))
return
}
// Execute
// @description: 序列化数据到文件中
// @receiver templateUtils
// @param fromTemplate
// @param data
// @param filePath
// @return err
func (templateUtils) Execute(fromTemplate *template.Template, data any, filePath string) (err error) {
wg0Conf, err := os.Create(filePath)
if err != nil {
log.Errorf("创建文件[%s]失败: %v", filePath, err.Error())
return
}
defer func() {
if err = wg0Conf.Close(); err != nil {
log.Errorf("关闭文件[%s]失败: %v", filePath, err.Error())
return
}
}()
return fromTemplate.Execute(wg0Conf, data)
}