support api templates
This commit is contained in:
79
tools/goctl/templatex/files.go
Normal file
79
tools/goctl/templatex/files.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package templatex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const goctlDir = ".goctl"
|
||||
|
||||
func InitTemplates(category string, templates map[string]string) error {
|
||||
dir, err := getTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := util.MkdirIfNotExist(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range templates {
|
||||
if err := createTemplate(filepath.Join(dir, k), v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir),
|
||||
aurora.Red("edit on your risk!"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadTemplate(category, file, builtin string) (string, error) {
|
||||
dir, err := getTemplateDir(category)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
file = filepath.Join(dir, file)
|
||||
if !util.FileExists(file) {
|
||||
return builtin, nil
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func createTemplate(file, content string) error {
|
||||
if util.FileExists(file) {
|
||||
println(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(content)
|
||||
return err
|
||||
}
|
||||
|
||||
func getTemplateDir(category string) (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(home, goctlDir, category), nil
|
||||
}
|
||||
11
tools/goctl/templatex/head.go
Normal file
11
tools/goctl/templatex/head.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package templatex
|
||||
|
||||
var headTemplate = `// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: {{.source}}`
|
||||
|
||||
func GetHead(source string) string {
|
||||
buffer, _ := With("head").Parse(headTemplate).Execute(map[string]interface{}{
|
||||
"source": source,
|
||||
})
|
||||
return buffer.String()
|
||||
}
|
||||
72
tools/goctl/templatex/templatex.go
Normal file
72
tools/goctl/templatex/templatex.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package templatex
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
goformat "go/format"
|
||||
"io/ioutil"
|
||||
"text/template"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const regularPerm = 0666
|
||||
|
||||
type defaultTemplate struct {
|
||||
name string
|
||||
text string
|
||||
goFmt bool
|
||||
savePath string
|
||||
}
|
||||
|
||||
func With(name string) *defaultTemplate {
|
||||
return &defaultTemplate{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
func (t *defaultTemplate) Parse(text string) *defaultTemplate {
|
||||
t.text = text
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) GoFmt(format bool) *defaultTemplate {
|
||||
t.goFmt = format
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
||||
if util.FileExists(path) && !forceUpdate {
|
||||
return nil
|
||||
}
|
||||
|
||||
output, err := t.Execute(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(path, output.Bytes(), regularPerm)
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) Execute(data interface{}) (*bytes.Buffer, error) {
|
||||
tem, err := template.New(t.name).Parse(t.text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err = tem.Execute(buf, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !t.goFmt {
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
formatOutput, err := goformat.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf.Reset()
|
||||
buf.Write(formatOutput)
|
||||
return buf, nil
|
||||
}
|
||||
Reference in New Issue
Block a user