mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +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()
|
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))
|
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
|
||||||
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
||||||
baseName = baseName[:len(baseName)-4]
|
baseName = baseName[:len(baseName)-4]
|
||||||
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
||||||
baseName = baseName[:len(baseName)-3]
|
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{
|
if err := t.Execute(fp, map[string]string{
|
||||||
"gitUser": getGitName(),
|
"gitUser": getGitName(),
|
||||||
"gitEmail": getGitEmail(),
|
"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",
|
Name: "o",
|
||||||
Usage: "the output api file",
|
Usage: "the output api file",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "home",
|
||||||
|
Usage: "the goctl home path of the template",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: apigen.ApiCommand,
|
Action: apigen.ApiCommand,
|
||||||
Subcommands: []cli.Command{
|
Subcommands: []cli.Command{
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/core/errorx"
|
"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/api/gogen"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/docker"
|
"github.com/tal-tech/go-zero/tools/goctl/docker"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/kube"
|
"github.com/tal-tech/go-zero/tools/goctl/kube"
|
||||||
@ -44,6 +45,9 @@ func GenTemplates(ctx *cli.Context) error {
|
|||||||
func() error {
|
func() error {
|
||||||
return mongogen.Templates(ctx)
|
return mongogen.Templates(ctx)
|
||||||
},
|
},
|
||||||
|
func() error {
|
||||||
|
return apigen.GenTemplates(ctx)
|
||||||
|
},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -90,6 +94,9 @@ func CleanTemplates(ctx *cli.Context) error {
|
|||||||
func() error {
|
func() error {
|
||||||
return mongogen.Clean()
|
return mongogen.Clean()
|
||||||
},
|
},
|
||||||
|
func() error {
|
||||||
|
return apigen.Clean()
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -126,6 +133,8 @@ func UpdateTemplates(ctx *cli.Context) (err error) {
|
|||||||
return modelgen.Update()
|
return modelgen.Update()
|
||||||
case mongogen.Category():
|
case mongogen.Category():
|
||||||
return mongogen.Update()
|
return mongogen.Update()
|
||||||
|
case apigen.Category():
|
||||||
|
return apigen.Update()
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unexpected category: %s", category)
|
err = fmt.Errorf("unexpected category: %s", category)
|
||||||
return
|
return
|
||||||
@ -159,6 +168,8 @@ func RevertTemplates(ctx *cli.Context) (err error) {
|
|||||||
return modelgen.RevertTemplate(filename)
|
return modelgen.RevertTemplate(filename)
|
||||||
case mongogen.Category():
|
case mongogen.Category():
|
||||||
return mongogen.RevertTemplate(filename)
|
return mongogen.RevertTemplate(filename)
|
||||||
|
case apigen.Category():
|
||||||
|
return apigen.RevertTemplate(filename)
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unexpected category: %s", category)
|
err = fmt.Errorf("unexpected category: %s", category)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user