refactor file|path (#1409)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-01-03 21:32:40 +08:00
committed by GitHub
parent 290de6aa96
commit 89ce5e492b
81 changed files with 279 additions and 245 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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) {

View File

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

View File

@@ -1,4 +1,4 @@
package util
package pathx
import (
"bufio"

View File

@@ -1,4 +1,4 @@
package util
package pathx
import (
"io/ioutil"

View File

@@ -1,4 +1,4 @@
package util
package pathx
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package util
package pathx
import (
"io/ioutil"

View File

@@ -1,7 +1,7 @@
//go:build windows
// +build windows
package util
package pathx
func ReadLink(name string) (string, error) {
return name, nil

View File

@@ -1,7 +1,7 @@
//go:build linux || darwin
// +build linux darwin
package util
package pathx
import (
"os"

View File

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