genconfig.go 930 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. func (g *defaultGenerator) 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 util.FileExists(fileName) {
  25. return nil
  26. }
  27. text, err := util.LoadTemplate(category, configTemplateFileFile, configTemplate)
  28. if err != nil {
  29. return err
  30. }
  31. return ioutil.WriteFile(fileName, []byte(text), os.ModePerm)
  32. }