update goctl api (#1052)

* update goctl api

* add LoadTemplate

* update new api template

* update
master
Amor 3 years ago committed by GitHub
parent f34d81ca2c
commit 41c980f00c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -59,7 +59,18 @@ func CreateServiceCommand(c *cli.Context) error {
} }
defer fp.Close() defer fp.Close()
t := template.Must(template.New("template").Parse(apiTemplate))
home := c.String("home")
if len(home) > 0 {
util.RegisterGoctlHome(home)
}
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
if err != nil {
return err
}
t := template.Must(template.New("template").Parse(text))
if err := t.Execute(fp, map[string]string{ if err := t.Execute(fp, map[string]string{
"name": dirName, "name": dirName,
"handler": strings.Title(dirName), "handler": strings.Title(dirName),

@ -0,0 +1,51 @@
package new
import (
"fmt"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)
const (
category = "newapi"
apiTemplateFile = "newtemplate.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)
}

@ -62,6 +62,12 @@ var commands = []cli.Command{
Name: "new", Name: "new",
Usage: "fast create api service", Usage: "fast create api service",
Action: new.CreateServiceCommand, Action: new.CreateServiceCommand,
Flags: []cli.Flag{
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
},
},
}, },
{ {
Name: "format", Name: "format",

@ -8,6 +8,7 @@ import (
"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/apigen"
"github.com/tal-tech/go-zero/tools/goctl/api/gogen" "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
apinew "github.com/tal-tech/go-zero/tools/goctl/api/new"
"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"
mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate" mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
@ -48,6 +49,9 @@ func GenTemplates(ctx *cli.Context) error {
func() error { func() error {
return apigen.GenTemplates(ctx) return apigen.GenTemplates(ctx)
}, },
func() error {
return apinew.GenTemplates(ctx)
},
); err != nil { ); err != nil {
return err return err
} }
@ -97,6 +101,9 @@ func CleanTemplates(ctx *cli.Context) error {
func() error { func() error {
return apigen.Clean() return apigen.Clean()
}, },
func() error {
return apinew.Clean()
},
) )
if err != nil { if err != nil {
return err return err
@ -135,6 +142,8 @@ func UpdateTemplates(ctx *cli.Context) (err error) {
return mongogen.Update() return mongogen.Update()
case apigen.Category(): case apigen.Category():
return apigen.Update() return apigen.Update()
case apinew.Category():
return apinew.Update()
default: default:
err = fmt.Errorf("unexpected category: %s", category) err = fmt.Errorf("unexpected category: %s", category)
return return
@ -170,6 +179,8 @@ func RevertTemplates(ctx *cli.Context) (err error) {
return mongogen.RevertTemplate(filename) return mongogen.RevertTemplate(filename)
case apigen.Category(): case apigen.Category():
return apigen.RevertTemplate(filename) return apigen.RevertTemplate(filename)
case apinew.Category():
return apinew.RevertTemplate(filename)
default: default:
err = fmt.Errorf("unexpected category: %s", category) err = fmt.Errorf("unexpected category: %s", category)
return return

Loading…
Cancel
Save