-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelpers.go
More file actions
58 lines (52 loc) · 1.01 KB
/
helpers.go
File metadata and controls
58 lines (52 loc) · 1.01 KB
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
package structgraph
import (
"bytes"
"errors"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
)
var modTmp string
func getModBase() (modBase string, err error) {
modpath := getGoModFilePath()
if len(modpath) == 0 {
err = errors.New("invalid go mod env")
return
}
mb, err := ioutil.ReadFile(modpath)
if err != nil {
return
}
f, err := modfile.Parse("", mb, func(path, version string) (s string, e error) {
return version, nil
})
if err != nil {
return
}
if f.Module == nil {
err = errors.New("parse mod error,please check your go env")
return
}
modBase = f.Module.Mod.Path
return
}
func getGoModDir() (modPath string) {
mod := getGoModFilePath()
modPath, _ = filepath.Split(mod)
return
}
func getGoModFilePath() (modPath string) {
if len(modTmp) > 0 {
return modTmp
}
cmd := exec.Command("go", "env", "GOMOD")
stdout := &bytes.Buffer{}
cmd.Stdout = stdout
_ = cmd.Run()
mod := stdout.String()
mod = strings.Trim(mod, "\n")
modTmp = mod
return mod
}