mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
refactor file|path (#1409)
Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
parent
290de6aa96
commit
89ce5e492b
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -46,7 +47,7 @@ func ApiCommand(c *cli.Context) error {
|
||||
return errors.New("missing -o")
|
||||
}
|
||||
|
||||
fp, err := util.CreateIfNotExist(apiFile)
|
||||
fp, err := pathx.CreateIfNotExist(apiFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -62,15 +63,15 @@ func ApiCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
|
||||
baseName := pathx.FileNameWithoutExt(filepath.Base(apiFile))
|
||||
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
|
||||
baseName = baseName[:len(baseName)-4]
|
||||
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
|
||||
|
@ -3,7 +3,7 @@ package apigen
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -23,12 +23,12 @@ func Category() string {
|
||||
|
||||
// Clean cleans the generated deployment files.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates generates api template files.
|
||||
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.
|
||||
@ -37,7 +37,7 @@ func RevertTemplate(name string) error {
|
||||
if !ok {
|
||||
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.
|
||||
@ -47,5 +47,5 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
"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/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -40,7 +41,7 @@ func GoCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
if len(apiFile) == 0 {
|
||||
return errors.New("missing -api")
|
||||
@ -64,7 +65,7 @@ func DoGenProject(apiFile, dir, style string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(pathx.MkdirIfNotExist(dir))
|
||||
rootPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -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 {
|
||||
var imports []string
|
||||
imports = append(imports, fmt.Sprintf("\"%s\"",
|
||||
util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
|
||||
imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
|
||||
pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
|
||||
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
|
||||
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()
|
||||
|
@ -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/spec"
|
||||
"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"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"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"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
|
@ -11,8 +11,8 @@ import (
|
||||
"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/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/pathx"
|
||||
"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 {
|
||||
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 _, route := range group.Routes {
|
||||
folder := route.GetAnnotation(groupProperty)
|
||||
@ -161,7 +161,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
||||
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()
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"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"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ package gogen
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -33,12 +33,12 @@ func Category() string {
|
||||
|
||||
// Clean cleans the generated deployment files.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates generates api template files.
|
||||
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.
|
||||
@ -47,7 +47,7 @@ func RevertTemplate(name string) error {
|
||||
if !ok {
|
||||
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.
|
||||
@ -57,5 +57,5 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
@ -6,13 +6,13 @@ import (
|
||||
"testing"
|
||||
|
||||
"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) {
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
file := filepath.Join(dir, "main.tpl")
|
||||
data, err := ioutil.ReadFile(file)
|
||||
@ -22,10 +22,10 @@ func TestGenTemplates(t *testing.T) {
|
||||
|
||||
func TestRevertTemplate(t *testing.T) {
|
||||
name := "main.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -33,7 +33,7 @@ func TestRevertTemplate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
modifyData := string(data) + "modify"
|
||||
err = util.CreateTemplate(category, name, modifyData)
|
||||
err = pathx.CreateTemplate(category, name, modifyData)
|
||||
assert.Nil(t, err)
|
||||
|
||||
data, err = ioutil.ReadFile(file)
|
||||
@ -50,12 +50,12 @@ func TestRevertTemplate(t *testing.T) {
|
||||
|
||||
func TestClean(t *testing.T) {
|
||||
name := "main.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, Clean())
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -65,10 +65,10 @@ func TestClean(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
name := "main.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -76,7 +76,7 @@ func TestUpdate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
modifyData := string(data) + "modify"
|
||||
err = util.CreateTemplate(category, name, modifyData)
|
||||
err = pathx.CreateTemplate(category, name, modifyData)
|
||||
assert.Nil(t, err)
|
||||
|
||||
data, err = ioutil.ReadFile(file)
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
"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/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
type fileGenConfig struct {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/logrusorgru/aurora"
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -30,7 +30,7 @@ func JavaCommand(c *cli.Context) error {
|
||||
|
||||
api.Service = api.Service.JoinPrefix()
|
||||
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(genComponents(dir, packetName, api))
|
||||
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
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 (
|
||||
@ -110,7 +111,7 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type
|
||||
|
||||
modelFile := util.Title(ty.Name()) + ".java"
|
||||
filename := path.Join(dir, modelDir, modelFile)
|
||||
if err := util.RemoveOrQuit(filename); err != nil {
|
||||
if err := pathx.RemoveOrQuit(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -270,7 +271,7 @@ func (c *componentsContext) buildConstructor() (string, string, error) {
|
||||
writeIndent(&constructorSetter, 2)
|
||||
constructorSetter.WriteString(fmt.Sprintf("this.%s = %s;", pn, util.Untitle(member.Name)))
|
||||
if index != len(c.members)-1 {
|
||||
constructorSetter.WriteString(util.NL)
|
||||
constructorSetter.WriteString(pathx.NL)
|
||||
}
|
||||
}
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
|
||||
if len(member.Comment) > 0 {
|
||||
writeIndent(writer, indent)
|
||||
fmt.Fprint(writer, member.Comment+util.NL)
|
||||
fmt.Fprint(writer, member.Comment+pathx.NL)
|
||||
}
|
||||
writeIndent(writer, indent)
|
||||
ty, err := specTypeToJava(member.Type)
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
|
||||
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/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -49,7 +50,7 @@ func CreateServiceCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = util.MkdirIfNotExist(abs)
|
||||
err = pathx.MkdirIfNotExist(abs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -74,10 +75,10 @@ func CreateServiceCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package new
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -23,12 +23,12 @@ func Category() string {
|
||||
|
||||
// Clean cleans the generated deployment files.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates generates api template files.
|
||||
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.
|
||||
@ -37,7 +37,7 @@ func RevertTemplate(name string) error {
|
||||
if !ok {
|
||||
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.
|
||||
@ -47,5 +47,5 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -119,7 +119,7 @@ func TestApiParser(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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -149,7 +149,7 @@ func TestApiParser(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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -163,7 +163,7 @@ func TestApiParser(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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -177,7 +177,7 @@ func TestApiParser(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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -207,7 +207,7 @@ func TestApiParser(t *testing.T) {
|
||||
`)
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -236,7 +236,7 @@ func TestApiParser(t *testing.T) {
|
||||
`)
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -260,7 +260,7 @@ func TestApiParser(t *testing.T) {
|
||||
`)
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/logrusorgru/aurora"
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -33,7 +33,7 @@ func TsCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
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(genComponents(dir, api))
|
||||
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
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 (
|
||||
@ -30,7 +30,7 @@ func genComponents(dir string, api *spec.ApiSpec) error {
|
||||
|
||||
outputFile := apiutil.ComponentName(api) + ".ts"
|
||||
filename := path.Join(dir, outputFile)
|
||||
if err := util.RemoveIfExist(filename); err != nil {
|
||||
if err := pathx.RemoveIfExist(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
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 (
|
||||
@ -20,7 +21,7 @@ const (
|
||||
|
||||
func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
|
||||
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
|
||||
}
|
||||
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(imports) > 0 {
|
||||
imports += util.NL
|
||||
imports += pathx.NL
|
||||
}
|
||||
outputFile := apiutil.ComponentName(api)
|
||||
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)
|
||||
|
@ -10,19 +10,19 @@ import (
|
||||
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
// MaybeCreateFile creates file if not exists
|
||||
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)
|
||||
if util.FileExists(fpath) {
|
||||
if pathx.FileExists(fpath) {
|
||||
fmt.Printf("%s exists, ignored generation\n", fpath)
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
fp, err = util.CreateIfNotExist(fpath)
|
||||
fp, err = pathx.CreateIfNotExist(fpath)
|
||||
created = err == nil
|
||||
return
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -57,14 +58,14 @@ func DockerCommand(c *cli.Context) (err error) {
|
||||
}
|
||||
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
if len(goFile) == 0 {
|
||||
return errors.New("-go can't be empty")
|
||||
}
|
||||
|
||||
if !util.FileExists(goFile) {
|
||||
if !pathx.FileExists(goFile) {
|
||||
return fmt.Errorf("file %q not found", goFile)
|
||||
}
|
||||
|
||||
@ -82,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
projDir, ok := util.FindProjectPath(goFile)
|
||||
projDir, ok := pathx.FindProjectPath(goFile)
|
||||
if ok {
|
||||
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 = "."
|
||||
}
|
||||
|
||||
out, err := util.CreateIfNotExist(dockerfileName)
|
||||
out, err := pathx.CreateIfNotExist(dockerfileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
text, err := util.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
|
||||
text, err := pathx.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -151,7 +152,7 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
|
||||
Chinese: offset == cstOffset,
|
||||
GoRelPath: projPath,
|
||||
GoFile: goFile,
|
||||
ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)),
|
||||
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
|
||||
HasPort: port > 0,
|
||||
Port: port,
|
||||
Argument: builder.String(),
|
||||
@ -165,9 +166,9 @@ func getFilePath(file string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
|
||||
projPath, ok := pathx.FindGoModPath(filepath.Join(wd, file))
|
||||
if !ok {
|
||||
projPath, err = util.PathFromGoSrc()
|
||||
projPath, err = pathx.PathFromGoSrc()
|
||||
if err != nil {
|
||||
return "", errors.New("no go.mod found, or not in GOPATH")
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -43,7 +43,7 @@ CMD ["./{{.ExeFile}}"{{.Argument}}]
|
||||
|
||||
// Clean deletes all templates files
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates creates docker template files
|
||||
@ -58,7 +58,7 @@ func Category() string {
|
||||
|
||||
// RevertTemplate recovers the deleted template files
|
||||
func RevertTemplate(name string) error {
|
||||
return util.CreateTemplate(category, name, dockerTemplate)
|
||||
return pathx.CreateTemplate(category, name, dockerTemplate)
|
||||
}
|
||||
|
||||
// Update deletes and creates new template files
|
||||
@ -72,7 +72,7 @@ func Update() error {
|
||||
}
|
||||
|
||||
func initTemplate() error {
|
||||
return util.InitTemplates(category, map[string]string{
|
||||
return pathx.InitTemplates(category, map[string]string{
|
||||
dockerTemplateFile: dockerTemplate,
|
||||
})
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -50,7 +51,7 @@ func DeploymentCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
|
||||
text, err := pathx.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := util.CreateIfNotExist(c.String("o"))
|
||||
out, err := pathx.CreateIfNotExist(c.String("o"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -102,12 +103,12 @@ func Category() string {
|
||||
|
||||
// Clean cleans the generated deployment files.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates generates the deployment template files.
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
return util.InitTemplates(category, map[string]string{
|
||||
return pathx.InitTemplates(category, map[string]string{
|
||||
deployTemplateFile: deploymentTemplate,
|
||||
jobTemplateFile: jobTmeplate,
|
||||
})
|
||||
@ -115,7 +116,7 @@ func GenTemplates(_ *cli.Context) error {
|
||||
|
||||
// RevertTemplate reverts the given template file to the default value.
|
||||
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.
|
||||
@ -125,7 +126,7 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, map[string]string{
|
||||
return pathx.InitTemplates(category, map[string]string{
|
||||
deployTemplateFile: deploymentTemplate,
|
||||
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/util"
|
||||
"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
|
||||
@ -39,7 +40,7 @@ func generateModel(ctx *Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Text)
|
||||
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -58,7 +59,7 @@ func generateModel(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 {
|
||||
return err
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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 = `
|
||||
@ -19,7 +19,7 @@ func TestDo(t *testing.T) {
|
||||
cfg, err := config.NewConfig(config.DefaultFormat)
|
||||
assert.Nil(t, err)
|
||||
|
||||
tempDir := util.MustTempDir()
|
||||
tempDir := pathx.MustTempDir()
|
||||
typesfile := filepath.Join(tempDir, "types.go")
|
||||
err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666)
|
||||
assert.Nil(t, err)
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -26,12 +26,12 @@ func Category() string {
|
||||
|
||||
// Clean cleans the mongo templates.
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// Templates initializes the mongo templates.
|
||||
func Templates(_ *cli.Context) error {
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// RevertTemplate reverts the given template.
|
||||
@ -41,7 +41,7 @@ func RevertTemplate(name string) error {
|
||||
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.
|
||||
@ -51,5 +51,5 @@ func Update() error {
|
||||
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/model/mongo/generate"
|
||||
file "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -26,7 +27,7 @@ func Action(ctx *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
file.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
if len(tp) == 0 {
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/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/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -50,7 +51,7 @@ func MysqlDDL(ctx *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
file.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
cfg, err := config.NewConfig(style)
|
||||
if err != nil {
|
||||
@ -76,7 +77,7 @@ func MySqlDataSource(ctx *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
file.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
pattern := strings.TrimSpace(ctx.String(flagTable))
|
||||
@ -105,7 +106,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
file.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
if len(schema) == 0 {
|
||||
|
@ -9,7 +9,7 @@ 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"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -23,12 +23,12 @@ func TestFromDDl(t *testing.T) {
|
||||
err := gen.Clean()
|
||||
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)
|
||||
|
||||
// case dir is not exists
|
||||
unknownDir := filepath.Join(util.MustTempDir(), "test", "user.sql")
|
||||
err = fromDDL(unknownDir, util.MustTempDir(), cfg, true, false, "go_zero")
|
||||
unknownDir := filepath.Join(pathx.MustTempDir(), "test", "user.sql")
|
||||
err = fromDDL(unknownDir, pathx.MustTempDir(), cfg, true, false, "go_zero")
|
||||
assert.True(t, func() bool {
|
||||
switch err.(type) {
|
||||
case *os.PathError:
|
||||
@ -39,13 +39,13 @@ func TestFromDDl(t *testing.T) {
|
||||
}())
|
||||
|
||||
// case empty src
|
||||
err = fromDDL("", util.MustTempDir(), cfg, true, false, "go_zero")
|
||||
err = fromDDL("", pathx.MustTempDir(), cfg, true, false, "go_zero")
|
||||
if err != nil {
|
||||
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
|
||||
}
|
||||
|
||||
tempDir := filepath.Join(util.MustTempDir(), "test")
|
||||
err = util.MkdirIfNotExist(tempDir)
|
||||
tempDir := filepath.Join(pathx.MustTempDir(), "test")
|
||||
err = pathx.MkdirIfNotExist(tempDir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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()
|
||||
text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete)
|
||||
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@ -43,7 +44,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
}
|
||||
|
||||
// interface method
|
||||
text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
|
||||
text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
|
||||
if err != nil {
|
||||
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/template"
|
||||
"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) {
|
||||
@ -29,7 +30,7 @@ func genField(field *parser.Field) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field)
|
||||
text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -3,12 +3,13 @@ package gen
|
||||
import (
|
||||
"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/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) {
|
||||
camel := table.Name.ToCamel()
|
||||
text, err := util.LoadTemplate(category, findOneTemplateFile, template.FindOne)
|
||||
text, err := pathx.LoadTemplate(category, findOneTemplateFile, template.FindOne)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@ -30,7 +31,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
text, err = util.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
|
||||
text, err = pathx.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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) {
|
||||
text, err := util.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
|
||||
text, err := pathx.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -47,7 +48,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
list = append(list, output.String())
|
||||
}
|
||||
|
||||
text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
|
||||
text, err = pathx.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -79,7 +80,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
}
|
||||
|
||||
if withCache {
|
||||
text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
|
||||
text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -96,15 +97,15 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
}
|
||||
|
||||
return &findOneCode{
|
||||
findOneMethod: strings.Join(list, util.NL),
|
||||
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
|
||||
findOneMethod: strings.Join(list, pathx.NL),
|
||||
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
|
||||
cacheExtra: out.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &findOneCode{
|
||||
findOneMethod: strings.Join(list, util.NL),
|
||||
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
|
||||
findOneMethod: strings.Join(list, pathx.NL),
|
||||
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
"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/format"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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
|
||||
pkg := filepath.Base(dirAbs)
|
||||
err = util.MkdirIfNotExist(dir)
|
||||
err = pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -134,7 +135,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
||||
|
||||
g.dir = dirAbs
|
||||
g.pkg = filepath.Base(dirAbs)
|
||||
err = util.MkdirIfNotExist(dirAbs)
|
||||
err = pathx.MkdirIfNotExist(dirAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -148,7 +149,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
||||
|
||||
name := util.SafeString(modelFilename) + ".go"
|
||||
filename := filepath.Join(dirAbs, name)
|
||||
if util.FileExists(filename) {
|
||||
if pathx.FileExists(filename) {
|
||||
g.Warning("%s already exists, ignored.", name)
|
||||
continue
|
||||
}
|
||||
@ -165,7 +166,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@ -261,7 +262,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
|
||||
var list []string
|
||||
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 {
|
||||
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) {
|
||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
"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/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;"
|
||||
@ -23,11 +23,11 @@ func TestCacheModel(t *testing.T) {
|
||||
logx.Disable()
|
||||
_ = Clean()
|
||||
|
||||
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
|
||||
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
||||
assert.Nil(t, err)
|
||||
|
||||
dir := filepath.Join(util.MustTempDir(), "./testmodel")
|
||||
dir := filepath.Join(pathx.MustTempDir(), "./testmodel")
|
||||
cacheDir := filepath.Join(dir, "cache")
|
||||
noCacheDir := filepath.Join(dir, "nocache")
|
||||
g, err := NewDefaultGenerator(cacheDir, &config.Config{
|
||||
@ -58,7 +58,7 @@ func TestNamingModel(t *testing.T) {
|
||||
logx.Disable()
|
||||
_ = Clean()
|
||||
|
||||
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
|
||||
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
|
||||
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
@ -3,11 +3,12 @@ package gen
|
||||
import (
|
||||
"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 genImports(withCache, timeImport bool) (string, error) {
|
||||
if withCache {
|
||||
text, err := util.LoadTemplate(category, importsTemplateFile, template.Imports)
|
||||
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -22,7 +23,7 @@ func genImports(withCache, timeImport bool) (string, error) {
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
|
||||
text, err := pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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()
|
||||
text, err := util.LoadTemplate(category, insertTemplateFile, template.Insert)
|
||||
text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@ -67,7 +68,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
}
|
||||
|
||||
// interface method
|
||||
text, err = util.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
|
||||
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
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 {
|
||||
return "", err
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package gen
|
||||
import (
|
||||
"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 genTag(in string) (string, error) {
|
||||
@ -10,7 +11,7 @@ func genTag(in string) (string, error) {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, tagTemplateFile, template.Tag)
|
||||
text, err := pathx.LoadTemplate(category, tagTemplateFile, template.Tag)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -62,12 +62,12 @@ func Category() string {
|
||||
|
||||
// Clean deletes all template files
|
||||
func Clean() error {
|
||||
return util.Clean(category)
|
||||
return pathx.Clean(category)
|
||||
}
|
||||
|
||||
// GenTemplates creates template files if not exists
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// 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 util.CreateTemplate(category, name, content)
|
||||
return pathx.CreateTemplate(category, name, content)
|
||||
}
|
||||
|
||||
// Update provides template clean and init
|
||||
@ -87,5 +87,5 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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) {
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
file := filepath.Join(dir, "model-new.tpl")
|
||||
data, err := ioutil.ReadFile(file)
|
||||
@ -23,10 +23,10 @@ func TestGenTemplates(t *testing.T) {
|
||||
|
||||
func TestRevertTemplate(t *testing.T) {
|
||||
name := "model-new.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -34,7 +34,7 @@ func TestRevertTemplate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
modifyData := string(data) + "modify"
|
||||
err = util.CreateTemplate(category, name, modifyData)
|
||||
err = pathx.CreateTemplate(category, name, modifyData)
|
||||
assert.Nil(t, err)
|
||||
|
||||
data, err = ioutil.ReadFile(file)
|
||||
@ -51,12 +51,12 @@ func TestRevertTemplate(t *testing.T) {
|
||||
|
||||
func TestClean(t *testing.T) {
|
||||
name := "model-new.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, Clean())
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -66,10 +66,10 @@ func TestClean(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
name := "model-new.tpl"
|
||||
err := util.InitTemplates(category, templates)
|
||||
err := pathx.InitTemplates(category, templates)
|
||||
assert.Nil(t, err)
|
||||
|
||||
dir, err := util.GetTemplateDir(category)
|
||||
dir, err := pathx.GetTemplateDir(category)
|
||||
assert.Nil(t, err)
|
||||
|
||||
file := filepath.Join(dir, name)
|
||||
@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
modifyData := string(data) + "modify"
|
||||
err = util.CreateTemplate(category, name, modifyData)
|
||||
err = pathx.CreateTemplate(category, name, modifyData)
|
||||
assert.Nil(t, err)
|
||||
|
||||
data, err = ioutil.ReadFile(file)
|
||||
|
@ -3,6 +3,7 @@ package gen
|
||||
import (
|
||||
"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 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
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, typesTemplateFile, template.Types)
|
||||
text, err := pathx.LoadTemplate(category, typesTemplateFile, template.Types)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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())
|
||||
}
|
||||
camelTableName := table.Name.ToCamel()
|
||||
text, err := util.LoadTemplate(category, updateTemplateFile, template.Update)
|
||||
text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@ -63,7 +64,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
}
|
||||
|
||||
// update interface method
|
||||
text, err = util.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
|
||||
text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"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()
|
||||
text, err := util.LoadTemplate(category, varTemplateFile, template.Vars)
|
||||
text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"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/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func TestParsePlainText(t *testing.T) {
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
"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/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"
|
||||
)
|
||||
|
||||
@ -73,7 +73,7 @@ func prepareArgs(c *cli.Context) ([]byte, error) {
|
||||
apiPath := c.String("api")
|
||||
|
||||
var transferData Plugin
|
||||
if len(apiPath) > 0 && util.FileExists(apiPath) {
|
||||
if len(apiPath) > 0 && pathx.FileExists(apiPath) {
|
||||
api, err := parser.Parse(apiPath)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -34,7 +35,7 @@ func RPC(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
if len(src) == 0 {
|
||||
@ -87,7 +88,7 @@ func RPCNew(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
protoName := rpcname + ".proto"
|
||||
@ -122,7 +123,7 @@ func RPCTemplate(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
if len(home) > 0 {
|
||||
util.RegisterGoctlHome(home)
|
||||
pathx.RegisterGoctlHome(home)
|
||||
}
|
||||
|
||||
if len(protoFile) == 0 {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"runtime"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -39,10 +39,10 @@ func Run(arg, dir string, in ...*bytes.Buffer) (string, error) {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
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 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"
|
||||
"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/ctx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
// RPCGenerator defines a generator and configure
|
||||
@ -42,7 +42,7 @@ func (g *RPCGenerator) Generate(src, target string, protoImportPath []string, go
|
||||
return err
|
||||
}
|
||||
|
||||
err = util.MkdirIfNotExist(abs)
|
||||
err = pathx.MkdirIfNotExist(abs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
var cfg = &conf.Config{
|
||||
@ -58,7 +58,7 @@ func TestRpcGenerate(t *testing.T) {
|
||||
|
||||
// case go mod
|
||||
t.Run("GOMOD", func(t *testing.T) {
|
||||
workDir := util.MustTempDir()
|
||||
workDir := pathx.MustTempDir()
|
||||
name := filepath.Base(workDir)
|
||||
_, err = execx.Run("go mod init "+name, workDir)
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
|
||||
return err
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText)
|
||||
text, err := pathx.LoadTemplate(category, callTemplateFile, callTemplateText)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -100,13 +101,13 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
|
||||
sort.Strings(aliasKeys)
|
||||
err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||
"name": callFilename,
|
||||
"alias": strings.Join(aliasKeys, util.NL),
|
||||
"alias": strings.Join(aliasKeys, pathx.NL),
|
||||
"head": head,
|
||||
"filePackage": dir.Base,
|
||||
"package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
|
||||
"serviceName": stringx.From(service.Name).ToCamel(),
|
||||
"functions": strings.Join(functions, util.NL),
|
||||
"interface": strings.Join(iFunctions, util.NL),
|
||||
"functions": strings.Join(functions, pathx.NL),
|
||||
"interface": strings.Join(iFunctions, pathx.NL),
|
||||
}, filename, true)
|
||||
return err
|
||||
}
|
||||
@ -137,7 +138,7 @@ func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service)
|
||||
functions := make([]string, 0)
|
||||
|
||||
for _, rpc := range service.RPC {
|
||||
text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
|
||||
text, err := pathx.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -171,7 +172,7 @@ func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Se
|
||||
functions := make([]string, 0)
|
||||
|
||||
for _, rpc := range service.RPC {
|
||||
text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
|
||||
text, err := pathx.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
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")
|
||||
if util.FileExists(fileName) {
|
||||
if pathx.FileExists(fileName) {
|
||||
return nil
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, configTemplateFileFile, configTemplate)
|
||||
text, err := pathx.LoadTemplate(category, configTemplateFileFile, configTemplate)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -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))
|
||||
|
||||
text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
|
||||
text, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -67,14 +68,14 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
|
||||
imports := collection.NewSet()
|
||||
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().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 {
|
||||
return err
|
||||
}
|
||||
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
|
||||
"functions": functions,
|
||||
"imports": strings.Join(imports.KeysStr(), util.NL),
|
||||
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||
}, filename, false)
|
||||
if err != nil {
|
||||
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) {
|
||||
functions := make([]string, 0)
|
||||
text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
||||
text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -111,5 +112,5 @@ func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *
|
||||
}
|
||||
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -65,7 +66,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
|
||||
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
|
||||
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
|
||||
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
|
||||
text, err := util.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||
text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||
if err != nil {
|
||||
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{}{
|
||||
"serviceName": etcFileName,
|
||||
"imports": strings.Join(imports, util.NL),
|
||||
"imports": strings.Join(imports, pathx.NL),
|
||||
"pkg": proto.PbPackage,
|
||||
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
|
||||
"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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -68,7 +69,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
|
||||
return err
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
||||
text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
||||
if err != nil {
|
||||
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{}{
|
||||
"head": head,
|
||||
"server": stringx.From(service.Name).ToCamel(),
|
||||
"imports": strings.Join(imports.KeysStr(), util.NL),
|
||||
"funcs": strings.Join(funcList, util.NL),
|
||||
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||
"funcs": strings.Join(funcList, pathx.NL),
|
||||
"notStream": notStream,
|
||||
}, serverFile, true)
|
||||
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) {
|
||||
var functionList []string
|
||||
for _, rpc := range service.RPC {
|
||||
text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
|
||||
text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
|
||||
if err != nil {
|
||||
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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
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")
|
||||
text, err := util.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
||||
text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
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/util"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -111,7 +111,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config) (DirCont
|
||||
Base: filepath.Base(callDir),
|
||||
}
|
||||
for _, v := range inner {
|
||||
err := util.MkdirIfNotExist(v.Filename)
|
||||
err := pathx.MkdirIfNotExist(v.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"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/util/stringx"
|
||||
)
|
||||
|
||||
@ -29,13 +30,13 @@ service {{.serviceName}} {
|
||||
func ProtoTmpl(out string) error {
|
||||
protoFilename := filepath.Base(out)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
dir := filepath.Dir(out)
|
||||
err = util.MkdirIfNotExist(dir)
|
||||
err = pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -5,17 +5,17 @@ import (
|
||||
"testing"
|
||||
|
||||
"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) {
|
||||
_ = Clean()
|
||||
// exists dir
|
||||
err := ProtoTmpl(util.MustTempDir())
|
||||
err := ProtoTmpl(pathx.MustTempDir())
|
||||
assert.Nil(t, err)
|
||||
|
||||
// not exist dir
|
||||
dir := filepath.Join(util.MustTempDir(), "test")
|
||||
dir := filepath.Join(pathx.MustTempDir(), "test")
|
||||
err = ProtoTmpl(dir)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package generator
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -41,7 +41,7 @@ var templates = map[string]string{
|
||||
// GenTemplates is the entry for command goctl template,
|
||||
// it will create the specified category
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// RevertTemplate restores the deleted template files
|
||||
@ -50,12 +50,12 @@ func RevertTemplate(name string) error {
|
||||
if !ok {
|
||||
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
|
||||
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,
|
||||
@ -66,7 +66,7 @@ func Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.InitTemplates(category, templates)
|
||||
return pathx.InitTemplates(category, templates)
|
||||
}
|
||||
|
||||
// Category returns a const string value for rpc template category
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"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) {
|
||||
@ -20,7 +20,7 @@ func TestRevertTemplate(t *testing.T) {
|
||||
_ = Clean()
|
||||
err := GenTemplates(nil)
|
||||
assert.Nil(t, err)
|
||||
fp, err := util.GetTemplateDir(category)
|
||||
fp, err := pathx.GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -61,7 +61,7 @@ func TestClean(t *testing.T) {
|
||||
_ = Clean()
|
||||
err := GenTemplates(nil)
|
||||
assert.Nil(t, err)
|
||||
fp, err := util.GetTemplateDir(category)
|
||||
fp, err := pathx.GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -80,7 +80,7 @@ func TestUpdate(t *testing.T) {
|
||||
_ = Clean()
|
||||
err := GenTemplates(nil)
|
||||
assert.Nil(t, err)
|
||||
fp, err := util.GetTemplateDir(category)
|
||||
fp, err := pathx.GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate"
|
||||
modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
||||
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"
|
||||
)
|
||||
|
||||
@ -24,7 +24,7 @@ const templateParentPath = "/"
|
||||
func GenTemplates(ctx *cli.Context) error {
|
||||
path := ctx.String("home")
|
||||
if len(path) != 0 {
|
||||
util.RegisterGoctlHome(path)
|
||||
pathx.RegisterGoctlHome(path)
|
||||
}
|
||||
|
||||
if err := errorx.Chain(
|
||||
@ -56,7 +56,7 @@ func GenTemplates(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
dir, err := util.GetTemplateDir(templateParentPath)
|
||||
dir, err := pathx.GetTemplateDir(templateParentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -76,7 +76,7 @@ func GenTemplates(ctx *cli.Context) error {
|
||||
func CleanTemplates(ctx *cli.Context) error {
|
||||
path := ctx.String("home")
|
||||
if len(path) != 0 {
|
||||
util.RegisterGoctlHome(path)
|
||||
pathx.RegisterGoctlHome(path)
|
||||
}
|
||||
|
||||
err := errorx.Chain(
|
||||
@ -119,7 +119,7 @@ func UpdateTemplates(ctx *cli.Context) (err error) {
|
||||
path := ctx.String("home")
|
||||
category := ctx.String("category")
|
||||
if len(path) != 0 {
|
||||
util.RegisterGoctlHome(path)
|
||||
pathx.RegisterGoctlHome(path)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@ -156,7 +156,7 @@ func RevertTemplates(ctx *cli.Context) (err error) {
|
||||
category := ctx.String("category")
|
||||
filename := ctx.String("name")
|
||||
if len(path) != 0 {
|
||||
util.RegisterGoctlHome(path)
|
||||
pathx.RegisterGoctlHome(path)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/core/hash"
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
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 {
|
||||
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)
|
||||
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
|
||||
return
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
// Module contains the relative data of go module,
|
||||
@ -31,7 +31,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
workDir, err := util.ReadLink(workDir)
|
||||
workDir, err := pathx.ReadLink(workDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -49,7 +49,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
|
||||
var ret ProjectContext
|
||||
ret.WorkDir = workDir
|
||||
ret.Name = filepath.Base(m.Dir)
|
||||
dir, err := util.ReadLink(m.Dir)
|
||||
dir, err := pathx.ReadLink(m.Dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func TestProjectFromGoMod(t *testing.T) {
|
||||
@ -20,7 +20,7 @@ func TestProjectFromGoMod(t *testing.T) {
|
||||
}
|
||||
projectName := stringx.Rand()
|
||||
dir := filepath.Join(gp, "src", projectName)
|
||||
err := util.MkdirIfNotExist(dir)
|
||||
err := pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"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
|
||||
@ -21,20 +21,20 @@ func projectFromGoPath(workDir string) (*ProjectContext, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
workDir, err := util.ReadLink(workDir)
|
||||
workDir, err := pathx.ReadLink(workDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buildContext := build.Default
|
||||
goPath := buildContext.GOPATH
|
||||
goPath, err = util.ReadLink(goPath)
|
||||
goPath, err = pathx.ReadLink(goPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
goSrc := filepath.Join(goPath, "src")
|
||||
if !util.FileExists(goSrc) {
|
||||
if !pathx.FileExists(goSrc) {
|
||||
return nil, errModuleCheck
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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) {
|
||||
@ -19,7 +19,7 @@ func TestProjectFromGoPath(t *testing.T) {
|
||||
}
|
||||
projectName := stringx.Rand()
|
||||
dir := filepath.Join(gp, "src", projectName)
|
||||
err := util.MkdirIfNotExist(dir)
|
||||
err := pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -41,7 +41,7 @@ func TestProjectFromGoPathNotInGoSrc(t *testing.T) {
|
||||
}
|
||||
projectName := stringx.Rand()
|
||||
dir := filepath.Join(gp, "src", projectName)
|
||||
err := util.MkdirIfNotExist(dir)
|
||||
err := pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func TestIsGoMod(t *testing.T) {
|
||||
@ -21,7 +21,7 @@ func TestIsGoMod(t *testing.T) {
|
||||
}
|
||||
projectName := stringx.Rand()
|
||||
dir := filepath.Join(gp, "src", projectName)
|
||||
err := util.MkdirIfNotExist(dir)
|
||||
err := pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -45,7 +45,7 @@ func TestIsGoModNot(t *testing.T) {
|
||||
}
|
||||
projectName := stringx.Rand()
|
||||
dir := filepath.Join(gp, "src", projectName)
|
||||
err := util.MkdirIfNotExist(dir)
|
||||
err := pathx.MkdirIfNotExist(dir)
|
||||
if err != nil {
|
||||
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"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -19,7 +19,7 @@ func TestLookUpGo(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, util.FileExists(xGo))
|
||||
assert.True(t, pathx.FileExists(xGo))
|
||||
output, errOutput, err := execCommand(xGo, "version")
|
||||
if err != nil {
|
||||
return
|
||||
@ -37,7 +37,7 @@ func TestLookUpProtoc(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, util.FileExists(xProtoc))
|
||||
assert.True(t, pathx.FileExists(xProtoc))
|
||||
output, errOutput, err := execCommand(xProtoc, "--version")
|
||||
if err != nil {
|
||||
return
|
||||
@ -54,7 +54,7 @@ func TestLookUpProtocGenGo(t *testing.T) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
assert.True(t, util.FileExists(xProtocGenGo))
|
||||
assert.True(t, pathx.FileExists(xProtocGenGo))
|
||||
}
|
||||
|
||||
func TestLookPath(t *testing.T) {
|
||||
@ -62,7 +62,7 @@ func TestLookPath(t *testing.T) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
assert.True(t, util.FileExists(xGo))
|
||||
assert.True(t, pathx.FileExists(xGo))
|
||||
}
|
||||
|
||||
func TestCanExec(t *testing.T) {
|
||||
|
@ -9,10 +9,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"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) {
|
||||
gitHome, err := GetGitHome()
|
||||
gitHome, err := pathx.GetGitHome()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"bufio"
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
@ -1,7 +1,7 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package util
|
||||
package pathx
|
||||
|
||||
func ReadLink(name string) (string, error) {
|
||||
return name, nil
|
@ -1,7 +1,7 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package util
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"os"
|
@ -7,6 +7,7 @@ import (
|
||||
"text/template"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
const regularPerm = 0o666
|
||||
@ -39,7 +40,7 @@ func (t *DefaultTemplate) GoFmt(format bool) *DefaultTemplate {
|
||||
|
||||
// SaveTo writes the codes to the target path
|
||||
func (t *DefaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
||||
if FileExists(path) && !forceUpdate {
|
||||
if pathx.FileExists(path) && !forceUpdate {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user