2025-05-08 17:33:46 +08:00

70 lines
1.3 KiB
Go

package errcode
import (
"admin/lib/gofmt"
"fmt"
"log"
"os"
"path/filepath"
"text/template"
)
type ErrCode struct {
Name string
Value int
Comment string
}
type ErrCodeList struct {
List []*ErrCode
}
var tpl = `
// Code generated by generator. DO NOT EDIT.
package errcode
import (
"fmt"
)
func GetErrCodeContent(code int) string {
switch code {
{{- range .List }}
case {{ .Name }}:
return "{{ .Comment -}}"
{{- end }}
default:
return fmt.Sprintf("错误码%v没有找到文本内容", code)
}
}
`
func renderFile(data any, templateContent, filePath string) error {
dir := filepath.Dir(filePath)
if dir != "" {
_ = os.MkdirAll(dir, os.ModePerm)
}
tmpl, err := template.New("client").Parse(templateContent)
if err != nil {
return fmt.Errorf("create template for file:%v error:%v", filePath, err)
}
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return fmt.Errorf("open file %v error:%v", filePath, err)
}
defer file.Close()
err = tmpl.Execute(file, data)
if err != nil {
return fmt.Errorf("execute template for file:%v error:%v", filePath, err)
}
log.Printf("write content file [%v] ok.", filePath)
gofmt.Go(filePath)
return nil
}
func must(err error) {
if err != nil {
panic(err)
}
}