-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.go
45 lines (39 loc) · 1014 Bytes
/
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
package gobump
import (
"fmt"
"os"
"path/filepath"
"golang.org/x/mod/modfile"
)
// ParseModules parses go modules in the go.mod of the Go module. This function
// returns the module path and a list of direct dependency module paths declared
// using "require" directive.
//
// For more info on the go.mod file structure refer to this resource:
// https://go.dev/doc/modules/gomod-ref.
func ParseModules(moduleDir string) (string, []string, error) {
p := filepath.Join(moduleDir, "go.mod")
bb, err := os.ReadFile(p)
if err != nil {
return "", nil, fmt.Errorf(
"failed to open go.mod file in %q directory: %s",
moduleDir,
err,
)
}
mf, err := modfile.Parse(p, bb, nil)
if err != nil {
return "", nil, fmt.Errorf(
"invalid go.mod file in %q directory: %s",
moduleDir,
err,
)
}
var directRequires []string
for _, req := range mf.Require {
if !req.Indirect {
directRequires = append(directRequires, req.Mod.Path)
}
}
return mf.Module.Mod.Path, directRequires, nil
}