go-zero/tools/goctl/api/gogen/genconfig.go

49 lines
810 B
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gogen
import (
"bytes"
"text/template"
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
)
const (
configFile = "config.go"
configTemplate = `package config
import (
2020-07-31 11:14:48 +08:00
"zero/rest"
2020-07-29 17:11:41 +08:00
{{.authImport}}
)
type Config struct {
2020-07-31 11:45:16 +08:00
rest.RestConf
2020-07-29 17:11:41 +08:00
}
`
)
func genConfig(dir string, api *spec.ApiSpec) error {
fp, created, err := util.MaybeCreateFile(dir, configDir, configFile)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
var authImportStr = ""
t := template.Must(template.New("configTemplate").Parse(configTemplate))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, map[string]string{
"authImport": authImportStr,
})
if err != nil {
return nil
}
formatCode := formatCode(buffer.String())
_, err = fp.WriteString(formatCode)
return err
}