/
GetMods.go
61 lines (49 loc) · 1.23 KB
/
GetMods.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
package util
import (
"path/filepath"
"runtime/debug"
"strings"
)
// ReadBuildInfo is a copy of debug.ReadBuildInfo to ease mocking during test
// for GetMods.
//
// This package uses this util.ReadBuildInfo insetead of debug.ReadBuildInfo.
var ReadBuildInfo = debug.ReadBuildInfo
// GetMods returns a list of external modules used in the package.
// The list contains: module name, path and the version.
func GetMods() []map[string]string {
mods := getModuleInfo()
modsFound := []map[string]string{}
if len(mods) == 0 {
dummyMod := &debug.Module{
Path: "n/a",
Version: "n/a",
Sum: "n/a",
}
mods = []*debug.Module{
dummyMod,
}
}
for _, modDep := range mods {
modsFound = append(modsFound, map[string]string{
"name": getModName(modDep),
"path": modDep.Path,
"version": modDep.Version,
})
}
return modsFound
}
func getModName(modDep *debug.Module) string {
result := filepath.Base(modDep.Path)
if chunk := strings.SplitN(modDep.Path, "/", 3); len(chunk) > 2 {
result = chunk[2] // Get the 3rd chunk
}
return result
}
func getModuleInfo() []*debug.Module {
debugModules := []*debug.Module{}
if buildInfo, ok := ReadBuildInfo(); ok {
debugModules = buildInfo.Deps
}
return debugModules
}