genetc.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package generator
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  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"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  11. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  12. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  13. )
  14. //go:embed etc.tpl
  15. var etcTemplate string
  16. // GenEtc generates the yaml configuration file of the rpc service,
  17. // including host, port monitoring configuration items and etcd configuration
  18. func (g *Generator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
  19. dir := ctx.GetEtc()
  20. etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
  21. if err != nil {
  22. return err
  23. }
  24. fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
  25. text, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
  26. if err != nil {
  27. return err
  28. }
  29. return util.With("etc").Parse(text).SaveTo(map[string]any{
  30. "serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
  31. }, fileName, false)
  32. }