Skip to content

Commit

Permalink
cmd/govulncheck_compare: simplify run
Browse files Browse the repository at this point in the history
Breaks running each comparison into its own function.

Change-Id: I3afd950b35bb6bf59d5d2848cccc80e7b98e3f39
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/613155
Reviewed-by: Zvonimir Pavlinovic <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
timothy-king committed Sep 16, 2024
1 parent a76cee4 commit 6e8a179
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions cmd/govulncheck_compare/govulncheck_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,11 @@ func run(w io.Writer, args []string) {
FindingsForMod: make(map[string]*govulncheck.ComparePair),
}
for _, binary := range binaries {
pair := &govulncheck.ComparePair{}
response.FindingsForMod[binary.ImportPath] = pair

if binary.Error != nil {
pair.Error = binary.Error.Error()
continue // there was an error in building the binary
}

srcResp, err := govulncheck.RunGovulncheckCmd(govulncheckPath, govulncheck.FlagSource, binary.ImportPath, modulePath, vulndbPath)
pair, err := runComparison(binary, govulncheckPath, modulePath, vulndbPath)
if err != nil {
pair.Error = err.Error()
continue
pair = &govulncheck.ComparePair{Error: err.Error()}
}
pair.SourceResults = *srcResp

binResp, err := govulncheck.RunGovulncheckCmd(govulncheckPath, govulncheck.FlagBinary, binary.BinaryPath, modulePath, vulndbPath)
if err != nil {
pair.Error = err.Error()
continue
}
pair.BinaryResults = *binResp
pair.BinaryResults.Stats.BuildTime = binary.BuildTime
response.FindingsForMod[binary.ImportPath] = pair
}

b, err := json.MarshalIndent(response, "", "\t")
Expand All @@ -91,6 +74,30 @@ func run(w io.Writer, args []string) {
fmt.Println()
}

// runComparison runs both a source mode and an binary mode comparison,
// and returns a govulncheck.ComparePair on success. Otherwise, returns an error.
func runComparison(binary *buildbinary.BinaryInfo, govulncheckPath, modulePath, vulndbPath string) (*govulncheck.ComparePair, error) {
if binary.Error != nil { // there was an error in building the binary
return nil, binary.Error
}

srcResp, err := govulncheck.RunGovulncheckCmd(govulncheckPath, govulncheck.FlagSource, binary.ImportPath, modulePath, vulndbPath)
if err != nil {
return nil, err
}
binResp, err := govulncheck.RunGovulncheckCmd(govulncheckPath, govulncheck.FlagBinary, binary.BinaryPath, modulePath, vulndbPath)
if err != nil {
return nil, err
}

pair := &govulncheck.ComparePair{
SourceResults: *srcResp,
BinaryResults: *binResp,
}
pair.BinaryResults.Stats.BuildTime = binary.BuildTime
return pair, nil
}

func removeBinaries(binaryPaths []*buildbinary.BinaryInfo) {
for _, bin := range binaryPaths {
os.Remove(bin.BinaryPath)
Expand Down

0 comments on commit 6e8a179

Please sign in to comment.