forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomod.go
49 lines (43 loc) · 1.05 KB
/
gomod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ctx
import (
"errors"
"os"
"path/filepath"
"github.com/melonwool/go-zero/core/jsonx"
"github.com/melonwool/go-zero/tools/goctl/rpc/execx"
)
// Module contains the relative data of go module,
// which is the result of the command go list
type Module struct {
Path string
Main bool
Dir string
GoMod string
GoVersion string
}
// projectFromGoMod is used to find the go module and project file path
// the workDir flag specifies which folder we need to detect based on
// only valid for go mod project
func projectFromGoMod(workDir string) (*ProjectContext, error) {
if len(workDir) == 0 {
return nil, errors.New("the work directory is not found")
}
if _, err := os.Stat(workDir); err != nil {
return nil, err
}
data, err := execx.Run("go list -json -m", workDir)
if err != nil {
return nil, err
}
var m Module
err = jsonx.Unmarshal([]byte(data), &m)
if err != nil {
return nil, err
}
var ret ProjectContext
ret.WorkDir = workDir
ret.Name = filepath.Base(m.Dir)
ret.Dir = m.Dir
ret.Path = m.Path
return &ret, nil
}