-
Notifications
You must be signed in to change notification settings - Fork 432
/
parse.go
45 lines (37 loc) · 1.03 KB
/
parse.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 modfile
import (
"encoding/json"
"os"
"os/exec"
)
type GoPackage struct {
Path string `json:"Path"`
Version string `json:"Version"`
Indirect bool `json:"Indirect"`
}
type ReplacedGoPackage struct {
Old GoPackage `json:"Old"`
New GoPackage `json:"New"`
}
type AllPackages struct {
Go string `json:"Go"`
Require []GoPackage `json:"Require"`
Replace []ReplacedGoPackage `json:"Replace"`
}
func GetCmdOutput(cmd []string) ([]byte, error) {
command := exec.Command(cmd[0], cmd[1:]...)
command.Stderr = os.Stderr
return command.Output()
}
// Parse returns the go.mod dependencies in a structured format
// It is an alternative to using https://pkg.go.dev/golang.org/x/mod which frequently
// contains CVEs and must be updated
func Parse() (AllPackages, error) {
allPackages := AllPackages{}
depList, err := GetCmdOutput([]string{"go", "mod", "edit", "-json"})
if err != nil {
return allPackages, err
}
unmarshalErr := json.Unmarshal(depList, &allPackages)
return allPackages, unmarshalErr
}