diff --git a/tools/goctl/api/apigen/gen.go b/tools/goctl/api/apigen/gen.go index 35c4218d..3e669330 100644 --- a/tools/goctl/api/apigen/gen.go +++ b/tools/goctl/api/apigen/gen.go @@ -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(), diff --git a/tools/goctl/api/apigen/template.go b/tools/goctl/api/apigen/template.go new file mode 100644 index 00000000..e2c88685 --- /dev/null +++ b/tools/goctl/api/apigen/template.go @@ -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) +} diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 0ffda9a3..5b0e92f0 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -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{ diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go index ce6c4e97..ba250701 100644 --- a/tools/goctl/tpl/templates.go +++ b/tools/goctl/tpl/templates.go @@ -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