-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.go
61 lines (51 loc) · 1.54 KB
/
mod.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
50
51
52
53
54
55
56
57
58
59
60
61
package pkgutil
import (
"encoding/json"
"os/exec"
"path/filepath"
"regexp"
"time"
"github.com/tsaikd/KDGoLib/futil"
)
// Module go mod info
type Module struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update, if any (with -u)
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
Error *ModuleError // error loading module
}
// ModuleError go mod error
type ModuleError struct {
Err string // the error itself
}
// IsGoModDir return true if dir contains go.mod file
func IsGoModDir(dir string) bool {
return futil.IsExist(filepath.Join(dir, "go.mod"))
}
var reJSONObj = regexp.MustCompile("{[^}]*}")
// ParseGoMod return go module and dep info in dir
func ParseGoMod(dir string) (result []Module, err error) {
cmd := exec.Command("go", "list", "-m", "-json", "all")
cmd.Dir = dir
goModData, err := cmd.Output()
if err != nil {
return
}
modsData := reJSONObj.FindAll(goModData, -1)
result = make([]Module, len(modsData))
for i, d := range modsData {
mod := Module{}
if err = json.Unmarshal(d, &mod); err != nil {
return []Module{}, err
}
result[i] = mod
}
return
}