modcheck.go 547 B

12345678910111213141516171819202122232425
  1. package ctx
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
  6. )
  7. // IsGoMod is used to determine whether workDir is a go module project through command `go list -json -m`
  8. func IsGoMod(workDir string) (bool, error) {
  9. if len(workDir) == 0 {
  10. return false, errors.New("the work directory is not found")
  11. }
  12. if _, err := os.Stat(workDir); err != nil {
  13. return false, err
  14. }
  15. data, err := execx.Run("go list -m -f '{{.GoMod}}'", workDir)
  16. if err != nil || len(data) == 0 {
  17. return false, nil
  18. }
  19. return true, nil
  20. }