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

47 lines
873 B
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gogen
import (
"bytes"
2020-08-08 15:16:59 +08:00
"fmt"
2020-07-29 17:11:41 +08:00
"text/template"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
2020-07-29 17:11:41 +08:00
)
const (
configFile = "config.go"
configTemplate = `package config
2020-08-27 14:40:05 +08:00
import {{.authImport}}
2020-07-29 17:11:41 +08:00
type Config struct {
2020-07-31 11:45:16 +08:00
rest.RestConf
2020-07-29 17:11:41 +08:00
}
`
)
2020-08-08 15:16:59 +08:00
func genConfig(dir string) error {
2020-07-29 17:11:41 +08:00
fp, created, err := util.MaybeCreateFile(dir, configDir, configFile)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
2020-08-08 15:16:59 +08:00
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
2020-07-29 17:11:41 +08:00
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
}