genconfig.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package generator
  2. import (
  3. _ "embed"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. conf "github.com/zeromicro/go-zero/tools/goctl/config"
  8. "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  11. )
  12. //go:embed config.tpl
  13. var configTemplate string
  14. // GenConfig generates the configuration structure definition file of the rpc service,
  15. // which contains the zrpc.RpcServerConf configuration item by default.
  16. // You can specify the naming style of the target file name through config.Config. For details,
  17. // see https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/config.go
  18. func (g *Generator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
  19. dir := ctx.GetConfig()
  20. configFilename, err := format.FileNamingFormat(cfg.NamingFormat, "config")
  21. if err != nil {
  22. return err
  23. }
  24. fileName := filepath.Join(dir.Filename, configFilename+".go")
  25. if pathx.FileExists(fileName) {
  26. return nil
  27. }
  28. text, err := pathx.LoadTemplate(category, configTemplateFileFile, configTemplate)
  29. if err != nil {
  30. return err
  31. }
  32. return ioutil.WriteFile(fileName, []byte(text), os.ModePerm)
  33. }