Compare commits
3 Commits
v1.2.5
...
tools/goct
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
304fb182bb | ||
|
|
e267d94ee1 | ||
|
|
89ce5e492b |
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ func ApiCommand(c *cli.Context) error {
|
|||||||
return errors.New("missing -o")
|
return errors.New("missing -o")
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err := util.CreateIfNotExist(apiFile)
|
fp, err := pathx.CreateIfNotExist(apiFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -62,15 +63,15 @@ func ApiCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
|
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
|
baseName := pathx.FileNameWithoutExt(filepath.Base(apiFile))
|
||||||
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
||||||
baseName = baseName[:len(baseName)-4]
|
baseName = baseName[:len(baseName)-4]
|
||||||
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package apigen
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,12 +23,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean cleans the generated deployment files.
|
// Clean cleans the generated deployment files.
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates generates api template files.
|
// GenTemplates generates api template files.
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate reverts the given template file to the default value.
|
// RevertTemplate reverts the given template file to the default value.
|
||||||
@@ -37,7 +37,7 @@ func RevertTemplate(name string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the template files to the templates built in current goctl.
|
// Update updates the template files to the templates built in current goctl.
|
||||||
@@ -47,5 +47,5 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ func DocCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !util.FileExists(dir) {
|
if !pathx.FileExists(dir) {
|
||||||
return fmt.Errorf("dir %s not exsit", dir)
|
return fmt.Errorf("dir %s not exsit", dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,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/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ func apiFormat(data string, filename ...string) (string, error) {
|
|||||||
newLineCount++
|
newLineCount++
|
||||||
} else {
|
} else {
|
||||||
if preLine == rightBrace {
|
if preLine == rightBrace {
|
||||||
builder.WriteString(ctlutil.NL)
|
builder.WriteString(pathx.NL)
|
||||||
}
|
}
|
||||||
newLineCount = 0
|
newLineCount = 0
|
||||||
}
|
}
|
||||||
@@ -152,7 +152,7 @@ func apiFormat(data string, filename ...string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
util.WriteIndent(&builder, tapCount)
|
util.WriteIndent(&builder, tapCount)
|
||||||
builder.WriteString(line + ctlutil.NL)
|
builder.WriteString(line + pathx.NL)
|
||||||
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
|
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
|
||||||
tapCount++
|
tapCount++
|
||||||
}
|
}
|
||||||
@@ -168,10 +168,10 @@ func formatGoTypeDef(line string, scanner *bufio.Scanner, builder *strings.Build
|
|||||||
if strings.HasPrefix(noCommentLine, "type") && (strings.HasSuffix(noCommentLine, leftParenthesis) ||
|
if strings.HasPrefix(noCommentLine, "type") && (strings.HasSuffix(noCommentLine, leftParenthesis) ||
|
||||||
strings.HasSuffix(noCommentLine, leftBrace)) {
|
strings.HasSuffix(noCommentLine, leftBrace)) {
|
||||||
var typeBuilder strings.Builder
|
var typeBuilder strings.Builder
|
||||||
typeBuilder.WriteString(mayInsertStructKeyword(line, &tokenCount) + ctlutil.NL)
|
typeBuilder.WriteString(mayInsertStructKeyword(line, &tokenCount) + pathx.NL)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
noCommentLine := util.RemoveComment(scanner.Text())
|
noCommentLine := util.RemoveComment(scanner.Text())
|
||||||
typeBuilder.WriteString(mayInsertStructKeyword(scanner.Text(), &tokenCount) + ctlutil.NL)
|
typeBuilder.WriteString(mayInsertStructKeyword(scanner.Text(), &tokenCount) + pathx.NL)
|
||||||
if noCommentLine == rightBrace || noCommentLine == rightParenthesis {
|
if noCommentLine == rightBrace || noCommentLine == rightParenthesis {
|
||||||
tokenCount--
|
tokenCount--
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ func GoCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
if len(apiFile) == 0 {
|
if len(apiFile) == 0 {
|
||||||
return errors.New("missing -api")
|
return errors.New("missing -api")
|
||||||
@@ -64,7 +65,7 @@ func DoGenProject(apiFile, dir, style string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logx.Must(util.MkdirIfNotExist(dir))
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
||||||
rootPkg, err := getParentPackage(dir)
|
rootPkg, err := getParentPackage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
|
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,10 +123,10 @@ func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) err
|
|||||||
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
|
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
|
||||||
var imports []string
|
var imports []string
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"",
|
imports = append(imports, fmt.Sprintf("\"%s\"",
|
||||||
util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
|
pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
|
||||||
if len(route.RequestTypeName()) > 0 {
|
if len(route.RequestTypeName()) > 0 {
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
|
||||||
}
|
}
|
||||||
|
|
||||||
currentVersion := version.GetGoctlVersion()
|
currentVersion := version.GetGoctlVersion()
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -113,9 +113,9 @@ func getLogicFolderPath(group spec.Group, route spec.Route) string {
|
|||||||
func genLogicImports(route spec.Route, parentPkg string) string {
|
func genLogicImports(route spec.Route, parentPkg string) string {
|
||||||
var imports []string
|
var imports []string
|
||||||
imports = append(imports, `"context"`+"\n")
|
imports = append(imports, `"context"`+"\n")
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
|
||||||
if shallImportTypesPackage(route) {
|
if shallImportTypesPackage(route) {
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
|
||||||
}
|
}
|
||||||
imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceURL))
|
imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceURL))
|
||||||
return strings.Join(imports, "\n\t")
|
return strings.Join(imports, "\n\t")
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -68,9 +68,9 @@ func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
|||||||
|
|
||||||
func genMainImports(parentPkg string) string {
|
func genMainImports(parentPkg string) string {
|
||||||
var imports []string
|
var imports []string
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, configDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, configDir)))
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, handlerDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, handlerDir)))
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, contextDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, contextDir)))
|
||||||
imports = append(imports, fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceURL))
|
imports = append(imports, fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceURL))
|
||||||
imports = append(imports, fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL))
|
imports = append(imports, fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL))
|
||||||
return strings.Join(imports, "\n\t")
|
return strings.Join(imports, "\n\t")
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ rest.WithPrefix("%s"),`, g.prefix)
|
|||||||
|
|
||||||
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
||||||
importSet := collection.NewSet()
|
importSet := collection.NewSet()
|
||||||
importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
|
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
|
||||||
for _, group := range api.Service.Groups {
|
for _, group := range api.Service.Groups {
|
||||||
for _, route := range group.Routes {
|
for _, route := range group.Routes {
|
||||||
folder := route.GetAnnotation(groupProperty)
|
folder := route.GetAnnotation(groupProperty)
|
||||||
@@ -161,7 +161,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), util.JoinPackages(parentPkg, handlerDir, folder)))
|
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), pathx.JoinPackages(parentPkg, handlerDir, folder)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imports := importSet.KeysStr()
|
imports := importSet.KeysStr()
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,9 +50,9 @@ func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpe
|
|||||||
fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
||||||
}
|
}
|
||||||
|
|
||||||
configImport := "\"" + ctlutil.JoinPackages(rootPkg, configDir) + "\""
|
configImport := "\"" + pathx.JoinPackages(rootPkg, configDir) + "\""
|
||||||
if len(middlewareStr) > 0 {
|
if len(middlewareStr) > 0 {
|
||||||
configImport += "\n\t\"" + ctlutil.JoinPackages(rootPkg, middlewareDir) + "\""
|
configImport += "\n\t\"" + pathx.JoinPackages(rootPkg, middlewareDir) + "\""
|
||||||
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
|
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package gogen
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,12 +33,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean cleans the generated deployment files.
|
// Clean cleans the generated deployment files.
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates generates api template files.
|
// GenTemplates generates api template files.
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate reverts the given template file to the default value.
|
// RevertTemplate reverts the given template file to the default value.
|
||||||
@@ -47,7 +47,7 @@ func RevertTemplate(name string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the template files to the templates built in current goctl.
|
// Update updates the template files to the templates built in current goctl.
|
||||||
@@ -57,5 +57,5 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenTemplates(t *testing.T) {
|
func TestGenTemplates(t *testing.T) {
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
file := filepath.Join(dir, "main.tpl")
|
file := filepath.Join(dir, "main.tpl")
|
||||||
data, err := ioutil.ReadFile(file)
|
data, err := ioutil.ReadFile(file)
|
||||||
@@ -22,10 +22,10 @@ func TestGenTemplates(t *testing.T) {
|
|||||||
|
|
||||||
func TestRevertTemplate(t *testing.T) {
|
func TestRevertTemplate(t *testing.T) {
|
||||||
name := "main.tpl"
|
name := "main.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -33,7 +33,7 @@ func TestRevertTemplate(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
modifyData := string(data) + "modify"
|
modifyData := string(data) + "modify"
|
||||||
err = util.CreateTemplate(category, name, modifyData)
|
err = pathx.CreateTemplate(category, name, modifyData)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
data, err = ioutil.ReadFile(file)
|
data, err = ioutil.ReadFile(file)
|
||||||
@@ -50,12 +50,12 @@ func TestRevertTemplate(t *testing.T) {
|
|||||||
|
|
||||||
func TestClean(t *testing.T) {
|
func TestClean(t *testing.T) {
|
||||||
name := "main.tpl"
|
name := "main.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
assert.Nil(t, Clean())
|
assert.Nil(t, Clean())
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -65,10 +65,10 @@ func TestClean(t *testing.T) {
|
|||||||
|
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
name := "main.tpl"
|
name := "main.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -76,7 +76,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
modifyData := string(data) + "modify"
|
modifyData := string(data) + "modify"
|
||||||
err = util.CreateTemplate(category, name, modifyData)
|
err = pathx.CreateTemplate(category, name, modifyData)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
data, err = ioutil.ReadFile(file)
|
data, err = ioutil.ReadFile(file)
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileGenConfig struct {
|
type fileGenConfig struct {
|
||||||
@@ -41,7 +41,7 @@ func genFile(c fileGenConfig) error {
|
|||||||
if len(c.category) == 0 || len(c.templateFile) == 0 {
|
if len(c.category) == 0 || len(c.templateFile) == 0 {
|
||||||
text = c.builtinTemplate
|
text = c.builtinTemplate
|
||||||
} else {
|
} else {
|
||||||
text, err = ctlutil.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
|
text, err = pathx.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ func getParentPackage(dir string) (string, error) {
|
|||||||
// fix https://github.com/zeromicro/go-zero/issues/1058
|
// fix https://github.com/zeromicro/go-zero/issues/1058
|
||||||
wd := projectCtx.WorkDir
|
wd := projectCtx.WorkDir
|
||||||
d := projectCtx.Dir
|
d := projectCtx.Dir
|
||||||
same, err := ctlutil.SameFile(wd, d)
|
same, err := pathx.SameFile(wd, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ func JavaCommand(c *cli.Context) error {
|
|||||||
|
|
||||||
api.Service = api.Service.JoinPrefix()
|
api.Service = api.Service.JoinPrefix()
|
||||||
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
packetName := strings.TrimSuffix(api.Service.Name, "-api")
|
||||||
logx.Must(util.MkdirIfNotExist(dir))
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
||||||
logx.Must(genPacket(dir, packetName, api))
|
logx.Must(genPacket(dir, packetName, api))
|
||||||
logx.Must(genComponents(dir, packetName, api))
|
logx.Must(genComponents(dir, packetName, api))
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -110,7 +111,7 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type
|
|||||||
|
|
||||||
modelFile := util.Title(ty.Name()) + ".java"
|
modelFile := util.Title(ty.Name()) + ".java"
|
||||||
filename := path.Join(dir, modelDir, modelFile)
|
filename := path.Join(dir, modelDir, modelFile)
|
||||||
if err := util.RemoveOrQuit(filename); err != nil {
|
if err := pathx.RemoveOrQuit(filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +271,7 @@ func (c *componentsContext) buildConstructor() (string, string, error) {
|
|||||||
writeIndent(&constructorSetter, 2)
|
writeIndent(&constructorSetter, 2)
|
||||||
constructorSetter.WriteString(fmt.Sprintf("this.%s = %s;", pn, util.Untitle(member.Name)))
|
constructorSetter.WriteString(fmt.Sprintf("this.%s = %s;", pn, util.Untitle(member.Name)))
|
||||||
if index != len(c.members)-1 {
|
if index != len(c.members)-1 {
|
||||||
constructorSetter.WriteString(util.NL)
|
constructorSetter.WriteString(pathx.NL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return params.String(), constructorSetter.String(), nil
|
return params.String(), constructorSetter.String(), nil
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
|
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
|
||||||
if len(member.Comment) > 0 {
|
if len(member.Comment) > 0 {
|
||||||
writeIndent(writer, indent)
|
writeIndent(writer, indent)
|
||||||
fmt.Fprint(writer, member.Comment+util.NL)
|
fmt.Fprint(writer, member.Comment+pathx.NL)
|
||||||
}
|
}
|
||||||
writeIndent(writer, indent)
|
writeIndent(writer, indent)
|
||||||
ty, err := specTypeToJava(member.Type)
|
ty, err := specTypeToJava(member.Type)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
|
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
|
||||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ func CreateServiceCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = util.MkdirIfNotExist(abs)
|
err = pathx.MkdirIfNotExist(abs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -74,10 +75,10 @@ func CreateServiceCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
|
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package new
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,12 +23,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean cleans the generated deployment files.
|
// Clean cleans the generated deployment files.
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates generates api template files.
|
// GenTemplates generates api template files.
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate reverts the given template file to the default value.
|
// RevertTemplate reverts the given template file to the default value.
|
||||||
@@ -37,7 +37,7 @@ func RevertTemplate(name string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the template files to the templates built in current goctl.
|
// Update updates the template files to the templates built in current goctl.
|
||||||
@@ -47,5 +47,5 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -119,7 +119,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("nestedImport", func(t *testing.T) {
|
t.Run("nestedImport", func(t *testing.T) {
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
|
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -149,7 +149,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ambiguousSyntax", func(t *testing.T) {
|
t.Run("ambiguousSyntax", func(t *testing.T) {
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
|
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -163,7 +163,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ambiguousSyntax", func(t *testing.T) {
|
t.Run("ambiguousSyntax", func(t *testing.T) {
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
|
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -177,7 +177,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ambiguousService", func(t *testing.T) {
|
t.Run("ambiguousService", func(t *testing.T) {
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm)
|
err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -207,7 +207,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
`)
|
`)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm)
|
err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -236,7 +236,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
`)
|
`)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm)
|
err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -260,7 +260,7 @@ func TestApiParser(t *testing.T) {
|
|||||||
`)
|
`)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
file := filepath.Join(util.MustTempDir(), "foo.api")
|
file := filepath.Join(pathx.MustTempDir(), "foo.api")
|
||||||
err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm)
|
err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ func TsCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
api.Service = api.Service.JoinPrefix()
|
api.Service = api.Service.JoinPrefix()
|
||||||
logx.Must(util.MkdirIfNotExist(dir))
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
||||||
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
||||||
logx.Must(genComponents(dir, api))
|
logx.Must(genComponents(dir, api))
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -30,7 +30,7 @@ func genComponents(dir string, api *spec.ApiSpec) error {
|
|||||||
|
|
||||||
outputFile := apiutil.ComponentName(api) + ".ts"
|
outputFile := apiutil.ComponentName(api) + ".ts"
|
||||||
filename := path.Join(dir, outputFile)
|
filename := path.Join(dir, outputFile)
|
||||||
if err := util.RemoveIfExist(filename); err != nil {
|
if err := pathx.RemoveIfExist(filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,7 +21,7 @@ const (
|
|||||||
|
|
||||||
func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
|
func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
|
||||||
filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
|
filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
|
||||||
if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil {
|
if err := pathx.RemoveIfExist(path.Join(dir, filename)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
|
fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
|
||||||
@@ -46,11 +47,11 @@ func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) e
|
|||||||
|
|
||||||
if len(api.Types) != 0 {
|
if len(api.Types) != 0 {
|
||||||
if len(imports) > 0 {
|
if len(imports) > 0 {
|
||||||
imports += util.NL
|
imports += pathx.NL
|
||||||
}
|
}
|
||||||
outputFile := apiutil.ComponentName(api)
|
outputFile := apiutil.ComponentName(api)
|
||||||
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
|
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
|
||||||
imports += fmt.Sprintf(`%sexport * from "%s"`, util.NL, "./"+outputFile)
|
imports += fmt.Sprintf(`%sexport * from "%s"`, pathx.NL, "./"+outputFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
apis, err := genAPI(api, caller)
|
apis, err := genAPI(api, caller)
|
||||||
|
|||||||
@@ -10,19 +10,19 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaybeCreateFile creates file if not exists
|
// MaybeCreateFile creates file if not exists
|
||||||
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
|
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
|
||||||
logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
|
logx.Must(pathx.MkdirIfNotExist(path.Join(dir, subdir)))
|
||||||
fpath := path.Join(dir, subdir, file)
|
fpath := path.Join(dir, subdir, file)
|
||||||
if util.FileExists(fpath) {
|
if pathx.FileExists(fpath) {
|
||||||
fmt.Printf("%s exists, ignored generation\n", fpath)
|
fmt.Printf("%s exists, ignored generation\n", fpath)
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fp, err = util.CreateIfNotExist(fpath)
|
fp, err = pathx.CreateIfNotExist(fpath)
|
||||||
created = err == nil
|
created = err == nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,14 +58,14 @@ func DockerCommand(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(goFile) == 0 {
|
if len(goFile) == 0 {
|
||||||
return errors.New("-go can't be empty")
|
return errors.New("-go can't be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !util.FileExists(goFile) {
|
if !pathx.FileExists(goFile) {
|
||||||
return fmt.Errorf("file %q not found", goFile)
|
return fmt.Errorf("file %q not found", goFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
projDir, ok := util.FindProjectPath(goFile)
|
projDir, ok := pathx.FindProjectPath(goFile)
|
||||||
if ok {
|
if ok {
|
||||||
fmt.Printf("Hint: run \"docker build ...\" command in dir:\n %s\n", projDir)
|
fmt.Printf("Hint: run \"docker build ...\" command in dir:\n %s\n", projDir)
|
||||||
}
|
}
|
||||||
@@ -129,13 +130,13 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
|
|||||||
projPath = "."
|
projPath = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := util.CreateIfNotExist(dockerfileName)
|
out, err := pathx.CreateIfNotExist(dockerfileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
|
text, err := pathx.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -151,7 +152,7 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
|
|||||||
Chinese: offset == cstOffset,
|
Chinese: offset == cstOffset,
|
||||||
GoRelPath: projPath,
|
GoRelPath: projPath,
|
||||||
GoFile: goFile,
|
GoFile: goFile,
|
||||||
ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)),
|
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
|
||||||
HasPort: port > 0,
|
HasPort: port > 0,
|
||||||
Port: port,
|
Port: port,
|
||||||
Argument: builder.String(),
|
Argument: builder.String(),
|
||||||
@@ -165,9 +166,9 @@ func getFilePath(file string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
|
projPath, ok := pathx.FindGoModPath(filepath.Join(wd, file))
|
||||||
if !ok {
|
if !ok {
|
||||||
projPath, err = util.PathFromGoSrc()
|
projPath, err = pathx.PathFromGoSrc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("no go.mod found, or not in GOPATH")
|
return "", errors.New("no go.mod found, or not in GOPATH")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ CMD ["./{{.ExeFile}}"{{.Argument}}]
|
|||||||
|
|
||||||
// Clean deletes all templates files
|
// Clean deletes all templates files
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates creates docker template files
|
// GenTemplates creates docker template files
|
||||||
@@ -58,7 +58,7 @@ func Category() string {
|
|||||||
|
|
||||||
// RevertTemplate recovers the deleted template files
|
// RevertTemplate recovers the deleted template files
|
||||||
func RevertTemplate(name string) error {
|
func RevertTemplate(name string) error {
|
||||||
return util.CreateTemplate(category, name, dockerTemplate)
|
return pathx.CreateTemplate(category, name, dockerTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update deletes and creates new template files
|
// Update deletes and creates new template files
|
||||||
@@ -72,7 +72,7 @@ func Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initTemplate() error {
|
func initTemplate() error {
|
||||||
return util.InitTemplates(category, map[string]string{
|
return pathx.InitTemplates(category, map[string]string{
|
||||||
dockerTemplateFile: dockerTemplate,
|
dockerTemplateFile: dockerTemplate,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ require (
|
|||||||
github.com/iancoleman/strcase v0.2.0
|
github.com/iancoleman/strcase v0.2.0
|
||||||
github.com/logrusorgru/aurora v2.0.3+incompatible
|
github.com/logrusorgru/aurora v2.0.3+incompatible
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
github.com/tal-tech/go-zero v1.2.5-0.20211230062155-e4ab51857606
|
github.com/tal-tech/go-zero v1.2.5
|
||||||
github.com/urfave/cli v1.22.5
|
github.com/urfave/cli v1.22.5
|
||||||
github.com/zeromicro/antlr v0.0.1
|
github.com/zeromicro/antlr v0.0.1
|
||||||
github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348
|
github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348
|
||||||
|
|||||||
@@ -349,8 +349,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
|
|||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/tal-tech/go-zero v1.2.5-0.20211230062155-e4ab51857606 h1:u6Nm/S8pOqHDdjYs8aFBxDGC1BfjVmJoOus4g2RanZE=
|
github.com/tal-tech/go-zero v1.2.5 h1:DpqTM/OTCThRtJoQPlp6b4/YnbCVkZ3gDT7+nFGUo3k=
|
||||||
github.com/tal-tech/go-zero v1.2.5-0.20211230062155-e4ab51857606/go.mod h1:OM2UangPHFMJ+5boRUaeQ/z6xpVvqprZda/JUWG1D7Y=
|
github.com/tal-tech/go-zero v1.2.5/go.mod h1:OM2UangPHFMJ+5boRUaeQ/z6xpVvqprZda/JUWG1D7Y=
|
||||||
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
|
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
|
||||||
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ func DeploymentCommand(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0 to disable the nodePort type
|
// 0 to disable the nodePort type
|
||||||
@@ -58,12 +59,12 @@ func DeploymentCommand(c *cli.Context) error {
|
|||||||
return errors.New("nodePort should be between 30000 and 32767")
|
return errors.New("nodePort should be between 30000 and 32767")
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
|
text, err := pathx.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := util.CreateIfNotExist(c.String("o"))
|
out, err := pathx.CreateIfNotExist(c.String("o"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -102,12 +103,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean cleans the generated deployment files.
|
// Clean cleans the generated deployment files.
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates generates the deployment template files.
|
// GenTemplates generates the deployment template files.
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, map[string]string{
|
return pathx.InitTemplates(category, map[string]string{
|
||||||
deployTemplateFile: deploymentTemplate,
|
deployTemplateFile: deploymentTemplate,
|
||||||
jobTemplateFile: jobTmeplate,
|
jobTemplateFile: jobTmeplate,
|
||||||
})
|
})
|
||||||
@@ -115,7 +116,7 @@ func GenTemplates(_ *cli.Context) error {
|
|||||||
|
|
||||||
// RevertTemplate reverts the given template file to the default value.
|
// RevertTemplate reverts the given template file to the default value.
|
||||||
func RevertTemplate(name string) error {
|
func RevertTemplate(name string) error {
|
||||||
return util.CreateTemplate(category, name, deploymentTemplate)
|
return pathx.CreateTemplate(category, name, deploymentTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the template files to the templates built in current goctl.
|
// Update updates the template files to the templates built in current goctl.
|
||||||
@@ -125,7 +126,7 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, map[string]string{
|
return pathx.InitTemplates(category, map[string]string{
|
||||||
deployTemplateFile: deploymentTemplate,
|
deployTemplateFile: deploymentTemplate,
|
||||||
jobTemplateFile: jobTmeplate,
|
jobTemplateFile: jobTmeplate,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context defines the model generation data what they needs
|
// Context defines the model generation data what they needs
|
||||||
@@ -39,7 +40,7 @@ func generateModel(ctx *Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Text)
|
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -58,7 +59,7 @@ func generateModel(ctx *Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateError(ctx *Context) error {
|
func generateError(ctx *Context) error {
|
||||||
text, err := util.LoadTemplate(category, errTemplateFile, template.Error)
|
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testTypes = `
|
var testTypes = `
|
||||||
@@ -19,7 +19,7 @@ func TestDo(t *testing.T) {
|
|||||||
cfg, err := config.NewConfig(config.DefaultFormat)
|
cfg, err := config.NewConfig(config.DefaultFormat)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
tempDir := util.MustTempDir()
|
tempDir := pathx.MustTempDir()
|
||||||
typesfile := filepath.Join(tempDir, "types.go")
|
typesfile := filepath.Join(tempDir, "types.go")
|
||||||
err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666)
|
err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,12 +26,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean cleans the mongo templates.
|
// Clean cleans the mongo templates.
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Templates initializes the mongo templates.
|
// Templates initializes the mongo templates.
|
||||||
func Templates(_ *cli.Context) error {
|
func Templates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate reverts the given template.
|
// RevertTemplate reverts the given template.
|
||||||
@@ -41,7 +41,7 @@ func RevertTemplate(name string) error {
|
|||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cleans and updates the templates.
|
// Update cleans and updates the templates.
|
||||||
@@ -51,5 +51,5 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
|
"github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
|
||||||
file "github.com/tal-tech/go-zero/tools/goctl/util"
|
file "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ func Action(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
file.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tp) == 0 {
|
if len(tp) == 0 {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
|
||||||
file "github.com/tal-tech/go-zero/tools/goctl/util"
|
file "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ func MysqlDDL(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
file.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
cfg, err := config.NewConfig(style)
|
cfg, err := config.NewConfig(style)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,7 +77,7 @@ func MySqlDataSource(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
file.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern := strings.TrimSpace(ctx.String(flagTable))
|
pattern := strings.TrimSpace(ctx.String(flagTable))
|
||||||
@@ -105,7 +106,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
file.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(schema) == 0 {
|
if len(schema) == 0 {
|
||||||
|
|||||||
45
tools/goctl/model/sql/command/command_pg_test.go
Normal file
45
tools/goctl/model/sql/command/command_pg_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// generate test table sql
|
||||||
|
/*
|
||||||
|
CREATE TABLE "public"."users" (
|
||||||
|
"id" serial NOT NULL,
|
||||||
|
"account" varchar(20) COLLATE "pg_catalog"."default" NOT NULL,
|
||||||
|
"avatar" text COLLATE "pg_catalog"."default",
|
||||||
|
"nick_name" varchar(60) COLLATE "pg_catalog"."default",
|
||||||
|
"register_time" timestamp(6) NOT NULL,
|
||||||
|
"update_time" timestamp(6),
|
||||||
|
"password" varchar(255) COLLATE "pg_catalog"."default",
|
||||||
|
"email" varchar(100) COLLATE "pg_catalog"."default",
|
||||||
|
"reset_key" varchar(10) COLLATE "pg_catalog"."default",
|
||||||
|
"active" bool NOT NULL DEFAULT true,
|
||||||
|
CONSTRAINT "users_pk" PRIMARY KEY ("id")
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
|
ALTER TABLE "public"."users"
|
||||||
|
OWNER TO "postgres";
|
||||||
|
*/
|
||||||
|
|
||||||
|
func TestFromDatasource(t *testing.T) {
|
||||||
|
err := gen.Clean()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
url := "postgres://postgres:postgres@127.0.0.1:5432/demo?sslmode=disable"
|
||||||
|
|
||||||
|
pattern := "users" // table name
|
||||||
|
|
||||||
|
cfg, err := config.NewConfig("")
|
||||||
|
tempDir := filepath.Join(util.MustTempDir(), "test")
|
||||||
|
err = fromPostgreSqlDataSource(url, pattern, tempDir, "public", cfg, false, false)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -23,12 +23,12 @@ func TestFromDDl(t *testing.T) {
|
|||||||
err := gen.Clean()
|
err := gen.Clean()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = fromDDL("./user.sql", util.MustTempDir(), cfg, true, false, "go_zero")
|
err = fromDDL("./user.sql", pathx.MustTempDir(), cfg, true, false, "go_zero")
|
||||||
assert.Equal(t, errNotMatched, err)
|
assert.Equal(t, errNotMatched, err)
|
||||||
|
|
||||||
// case dir is not exists
|
// case dir is not exists
|
||||||
unknownDir := filepath.Join(util.MustTempDir(), "test", "user.sql")
|
unknownDir := filepath.Join(pathx.MustTempDir(), "test", "user.sql")
|
||||||
err = fromDDL(unknownDir, util.MustTempDir(), cfg, true, false, "go_zero")
|
err = fromDDL(unknownDir, pathx.MustTempDir(), cfg, true, false, "go_zero")
|
||||||
assert.True(t, func() bool {
|
assert.True(t, func() bool {
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case *os.PathError:
|
case *os.PathError:
|
||||||
@@ -39,13 +39,13 @@ func TestFromDDl(t *testing.T) {
|
|||||||
}())
|
}())
|
||||||
|
|
||||||
// case empty src
|
// case empty src
|
||||||
err = fromDDL("", util.MustTempDir(), cfg, true, false, "go_zero")
|
err = fromDDL("", pathx.MustTempDir(), cfg, true, false, "go_zero")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
|
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
tempDir := filepath.Join(util.MustTempDir(), "test")
|
tempDir := filepath.Join(pathx.MustTempDir(), "test")
|
||||||
err = util.MkdirIfNotExist(tempDir)
|
err = pathx.MkdirIfNotExist(tempDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
camel := table.Name.ToCamel()
|
camel := table.Name.ToCamel()
|
||||||
text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete)
|
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
@@ -43,7 +44,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// interface method
|
// interface method
|
||||||
text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
|
text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genFields(fields []*parser.Field) (string, error) {
|
func genFields(fields []*parser.Field) (string, error) {
|
||||||
@@ -29,7 +30,7 @@ func genField(field *parser.Field) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field)
|
text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ package gen
|
|||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) {
|
func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) {
|
||||||
camel := table.Name.ToCamel()
|
camel := table.Name.ToCamel()
|
||||||
text, err := util.LoadTemplate(category, findOneTemplateFile, template.FindOne)
|
text, err := pathx.LoadTemplate(category, findOneTemplateFile, template.FindOne)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
@@ -30,7 +31,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err = util.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
|
text, err = pathx.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ type findOneCode struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, error) {
|
func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, error) {
|
||||||
text, err := util.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
|
text, err := pathx.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -47,7 +48,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
|||||||
list = append(list, output.String())
|
list = append(list, output.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
|
text, err = pathx.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -79,7 +80,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
if withCache {
|
if withCache {
|
||||||
text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
|
text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -96,15 +97,15 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &findOneCode{
|
return &findOneCode{
|
||||||
findOneMethod: strings.Join(list, util.NL),
|
findOneMethod: strings.Join(list, pathx.NL),
|
||||||
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
|
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
|
||||||
cacheExtra: out.String(),
|
cacheExtra: out.String(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &findOneCode{
|
return &findOneCode{
|
||||||
findOneMethod: strings.Join(list, util.NL),
|
findOneMethod: strings.Join(list, pathx.NL),
|
||||||
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
|
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
|
|||||||
|
|
||||||
dir = dirAbs
|
dir = dirAbs
|
||||||
pkg := filepath.Base(dirAbs)
|
pkg := filepath.Base(dirAbs)
|
||||||
err = util.MkdirIfNotExist(dir)
|
err = pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -134,7 +135,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
|||||||
|
|
||||||
g.dir = dirAbs
|
g.dir = dirAbs
|
||||||
g.pkg = filepath.Base(dirAbs)
|
g.pkg = filepath.Base(dirAbs)
|
||||||
err = util.MkdirIfNotExist(dirAbs)
|
err = pathx.MkdirIfNotExist(dirAbs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -148,7 +149,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
|||||||
|
|
||||||
name := util.SafeString(modelFilename) + ".go"
|
name := util.SafeString(modelFilename) + ".go"
|
||||||
filename := filepath.Join(dirAbs, name)
|
filename := filepath.Join(dirAbs, name)
|
||||||
if util.FileExists(filename) {
|
if pathx.FileExists(filename) {
|
||||||
g.Warning("%s already exists, ignored.", name)
|
g.Warning("%s already exists, ignored.", name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -165,7 +166,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
filename := filepath.Join(dirAbs, varFilename+".go")
|
filename := filepath.Join(dirAbs, varFilename+".go")
|
||||||
text, err := util.LoadTemplate(category, errTemplateFile, template.Error)
|
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -261,7 +262,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
|
|
||||||
var list []string
|
var list []string
|
||||||
list = append(list, insertCodeMethod, findOneCodeMethod, ret.findOneInterfaceMethod, updateCodeMethod, deleteCodeMethod)
|
list = append(list, insertCodeMethod, findOneCodeMethod, ret.findOneInterfaceMethod, updateCodeMethod, deleteCodeMethod)
|
||||||
typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), util.NL), withCache)
|
typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), pathx.NL), withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -292,7 +293,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
|
func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
|
||||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Model)
|
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
|
var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
|
||||||
@@ -23,11 +23,11 @@ func TestCacheModel(t *testing.T) {
|
|||||||
logx.Disable()
|
logx.Disable()
|
||||||
_ = Clean()
|
_ = Clean()
|
||||||
|
|
||||||
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
|
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||||
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
dir := filepath.Join(util.MustTempDir(), "./testmodel")
|
dir := filepath.Join(pathx.MustTempDir(), "./testmodel")
|
||||||
cacheDir := filepath.Join(dir, "cache")
|
cacheDir := filepath.Join(dir, "cache")
|
||||||
noCacheDir := filepath.Join(dir, "nocache")
|
noCacheDir := filepath.Join(dir, "nocache")
|
||||||
g, err := NewDefaultGenerator(cacheDir, &config.Config{
|
g, err := NewDefaultGenerator(cacheDir, &config.Config{
|
||||||
@@ -58,7 +58,7 @@ func TestNamingModel(t *testing.T) {
|
|||||||
logx.Disable()
|
logx.Disable()
|
||||||
_ = Clean()
|
_ = Clean()
|
||||||
|
|
||||||
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
|
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||||
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ package gen
|
|||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genImports(withCache, timeImport bool) (string, error) {
|
func genImports(withCache, timeImport bool) (string, error) {
|
||||||
if withCache {
|
if withCache {
|
||||||
text, err := util.LoadTemplate(category, importsTemplateFile, template.Imports)
|
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -22,7 +23,7 @@ func genImports(withCache, timeImport bool) (string, error) {
|
|||||||
return buffer.String(), nil
|
return buffer.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
|
text, err := pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
camel := table.Name.ToCamel()
|
camel := table.Name.ToCamel()
|
||||||
text, err := util.LoadTemplate(category, insertTemplateFile, template.Insert)
|
text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
@@ -67,7 +68,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// interface method
|
// interface method
|
||||||
text, err = util.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
|
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genNew(table Table, withCache, postgreSql bool) (string, error) {
|
func genNew(table Table, withCache, postgreSql bool) (string, error) {
|
||||||
text, err := util.LoadTemplate(category, modelNewTemplateFile, template.New)
|
text, err := pathx.LoadTemplate(category, modelNewTemplateFile, template.New)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package gen
|
|||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genTag(in string) (string, error) {
|
func genTag(in string) (string, error) {
|
||||||
@@ -10,7 +11,7 @@ func genTag(in string) (string, error) {
|
|||||||
return in, nil
|
return in, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, tagTemplateFile, template.Tag)
|
text, err := pathx.LoadTemplate(category, tagTemplateFile, template.Tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,12 +62,12 @@ func Category() string {
|
|||||||
|
|
||||||
// Clean deletes all template files
|
// Clean deletes all template files
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTemplates creates template files if not exists
|
// GenTemplates creates template files if not exists
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate recovers the delete template files
|
// RevertTemplate recovers the delete template files
|
||||||
@@ -77,7 +77,7 @@ func RevertTemplate(name string) error {
|
|||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update provides template clean and init
|
// Update provides template clean and init
|
||||||
@@ -87,5 +87,5 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenTemplates(t *testing.T) {
|
func TestGenTemplates(t *testing.T) {
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
file := filepath.Join(dir, "model-new.tpl")
|
file := filepath.Join(dir, "model-new.tpl")
|
||||||
data, err := ioutil.ReadFile(file)
|
data, err := ioutil.ReadFile(file)
|
||||||
@@ -23,10 +23,10 @@ func TestGenTemplates(t *testing.T) {
|
|||||||
|
|
||||||
func TestRevertTemplate(t *testing.T) {
|
func TestRevertTemplate(t *testing.T) {
|
||||||
name := "model-new.tpl"
|
name := "model-new.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -34,7 +34,7 @@ func TestRevertTemplate(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
modifyData := string(data) + "modify"
|
modifyData := string(data) + "modify"
|
||||||
err = util.CreateTemplate(category, name, modifyData)
|
err = pathx.CreateTemplate(category, name, modifyData)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
data, err = ioutil.ReadFile(file)
|
data, err = ioutil.ReadFile(file)
|
||||||
@@ -51,12 +51,12 @@ func TestRevertTemplate(t *testing.T) {
|
|||||||
|
|
||||||
func TestClean(t *testing.T) {
|
func TestClean(t *testing.T) {
|
||||||
name := "model-new.tpl"
|
name := "model-new.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
assert.Nil(t, Clean())
|
assert.Nil(t, Clean())
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -66,10 +66,10 @@ func TestClean(t *testing.T) {
|
|||||||
|
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
name := "model-new.tpl"
|
name := "model-new.tpl"
|
||||||
err := util.InitTemplates(category, templates)
|
err := pathx.InitTemplates(category, templates)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(category)
|
dir, err := pathx.GetTemplateDir(category)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
file := filepath.Join(dir, name)
|
file := filepath.Join(dir, name)
|
||||||
@@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
modifyData := string(data) + "modify"
|
modifyData := string(data) + "modify"
|
||||||
err = util.CreateTemplate(category, name, modifyData)
|
err = pathx.CreateTemplate(category, name, modifyData)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
data, err = ioutil.ReadFile(file)
|
data, err = ioutil.ReadFile(file)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package gen
|
|||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func genTypes(table Table, methods string, withCache bool) (string, error) {
|
func genTypes(table Table, methods string, withCache bool) (string, error) {
|
||||||
@@ -12,7 +13,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, typesTemplateFile, template.Types)
|
text, err := pathx.LoadTemplate(category, typesTemplateFile, template.Types)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/collection"
|
"github.com/tal-tech/go-zero/core/collection"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
|
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
|
||||||
}
|
}
|
||||||
camelTableName := table.Name.ToCamel()
|
camelTableName := table.Name.ToCamel()
|
||||||
text, err := util.LoadTemplate(category, updateTemplateFile, template.Update)
|
text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
@@ -63,7 +64,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update interface method
|
// update interface method
|
||||||
text, err = util.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
|
text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
camel := table.Name.ToCamel()
|
camel := table.Name.ToCamel()
|
||||||
text, err := util.LoadTemplate(category, varTemplateFile, template.Vars)
|
text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,6 +360,7 @@ func getTableFields(table *model.Table) (map[string]*Field, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
field := &Field{
|
field := &Field{
|
||||||
|
NameOriginal: each.Name,
|
||||||
Name: stringx.From(each.Name),
|
Name: stringx.From(each.Name),
|
||||||
DataType: dt,
|
DataType: dt,
|
||||||
Comment: each.Comment,
|
Comment: each.Comment,
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
|
||||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParsePlainText(t *testing.T) {
|
func TestParsePlainText(t *testing.T) {
|
||||||
sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql")
|
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||||
err := ioutil.WriteFile(sqlFile, []byte("plain text"), 0o777)
|
err := ioutil.WriteFile(sqlFile, []byte("plain text"), 0o777)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ func TestParsePlainText(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseSelect(t *testing.T) {
|
func TestParseSelect(t *testing.T) {
|
||||||
sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql")
|
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||||
err := ioutil.WriteFile(sqlFile, []byte("select * from user"), 0o777)
|
err := ioutil.WriteFile(sqlFile, []byte("select * from user"), 0o777)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ func TestParseSelect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseCreateTable(t *testing.T) {
|
func TestParseCreateTable(t *testing.T) {
|
||||||
sqlFile := filepath.Join(ctlutil.MustTempDir(), "tmp.sql")
|
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||||
err := ioutil.WriteFile(sqlFile, []byte("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"), 0o777)
|
err := ioutil.WriteFile(sqlFile, []byte("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"), 0o777)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ func prepareArgs(c *cli.Context) ([]byte, error) {
|
|||||||
apiPath := c.String("api")
|
apiPath := c.String("api")
|
||||||
|
|
||||||
var transferData Plugin
|
var transferData Plugin
|
||||||
if len(apiPath) > 0 && util.FileExists(apiPath) {
|
if len(apiPath) > 0 && pathx.FileExists(apiPath) {
|
||||||
api, err := parser.Parse(apiPath)
|
api, err := parser.Parse(apiPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ func RPC(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
@@ -87,7 +88,7 @@ func RPCNew(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
protoName := rpcname + ".proto"
|
protoName := rpcname + ".proto"
|
||||||
@@ -122,7 +123,7 @@ func RPCTemplate(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(home) > 0 {
|
if len(home) > 0 {
|
||||||
util.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(protoFile) == 0 {
|
if len(protoFile) == 0 {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,10 +39,10 @@ func Run(arg, dir string, in ...*bytes.Buffer) (string, error) {
|
|||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if stderr.Len() > 0 {
|
if stderr.Len() > 0 {
|
||||||
return "", errors.New(strings.TrimSuffix(stderr.String(), util.NL))
|
return "", errors.New(strings.TrimSuffix(stderr.String(), pathx.NL))
|
||||||
}
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSuffix(stdout.String(), util.NL), nil
|
return strings.TrimSuffix(stdout.String(), pathx.NL), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
|
|
||||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RPCGenerator defines a generator and configure
|
// RPCGenerator defines a generator and configure
|
||||||
@@ -42,7 +42,7 @@ func (g *RPCGenerator) Generate(src, target string, protoImportPath []string, go
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = util.MkdirIfNotExist(abs)
|
err = pathx.MkdirIfNotExist(abs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cfg = &conf.Config{
|
var cfg = &conf.Config{
|
||||||
@@ -58,7 +58,7 @@ func TestRpcGenerate(t *testing.T) {
|
|||||||
|
|
||||||
// case go mod
|
// case go mod
|
||||||
t.Run("GOMOD", func(t *testing.T) {
|
t.Run("GOMOD", func(t *testing.T) {
|
||||||
workDir := util.MustTempDir()
|
workDir := pathx.MustTempDir()
|
||||||
name := filepath.Base(workDir)
|
name := filepath.Base(workDir)
|
||||||
_, err = execx.Run("go mod init "+name, workDir)
|
_, err = execx.Run("go mod init "+name, workDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText)
|
text, err := pathx.LoadTemplate(category, callTemplateFile, callTemplateText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -100,13 +101,13 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
|
|||||||
sort.Strings(aliasKeys)
|
sort.Strings(aliasKeys)
|
||||||
err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||||
"name": callFilename,
|
"name": callFilename,
|
||||||
"alias": strings.Join(aliasKeys, util.NL),
|
"alias": strings.Join(aliasKeys, pathx.NL),
|
||||||
"head": head,
|
"head": head,
|
||||||
"filePackage": dir.Base,
|
"filePackage": dir.Base,
|
||||||
"package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
|
"package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
|
||||||
"serviceName": stringx.From(service.Name).ToCamel(),
|
"serviceName": stringx.From(service.Name).ToCamel(),
|
||||||
"functions": strings.Join(functions, util.NL),
|
"functions": strings.Join(functions, pathx.NL),
|
||||||
"interface": strings.Join(iFunctions, util.NL),
|
"interface": strings.Join(iFunctions, pathx.NL),
|
||||||
}, filename, true)
|
}, filename, true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -137,7 +138,7 @@ func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service)
|
|||||||
functions := make([]string, 0)
|
functions := make([]string, 0)
|
||||||
|
|
||||||
for _, rpc := range service.RPC {
|
for _, rpc := range service.RPC {
|
||||||
text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
|
text, err := pathx.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -171,7 +172,7 @@ func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Se
|
|||||||
functions := make([]string, 0)
|
functions := make([]string, 0)
|
||||||
|
|
||||||
for _, rpc := range service.RPC {
|
for _, rpc := range service.RPC {
|
||||||
text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
|
text, err := pathx.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const configTemplate = `package config
|
const configTemplate = `package config
|
||||||
@@ -32,11 +32,11 @@ func (g *DefaultGenerator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
fileName := filepath.Join(dir.Filename, configFilename+".go")
|
fileName := filepath.Join(dir.Filename, configFilename+".go")
|
||||||
if util.FileExists(fileName) {
|
if pathx.FileExists(fileName) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, configTemplateFileFile, configTemplate)
|
text, err := pathx.LoadTemplate(category, configTemplateFileFile, configTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ func (g *DefaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Conf
|
|||||||
|
|
||||||
fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
|
fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
|
text, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -67,14 +68,14 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
|
|||||||
imports := collection.NewSet()
|
imports := collection.NewSet()
|
||||||
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
|
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
|
||||||
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
|
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
|
||||||
text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
|
text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||||
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
|
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
|
||||||
"functions": functions,
|
"functions": functions,
|
||||||
"imports": strings.Join(imports.KeysStr(), util.NL),
|
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||||
}, filename, false)
|
}, filename, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
|
|||||||
|
|
||||||
func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
|
func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
|
||||||
functions := make([]string, 0)
|
functions := make([]string, 0)
|
||||||
text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -111,5 +112,5 @@ func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *
|
|||||||
}
|
}
|
||||||
|
|
||||||
functions = append(functions, buffer.String())
|
functions = append(functions, buffer.String())
|
||||||
return strings.Join(functions, util.NL), nil
|
return strings.Join(functions, pathx.NL), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -65,7 +66,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
|
|||||||
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
|
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
|
||||||
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
|
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
|
||||||
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
|
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
|
||||||
text, err := util.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
|
|||||||
|
|
||||||
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||||
"serviceName": etcFileName,
|
"serviceName": etcFileName,
|
||||||
"imports": strings.Join(imports, util.NL),
|
"imports": strings.Join(imports, pathx.NL),
|
||||||
"pkg": proto.PbPackage,
|
"pkg": proto.PbPackage,
|
||||||
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
|
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
|
||||||
"service": parser.CamelCase(proto.Service.Name),
|
"service": parser.CamelCase(proto.Service.Name),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := util.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -84,8 +85,8 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
|
|||||||
err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||||
"head": head,
|
"head": head,
|
||||||
"server": stringx.From(service.Name).ToCamel(),
|
"server": stringx.From(service.Name).ToCamel(),
|
||||||
"imports": strings.Join(imports.KeysStr(), util.NL),
|
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||||
"funcs": strings.Join(funcList, util.NL),
|
"funcs": strings.Join(funcList, pathx.NL),
|
||||||
"notStream": notStream,
|
"notStream": notStream,
|
||||||
}, serverFile, true)
|
}, serverFile, true)
|
||||||
return err
|
return err
|
||||||
@@ -94,7 +95,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
|
|||||||
func (g *DefaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
|
func (g *DefaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
|
||||||
var functionList []string
|
var functionList []string
|
||||||
for _, rpc := range service.RPC {
|
for _, rpc := range service.RPC {
|
||||||
text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
|
text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const svcTemplate = `package svc
|
const svcTemplate = `package svc
|
||||||
@@ -35,7 +36,7 @@ func (g *DefaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Conf
|
|||||||
}
|
}
|
||||||
|
|
||||||
fileName := filepath.Join(dir.Filename, svcFilename+".go")
|
fileName := filepath.Join(dir.Filename, svcFilename+".go")
|
||||||
text, err := util.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config) (DirCont
|
|||||||
Base: filepath.Base(callDir),
|
Base: filepath.Base(callDir),
|
||||||
}
|
}
|
||||||
for _, v := range inner {
|
for _, v := range inner {
|
||||||
err := util.MkdirIfNotExist(v.Filename)
|
err := pathx.MkdirIfNotExist(v.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,13 +30,13 @@ service {{.serviceName}} {
|
|||||||
func ProtoTmpl(out string) error {
|
func ProtoTmpl(out string) error {
|
||||||
protoFilename := filepath.Base(out)
|
protoFilename := filepath.Base(out)
|
||||||
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
|
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
|
||||||
text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
|
text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := filepath.Dir(out)
|
dir := filepath.Dir(out)
|
||||||
err = util.MkdirIfNotExist(dir)
|
err = pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,17 +5,17 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProtoTmpl(t *testing.T) {
|
func TestProtoTmpl(t *testing.T) {
|
||||||
_ = Clean()
|
_ = Clean()
|
||||||
// exists dir
|
// exists dir
|
||||||
err := ProtoTmpl(util.MustTempDir())
|
err := ProtoTmpl(pathx.MustTempDir())
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// not exist dir
|
// not exist dir
|
||||||
dir := filepath.Join(util.MustTempDir(), "test")
|
dir := filepath.Join(pathx.MustTempDir(), "test")
|
||||||
err = ProtoTmpl(dir)
|
err = ProtoTmpl(dir)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package generator
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ var templates = map[string]string{
|
|||||||
// GenTemplates is the entry for command goctl template,
|
// GenTemplates is the entry for command goctl template,
|
||||||
// it will create the specified category
|
// it will create the specified category
|
||||||
func GenTemplates(_ *cli.Context) error {
|
func GenTemplates(_ *cli.Context) error {
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertTemplate restores the deleted template files
|
// RevertTemplate restores the deleted template files
|
||||||
@@ -50,12 +50,12 @@ func RevertTemplate(name string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: no such file name", name)
|
return fmt.Errorf("%s: no such file name", name)
|
||||||
}
|
}
|
||||||
return util.CreateTemplate(category, name, content)
|
return pathx.CreateTemplate(category, name, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean deletes all template files
|
// Clean deletes all template files
|
||||||
func Clean() error {
|
func Clean() error {
|
||||||
return util.Clean(category)
|
return pathx.Clean(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is used to update the template files, it will delete the existing old templates at first,
|
// Update is used to update the template files, it will delete the existing old templates at first,
|
||||||
@@ -66,7 +66,7 @@ func Update() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.InitTemplates(category, templates)
|
return pathx.InitTemplates(category, templates)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Category returns a const string value for rpc template category
|
// Category returns a const string value for rpc template category
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenTemplates(t *testing.T) {
|
func TestGenTemplates(t *testing.T) {
|
||||||
@@ -20,7 +20,7 @@ func TestRevertTemplate(t *testing.T) {
|
|||||||
_ = Clean()
|
_ = Clean()
|
||||||
err := GenTemplates(nil)
|
err := GenTemplates(nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
fp, err := util.GetTemplateDir(category)
|
fp, err := pathx.GetTemplateDir(category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ func TestClean(t *testing.T) {
|
|||||||
_ = Clean()
|
_ = Clean()
|
||||||
err := GenTemplates(nil)
|
err := GenTemplates(nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
fp, err := util.GetTemplateDir(category)
|
fp, err := pathx.GetTemplateDir(category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
_ = Clean()
|
_ = Clean()
|
||||||
err := GenTemplates(nil)
|
err := GenTemplates(nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
fp, err := util.GetTemplateDir(category)
|
fp, err := pathx.GetTemplateDir(category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
|
mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
|
||||||
modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||||
rpcgen "github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
|
rpcgen "github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ const templateParentPath = "/"
|
|||||||
func GenTemplates(ctx *cli.Context) error {
|
func GenTemplates(ctx *cli.Context) error {
|
||||||
path := ctx.String("home")
|
path := ctx.String("home")
|
||||||
if len(path) != 0 {
|
if len(path) != 0 {
|
||||||
util.RegisterGoctlHome(path)
|
pathx.RegisterGoctlHome(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := errorx.Chain(
|
if err := errorx.Chain(
|
||||||
@@ -56,7 +56,7 @@ func GenTemplates(ctx *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, err := util.GetTemplateDir(templateParentPath)
|
dir, err := pathx.GetTemplateDir(templateParentPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ func GenTemplates(ctx *cli.Context) error {
|
|||||||
func CleanTemplates(ctx *cli.Context) error {
|
func CleanTemplates(ctx *cli.Context) error {
|
||||||
path := ctx.String("home")
|
path := ctx.String("home")
|
||||||
if len(path) != 0 {
|
if len(path) != 0 {
|
||||||
util.RegisterGoctlHome(path)
|
pathx.RegisterGoctlHome(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := errorx.Chain(
|
err := errorx.Chain(
|
||||||
@@ -119,7 +119,7 @@ func UpdateTemplates(ctx *cli.Context) (err error) {
|
|||||||
path := ctx.String("home")
|
path := ctx.String("home")
|
||||||
category := ctx.String("category")
|
category := ctx.String("category")
|
||||||
if len(path) != 0 {
|
if len(path) != 0 {
|
||||||
util.RegisterGoctlHome(path)
|
pathx.RegisterGoctlHome(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -156,7 +156,7 @@ func RevertTemplates(ctx *cli.Context) (err error) {
|
|||||||
category := ctx.String("category")
|
category := ctx.String("category")
|
||||||
filename := ctx.String("name")
|
filename := ctx.String("name")
|
||||||
if len(path) != 0 {
|
if len(path) != 0 {
|
||||||
util.RegisterGoctlHome(path)
|
pathx.RegisterGoctlHome(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/hash"
|
"github.com/tal-tech/go-zero/core/hash"
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/update/config"
|
"github.com/tal-tech/go-zero/tools/goctl/update/config"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -22,7 +22,7 @@ var configFile = flag.String("f", "etc/update-api.json", "the config file")
|
|||||||
|
|
||||||
func forChksumHandler(file string, next http.Handler) http.Handler {
|
func forChksumHandler(file string, next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !util.FileExists(file) {
|
if !pathx.FileExists(file) {
|
||||||
logx.Errorf("file %q not exist", file)
|
logx.Errorf("file %q not exist", file)
|
||||||
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
|
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/jsonx"
|
"github.com/tal-tech/go-zero/core/jsonx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Module contains the relative data of go module,
|
// Module contains the relative data of go module,
|
||||||
@@ -31,7 +31,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
workDir, err := util.ReadLink(workDir)
|
workDir, err := pathx.ReadLink(workDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
|
|||||||
var ret ProjectContext
|
var ret ProjectContext
|
||||||
ret.WorkDir = workDir
|
ret.WorkDir = workDir
|
||||||
ret.Name = filepath.Base(m.Dir)
|
ret.Name = filepath.Base(m.Dir)
|
||||||
dir, err := util.ReadLink(m.Dir)
|
dir, err := pathx.ReadLink(m.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProjectFromGoMod(t *testing.T) {
|
func TestProjectFromGoMod(t *testing.T) {
|
||||||
@@ -20,7 +20,7 @@ func TestProjectFromGoMod(t *testing.T) {
|
|||||||
}
|
}
|
||||||
projectName := stringx.Rand()
|
projectName := stringx.Rand()
|
||||||
dir := filepath.Join(gp, "src", projectName)
|
dir := filepath.Join(gp, "src", projectName)
|
||||||
err := util.MkdirIfNotExist(dir)
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// projectFromGoPath is used to find the main module and project file path
|
// projectFromGoPath is used to find the main module and project file path
|
||||||
@@ -21,20 +21,20 @@ func projectFromGoPath(workDir string) (*ProjectContext, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
workDir, err := util.ReadLink(workDir)
|
workDir, err := pathx.ReadLink(workDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
buildContext := build.Default
|
buildContext := build.Default
|
||||||
goPath := buildContext.GOPATH
|
goPath := buildContext.GOPATH
|
||||||
goPath, err = util.ReadLink(goPath)
|
goPath, err = pathx.ReadLink(goPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
goSrc := filepath.Join(goPath, "src")
|
goSrc := filepath.Join(goPath, "src")
|
||||||
if !util.FileExists(goSrc) {
|
if !pathx.FileExists(goSrc) {
|
||||||
return nil, errModuleCheck
|
return nil, errModuleCheck
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProjectFromGoPath(t *testing.T) {
|
func TestProjectFromGoPath(t *testing.T) {
|
||||||
@@ -19,7 +19,7 @@ func TestProjectFromGoPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
projectName := stringx.Rand()
|
projectName := stringx.Rand()
|
||||||
dir := filepath.Join(gp, "src", projectName)
|
dir := filepath.Join(gp, "src", projectName)
|
||||||
err := util.MkdirIfNotExist(dir)
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ func TestProjectFromGoPathNotInGoSrc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
projectName := stringx.Rand()
|
projectName := stringx.Rand()
|
||||||
dir := filepath.Join(gp, "src", projectName)
|
dir := filepath.Join(gp, "src", projectName)
|
||||||
err := util.MkdirIfNotExist(dir)
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/core/stringx"
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsGoMod(t *testing.T) {
|
func TestIsGoMod(t *testing.T) {
|
||||||
@@ -21,7 +21,7 @@ func TestIsGoMod(t *testing.T) {
|
|||||||
}
|
}
|
||||||
projectName := stringx.Rand()
|
projectName := stringx.Rand()
|
||||||
dir := filepath.Join(gp, "src", projectName)
|
dir := filepath.Join(gp, "src", projectName)
|
||||||
err := util.MkdirIfNotExist(dir)
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ func TestIsGoModNot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
projectName := stringx.Rand()
|
projectName := stringx.Rand()
|
||||||
dir := filepath.Join(gp, "src", projectName)
|
dir := filepath.Join(gp, "src", projectName)
|
||||||
err := util.MkdirIfNotExist(dir)
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
10
tools/goctl/util/env/env_test.go
vendored
10
tools/goctl/util/env/env_test.go
vendored
@@ -9,7 +9,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ func TestLookUpGo(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.True(t, util.FileExists(xGo))
|
assert.True(t, pathx.FileExists(xGo))
|
||||||
output, errOutput, err := execCommand(xGo, "version")
|
output, errOutput, err := execCommand(xGo, "version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -37,7 +37,7 @@ func TestLookUpProtoc(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.True(t, util.FileExists(xProtoc))
|
assert.True(t, pathx.FileExists(xProtoc))
|
||||||
output, errOutput, err := execCommand(xProtoc, "--version")
|
output, errOutput, err := execCommand(xProtoc, "--version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -54,7 +54,7 @@ func TestLookUpProtocGenGo(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
assert.True(t, util.FileExists(xProtocGenGo))
|
assert.True(t, pathx.FileExists(xProtocGenGo))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLookPath(t *testing.T) {
|
func TestLookPath(t *testing.T) {
|
||||||
@@ -62,7 +62,7 @@ func TestLookPath(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
assert.True(t, util.FileExists(xGo))
|
assert.True(t, pathx.FileExists(xGo))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCanExec(t *testing.T) {
|
func TestCanExec(t *testing.T) {
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CloneIntoGitHome(url string) (dir string, err error) {
|
func CloneIntoGitHome(url string) (dir string, err error) {
|
||||||
gitHome, err := GetGitHome()
|
gitHome, err := pathx.GetGitHome()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package pathx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package pathx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package pathx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package pathx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
//go:build windows
|
//go:build windows
|
||||||
// +build windows
|
// +build windows
|
||||||
|
|
||||||
package util
|
package pathx
|
||||||
|
|
||||||
func ReadLink(name string) (string, error) {
|
func ReadLink(name string) (string, error) {
|
||||||
return name, nil
|
return name, nil
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
//go:build linux || darwin
|
//go:build linux || darwin
|
||||||
// +build linux darwin
|
// +build linux darwin
|
||||||
|
|
||||||
package util
|
package pathx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
|
"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
|
||||||
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const regularPerm = 0o666
|
const regularPerm = 0o666
|
||||||
@@ -39,7 +40,7 @@ func (t *DefaultTemplate) GoFmt(format bool) *DefaultTemplate {
|
|||||||
|
|
||||||
// SaveTo writes the codes to the target path
|
// SaveTo writes the codes to the target path
|
||||||
func (t *DefaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
func (t *DefaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
||||||
if FileExists(path) && !forceUpdate {
|
if pathx.FileExists(path) && !forceUpdate {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user