/
prototmpl.go
36 lines (30 loc) · 869 Bytes
/
prototmpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package generator
import (
_ "embed"
"path/filepath"
"strings"
"github.com/zhangbao138208/goctls/util"
"github.com/zhangbao138208/goctls/util/pathx"
"github.com/zhangbao138208/goctls/util/stringx"
)
//go:embed rpc.tpl
var rpcTemplateText string
// ProtoTmpl returns a sample of a proto file
func ProtoTmpl(out string) error {
protoFilename := filepath.Base(out)
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
if err != nil {
return err
}
dir := filepath.Dir(out)
err = pathx.MkdirIfNotExist(dir)
if err != nil {
return err
}
err = util.With("t").Parse(text).SaveTo(map[string]string{
"package": strings.Replace(serviceName.Lower(), "_", "", -1),
"serviceName": serviceName.ToCamel(),
}, out, false)
return err
}