gomod.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package ctx
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "github.com/tal-tech/go-zero/core/jsonx"
  7. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  8. )
  9. type Module struct {
  10. Path string
  11. Main bool
  12. Dir string
  13. GoMod string
  14. GoVersion string
  15. }
  16. // projectFromGoMod is used to find the go module and project file path
  17. // the workDir flag specifies which folder we need to detect based on
  18. // only valid for go mod project
  19. func projectFromGoMod(workDir string) (*ProjectContext, error) {
  20. if len(workDir) == 0 {
  21. return nil, errors.New("the work directory is not found")
  22. }
  23. if _, err := os.Stat(workDir); err != nil {
  24. return nil, err
  25. }
  26. data, err := execx.Run("go list -json -m", workDir)
  27. if err != nil {
  28. return nil, err
  29. }
  30. var m Module
  31. err = jsonx.Unmarshal([]byte(data), &m)
  32. if err != nil {
  33. return nil, err
  34. }
  35. var ret ProjectContext
  36. ret.WorkDir = workDir
  37. ret.Name = filepath.Base(m.Dir)
  38. ret.Dir = m.Dir
  39. ret.Path = m.Path
  40. return &ret, nil
  41. }