-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
47 lines (38 loc) · 1.04 KB
/
repository.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func detectRootModule() (ModuleName, error) {
data, err := os.ReadFile("go.mod")
if err != nil {
return "", fmt.Errorf("could not read go.mod file: %w", err)
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "module ") {
return ModuleName(strings.TrimPrefix(line, "module ")), nil
}
}
return "", errors.New("missing module name in go.mod file")
}
func parseGoModGraph() (map[ModuleName][]ModuleName, error) {
output, err := exec.Command("go", "mod", "graph").Output()
if err != nil {
return nil, fmt.Errorf("could not run `go mod graph` command: %w", err)
}
modules := make(map[ModuleName][]ModuleName)
lines := strings.Split(string(output), "\n")
for _, line := range lines {
splits := strings.Split(line, " ")
if len(splits) == 2 {
module, dep := ModuleName(splits[0]), ModuleName(splits[1])
modules[module] = append(modules[module], dep)
modules[dep] = modules[dep]
}
}
return modules, nil
}