-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(goctl): support go work (#4332)
- Loading branch information
Showing
4 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters