feat: 重构 config 和 configexport 包

- 配置加载包 config 更名为 configuration
- 配置导出包 configexport 更名为 pce
- 重构
config 包加载方式,采用加载器的方式,并且支持多加载器
- 重构 configexport
包,支持通过实现模板的方式导出不同格式的数据文件及结构文件
This commit is contained in:
kercylan98
2023-07-17 13:28:17 +08:00
parent 8e2b4ebc89
commit 7e7a504421
36 changed files with 1368 additions and 1580 deletions
+53
View File
@@ -0,0 +1,53 @@
package pce
import (
"github.com/kercylan98/minotaur/utils/file"
"os/exec"
"path/filepath"
)
// NewExporter 创建导出器
func NewExporter(filePath string) *Exporter {
return &Exporter{
filePath: filePath,
}
}
// Exporter 导出器
type Exporter struct {
filePath string
}
// ExportStruct 导出结构
func (slf *Exporter) ExportStruct(tmpl Tmpl, tmplStruct ...*TmplStruct) error {
filePath, err := filepath.Abs(slf.filePath)
if err != nil {
return err
}
raw, err := tmpl.Render(tmplStruct)
if err != nil {
return err
}
if err = file.WriterFile(filePath, []byte(raw)); err != nil {
return err
}
cmd := exec.Command("gofmt", "-w", filePath)
return cmd.Run()
}
// ExportData 导出数据
func (slf *Exporter) ExportData(tmpl DataTmpl, data map[any]any) error {
filePath, err := filepath.Abs(slf.filePath)
if err != nil {
return err
}
raw, err := tmpl.Render(data)
if err != nil {
return err
}
if err = file.WriterFile(filePath, []byte(raw)); err != nil {
return err
}
return nil
}