go-zero/tools/goctl/api/gogen/template.go

70 lines
1.9 KiB
Go
Raw Normal View History

2020-10-15 16:36:49 +08:00
package gogen
import (
"fmt"
2020-10-15 16:36:49 +08:00
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
2020-10-15 16:36:49 +08:00
)
const (
category = "api"
configTemplateFile = "config.tpl"
contextTemplateFile = "context.tpl"
etcTemplateFile = "etc.tpl"
handlerTemplateFile = "handler.tpl"
logicTemplateFile = "logic.tpl"
mainTemplateFile = "main.tpl"
middlewareImplementCodeFile = "middleware.tpl"
routesTemplateFile = "routes.tpl"
routesAdditionTemplateFile = "route-addition.tpl"
typesTemplateFile = "types.tpl"
2020-10-15 16:36:49 +08:00
)
var templates = map[string]string{
configTemplateFile: configTemplate,
contextTemplateFile: contextTemplate,
etcTemplateFile: etcTemplate,
handlerTemplateFile: handlerTemplate,
logicTemplateFile: logicTemplate,
mainTemplateFile: mainTemplate,
middlewareImplementCodeFile: middlewareImplementCode,
routesTemplateFile: routesTemplate,
routesAdditionTemplateFile: routesAdditionTemplate,
typesTemplateFile: typesTemplate,
2020-10-15 16:36:49 +08:00
}
2021-02-08 21:31:56 +08:00
// Category returns the category of the api files.
func Category() string {
return category
}
// Clean cleans the generated deployment files.
func Clean() error {
return pathx.Clean(category)
2021-02-08 21:31:56 +08:00
}
// GenTemplates generates api template files.
2020-10-15 16:36:49 +08:00
func GenTemplates(_ *cli.Context) error {
return pathx.InitTemplates(category, templates)
2020-10-15 16:36:49 +08:00
}
2021-02-08 21:31:56 +08:00
// 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 pathx.CreateTemplate(category, name, content)
}
2021-02-08 21:31:56 +08:00
// Update updates the template files to the templates built in current goctl.
2020-12-11 18:53:40 +08:00
func Update() error {
err := Clean()
if err != nil {
return err
}
2020-12-11 18:53:40 +08:00
return pathx.InitTemplates(category, templates)
}