genconfig.go 1.2 KB

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