Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(goctl): support go work (#4332) #4344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tools/goctl/util/ctx/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
return nil, err
}

if err := UpdateGoWorkIfExist(workDir); err != nil {
return nil, err
}

m, err := getRealModule(workDir, execx.Run)
if err != nil {
return nil, err
Expand Down
33 changes: 33 additions & 0 deletions tools/goctl/util/ctx/gowork.go
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions tools/goctl/util/ctx/gowork_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions tools/goctl/util/ctx/modcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"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) {
if len(workDir) == 0 {
return false, errors.New("the work directory is not found")
Expand All @@ -16,8 +16,8 @@ func IsGoMod(workDir string) (bool, error) {
return false, err
}

data, err := execx.Run("go list -m -f '{{.GoMod}}'", workDir)
if err != nil || len(data) == 0 {
data, err := execx.Run("go env GOMOD", workDir)
if err != nil || data == "/dev/null" {
return false, nil
}

Expand Down