add api template file (#1003)

master
neosu 3 years ago committed by GitHub
parent dec6309c55
commit d8905b9e9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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(),

@ -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…
Cancel
Save