You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/gogen/genconfig.go

49 lines
808 B
Go

4 years ago
package gogen
import (
"bytes"
"text/template"
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
)
const (
configFile = "config.go"
configTemplate = `package config
import (
"zero/rest"
4 years ago
{{.authImport}}
)
type Config struct {
rest.RtConf
4 years ago
}
`
)
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
}