Forráskód Böngészése

update goctl api (#1052)

* update goctl api

* add LoadTemplate

* update new api template

* update
Amor 3 éve
szülő
commit
41c980f00c

+ 12 - 1
tools/goctl/api/new/newservice.go

@@ -59,7 +59,18 @@ func CreateServiceCommand(c *cli.Context) error {
 	}
 
 	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{
 		"name":    dirName,
 		"handler": strings.Title(dirName),

+ 51 - 0
tools/goctl/api/new/template.go

@@ -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)
+}

+ 6 - 0
tools/goctl/goctl.go

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

+ 11 - 0
tools/goctl/tpl/templates.go

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