genconfig.go 1.2 KB

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