mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
add api template file (#1003)
This commit is contained in:
parent
dec6309c55
commit
d8905b9e9e
@ -52,13 +52,24 @@ func ApiCommand(c *cli.Context) error {
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
home := c.String("home")
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
|
||||
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
||||
baseName = baseName[:len(baseName)-4]
|
||||
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
||||
baseName = baseName[:len(baseName)-3]
|
||||
}
|
||||
t := template.Must(template.New("etcTemplate").Parse(apiTemplate))
|
||||
|
||||
t := template.Must(template.New("etcTemplate").Parse(text))
|
||||
if err := t.Execute(fp, map[string]string{
|
||||
"gitUser": getGitName(),
|
||||
"gitEmail": getGitEmail(),
|
||||
|
51
tools/goctl/api/apigen/template.go
Normal file
51
tools/goctl/api/apigen/template.go
Normal file
@ -0,0 +1,51 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
category = "api"
|
||||
apiTemplateFile = "template.tpl"
|
||||
)
|
||||
|
||||
var templates = map[string]string{
|
||||
apiTemplateFile: apiTemplate,
|
||||
}
|
||||
|
||||
// Category returns the category of the api files.
|
||||
func Category() string {
|
||||
return category
|
||||
}
|
||||
|
||||
// Clean cleans the generated deployment files.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates generates api template files.
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
return util.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// RevertTemplate reverts the given template file to the default value.
|
||||
func RevertTemplate(name string) error {
|
||||
content, ok := templates[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s: no such file name", name)
|
||||
}
|
||||
return util.CreateTemplate(category, name, content)
|
||||
}
|
||||
|
||||
// Update updates the template files to the templates built in current goctl.
|
||||
func Update() error {
|
||||
err := Clean()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
}
|
@ -48,6 +48,10 @@ var commands = []cli.Command{
|
||||
Name: "o",
|
||||
Usage: "the output api file",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "home",
|
||||
Usage: "the goctl home path of the template",
|
||||
},
|
||||
},
|
||||
Action: apigen.ApiCommand,
|
||||
Subcommands: []cli.Command{
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/core/errorx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/apigen"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/docker"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/kube"
|
||||
@ -44,6 +45,9 @@ func GenTemplates(ctx *cli.Context) error {
|
||||
func() error {
|
||||
return mongogen.Templates(ctx)
|
||||
},
|
||||
func() error {
|
||||
return apigen.GenTemplates(ctx)
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -90,6 +94,9 @@ func CleanTemplates(ctx *cli.Context) error {
|
||||
func() error {
|
||||
return mongogen.Clean()
|
||||
},
|
||||
func() error {
|
||||
return apigen.Clean()
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -126,6 +133,8 @@ func UpdateTemplates(ctx *cli.Context) (err error) {
|
||||
return modelgen.Update()
|
||||
case mongogen.Category():
|
||||
return mongogen.Update()
|
||||
case apigen.Category():
|
||||
return apigen.Update()
|
||||
default:
|
||||
err = fmt.Errorf("unexpected category: %s", category)
|
||||
return
|
||||
@ -159,6 +168,8 @@ func RevertTemplates(ctx *cli.Context) (err error) {
|
||||
return modelgen.RevertTemplate(filename)
|
||||
case mongogen.Category():
|
||||
return mongogen.RevertTemplate(filename)
|
||||
case apigen.Category():
|
||||
return apigen.RevertTemplate(filename)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected category: %s", category)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user