modcheck.go 668 B

1234567891011121314151617181920212223242526272829303132
  1. package ctx
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/tal-tech/go-zero/core/jsonx"
  6. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  7. )
  8. // IsGoMod is used to determine whether workDir is a go module project through command `go list -json -m`
  9. func IsGoMod(workDir string) (bool, error) {
  10. if len(workDir) == 0 {
  11. return false, errors.New("the work directory is not found")
  12. }
  13. if _, err := os.Stat(workDir); err != nil {
  14. return false, err
  15. }
  16. data, err := execx.Run("go list -json -m", workDir)
  17. if err != nil {
  18. return false, nil
  19. }
  20. var m Module
  21. err = jsonx.Unmarshal([]byte(data), &m)
  22. if err != nil {
  23. return false, err
  24. }
  25. return len(m.GoMod) > 0, nil
  26. }