genetc.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  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. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  11. )
  12. const etcTemplate = `Name: {{.serviceName}}.rpc
  13. ListenOn: 127.0.0.1:8080
  14. Etcd:
  15. Hosts:
  16. - 127.0.0.1:2379
  17. Key: {{.serviceName}}.rpc
  18. `
  19. func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
  20. dir := ctx.GetEtc()
  21. etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
  22. if err != nil {
  23. return err
  24. }
  25. fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
  26. text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
  27. if err != nil {
  28. return err
  29. }
  30. return util.With("etc").Parse(text).SaveTo(map[string]interface{}{
  31. "serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
  32. }, fileName, false)
  33. }