go-zero/tools/goctl/rpc/generator/genetc.go

41 lines
1.1 KiB
Go
Raw Normal View History

package generator
2020-08-28 21:22:35 +08:00
import (
"fmt"
"path/filepath"
"strings"
2020-08-28 21:22:35 +08:00
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
2020-08-28 21:22:35 +08:00
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
2020-08-28 21:22:35 +08:00
)
const etcTemplate = `Name: {{.serviceName}}.rpc
ListenOn: 127.0.0.1:8080
Etcd:
Hosts:
2020-08-29 20:27:52 +08:00
- 127.0.0.1:2379
2020-08-28 21:22:35 +08:00
Key: {{.serviceName}}.rpc
`
func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetEtc()
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {
return err
}
fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
2020-08-28 21:22:35 +08:00
text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
if err != nil {
return err
}
return util.With("etc").Parse(text).SaveTo(map[string]interface{}{
"serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
2020-08-28 21:22:35 +08:00
}, fileName, false)
}