-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgopackages.go
60 lines (47 loc) · 1.14 KB
/
gopackages.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
package gopackages
import (
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/mod/modfile"
"golang.org/x/xerrors"
)
var (
NotFound = xerrors.New("not found")
PkgNameNotFound = xerrors.New("package name was not found")
)
// GetGoModPath - Search for go.mod by breadth-first-search directly under the Git repository
func GetGoModPath(in string) (string, error) {
base := filepath.Clean(in)
prev := base
for {
goMod := filepath.Join(base, "go.mod")
_, err := os.Stat(goMod)
if err == nil {
return goMod, nil
}
base, err = filepath.Abs(filepath.Join(base, ".."))
if err != nil {
return "", NotFound
}
if prev == base {
return "", NotFound
}
prev = base
}
}
// GetGoModule - Get the Go root package name from go.mod
func GetGoModule(goMod string) (string, error) {
d, err := ioutil.ReadFile(goMod)
if err != nil {
return "", xerrors.Errorf("error in ioutil ReadFile method: %w", err)
}
f, err := modfile.Parse("", d, nil)
if err != nil {
return "", xerrors.Errorf("failed to mod file parsed: %w", err)
}
if len(f.Module.Mod.Path) == 0 {
return "", PkgNameNotFound
}
return f.Module.Mod.Path, nil
}