mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
@@ -51,6 +51,10 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := UpdateGoWorkIfExist(workDir); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
m, err := getRealModule(workDir, execx.Run)
|
m, err := getRealModule(workDir, execx.Run)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
33
tools/goctl/util/ctx/gowork.go
Normal file
33
tools/goctl/util/ctx/gowork.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package ctx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UpdateGoWorkIfExist updates go work if workDir is in a go workspace
|
||||||
|
func UpdateGoWorkIfExist(workDir string) error {
|
||||||
|
if isGoWork, err := isGoWork(workDir); !isGoWork || err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := execx.Run("go work use .", workDir)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// isGoWork detect if the workDir is in a go workspace
|
||||||
|
func isGoWork(workDir string) (bool, error) {
|
||||||
|
if len(workDir) == 0 {
|
||||||
|
return false, errors.New("the work directory is not found")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(workDir); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
goWorkPath, err := execx.Run("go env GOWORK", workDir)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return len(goWorkPath) > 0, nil
|
||||||
|
}
|
||||||
40
tools/goctl/util/ctx/gowork_test.go
Normal file
40
tools/goctl/util/ctx/gowork_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package ctx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/core/stringx"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_isGoWork(t *testing.T) {
|
||||||
|
dir := filepath.Join("/tmp", stringx.Rand())
|
||||||
|
|
||||||
|
err := pathx.MkdirIfNotExist(dir)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
gowork, err := isGoWork(dir)
|
||||||
|
assert.False(t, gowork)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
_, err = execx.Run("go work init", dir)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
gowork, err = isGoWork(dir)
|
||||||
|
assert.True(t, gowork)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
subDir := filepath.Join(dir, stringx.Rand())
|
||||||
|
err = pathx.MkdirIfNotExist(subDir)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
gowork, err = isGoWork(subDir)
|
||||||
|
assert.True(t, gowork)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsGoMod is used to determine whether workDir is a go module project through command `go list -json -m`
|
// IsGoMod is used to determine whether workDir is a go module project through command `go env GOMOD`
|
||||||
func IsGoMod(workDir string) (bool, error) {
|
func IsGoMod(workDir string) (bool, error) {
|
||||||
if len(workDir) == 0 {
|
if len(workDir) == 0 {
|
||||||
return false, errors.New("the work directory is not found")
|
return false, errors.New("the work directory is not found")
|
||||||
@@ -16,8 +16,8 @@ func IsGoMod(workDir string) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := execx.Run("go list -m -f '{{.GoMod}}'", workDir)
|
data, err := execx.Run("go env GOMOD", workDir)
|
||||||
if err != nil || len(data) == 0 {
|
if err != nil || data == "/dev/null" {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user