diff --git a/cmd/govulncheck_compare/govulncheck_compare.go b/cmd/govulncheck_compare/govulncheck_compare.go index 6a9b80a..9fb0a57 100644 --- a/cmd/govulncheck_compare/govulncheck_compare.go +++ b/cmd/govulncheck_compare/govulncheck_compare.go @@ -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") @@ -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)