/
modules.go
70 lines (59 loc) · 1.61 KB
/
modules.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
62
63
64
65
66
67
68
69
70
package astinfo
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// A Dir describes a directory holding code by specifying
// the expected import path and the file system directory.
type Dir struct {
importPath string // import path for that dir
dir string // file system directory
}
func findModuleRoots() ([]Dir, error) {
var list []Dir
// Check for use of modules by 'go env GOMOD',
// which reports a go.mod file path if modules are enabled.
stdout, _ := exec.Command("go", "env", "GOMOD").Output()
gomod := string(bytes.TrimSpace(stdout))
if gomod == "" || gomod == os.DevNull {
return nil, errors.Errorf("no go modules detected")
}
cmd := exec.Command("go", "list", "-m", "-f={{.Path}}\t{{.Dir}}", "all")
cmd.Stderr = os.Stderr
out, _ := cmd.Output()
for _, line := range strings.Split(string(out), "\n") {
i := strings.Index(line, "\t")
if i < 0 {
continue
}
path, dir := line[:i], line[i+1:]
if dir != "" {
list = append(list, Dir{importPath: path, dir: dir})
}
}
return list, nil
}
// findPackagePath finds package import path by absolute directory path.
func findPackagePath(pkgDir string) (string, error) {
dirs, err := findModuleRoots()
if err != nil {
return "", err
}
for _, root := range dirs {
if pkgDir == root.dir {
return root.importPath, nil
}
if strings.HasPrefix(pkgDir, root.dir+string(filepath.Separator)) {
suffix := filepath.ToSlash(pkgDir[len(root.dir)+1:])
if root.importPath == "" {
return suffix, nil
}
return root.importPath + "/" + suffix, nil
}
}
return "", errors.Errorf("not found")
}