internal/scan: move exit code handling into the error
Change-Id: I2ab507685df36190e218ff49defd1aed8edea860
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/487976
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Julie Qiu <julieqiu@google.com>
diff --git a/cmd/govulncheck/main.go b/cmd/govulncheck/main.go
index eee8aa1..f166323 100644
--- a/cmd/govulncheck/main.go
+++ b/cmd/govulncheck/main.go
@@ -6,7 +6,6 @@
import (
"context"
- "flag"
"fmt"
"os"
@@ -16,19 +15,12 @@
func main() {
ctx := context.Background()
err := scan.Command(ctx, os.Args[1:]...).Run()
- if err != nil {
- switch err {
- case flag.ErrHelp:
- os.Exit(0)
- case scan.ErrVulnerabilitiesFound:
- os.Exit(3)
- case scan.ErrNoPatterns:
- // flag.Usage is printed in the case of this error, so do not print
- // the actual error message.
- os.Exit(1)
- default:
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
+ switch err := err.(type) {
+ case nil:
+ case interface{ ExitCode() int }:
+ os.Exit(err.ExitCode())
+ default:
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
}
}
diff --git a/cmd/govulncheck/main_command_118_test.go b/cmd/govulncheck/main_command_118_test.go
index 804d55f..e4f9ae0 100644
--- a/cmd/govulncheck/main_command_118_test.go
+++ b/cmd/govulncheck/main_command_118_test.go
@@ -128,7 +128,7 @@
if err != nil {
return nil, err
}
- ts.DisableLogging = false
+ ts.DisableLogging = true
ts.Commands["govulncheck"] = func(args []string, inputFile string) ([]byte, error) {
govulndbURI, err := web.URLFromFilePath(vulndbDir)
if err != nil {
@@ -157,7 +157,7 @@
heapGoRegexp = regexp.MustCompile(`heap\.go:(\d+)`)
progressRegexp = regexp.MustCompile(`Scanning your code and (\d+) packages across (\d+)`)
govulncheckRegexp = regexp.MustCompile(`govulncheck@v(.*) with`)
- govulncheckBinaryErrorRegexp = regexp.MustCompile(`govulncheck: (.*) is a file`)
+ govulncheckBinaryErrorRegexp = regexp.MustCompile(`"([^"]*") is a file`)
govulncheckJSONRegexp = regexp.MustCompile(`"govulncheck@v(.*)",`)
vulndbRegexp = regexp.MustCompile(`file:///(.*)/testdata/vulndb`)
gorootRegexp = regexp.MustCompile(`package (.*) is not in GOROOT (.*)`)
diff --git a/cmd/govulncheck/testdata/badmode.ct b/cmd/govulncheck/testdata/badmode.ct
index a435b42..3b94e00 100644
--- a/cmd/govulncheck/testdata/badmode.ct
+++ b/cmd/govulncheck/testdata/badmode.ct
@@ -1,4 +1,4 @@
# Test of invalid input to -mode
$ govulncheck -mode=invalid ./... --> FAIL 1
-govulncheck: "invalid" is not a valid mode
+"invalid" is not a valid mode
diff --git a/cmd/govulncheck/testdata/binarybadfile.ct b/cmd/govulncheck/testdata/binarybadfile.ct
index d742414..965aa54 100644
--- a/cmd/govulncheck/testdata/binarybadfile.ct
+++ b/cmd/govulncheck/testdata/binarybadfile.ct
@@ -1,4 +1,4 @@
# Test of passing a non-file to -mode=binary
$ govulncheck -mode=binary notafile --> FAIL 1
-govulncheck: "notafile" is not a file
+"notafile" is not a file
diff --git a/cmd/govulncheck/testdata/binarymulti.ct b/cmd/govulncheck/testdata/binarymulti.ct
index e060de1..de38d29 100644
--- a/cmd/govulncheck/testdata/binarymulti.ct
+++ b/cmd/govulncheck/testdata/binarymulti.ct
@@ -1,4 +1,4 @@
# Test of trying to analyze multiple binaries
$ govulncheck -mode=binary ${vuln_binary} ${vuln_binary} --> FAIL 1
-govulncheck: only 1 binary can be analyzed at a time
+only 1 binary can be analyzed at a time
diff --git a/cmd/govulncheck/testdata/binarynotags.ct b/cmd/govulncheck/testdata/binarynotags.ct
index 0a9dab3..8e2df8a 100644
--- a/cmd/govulncheck/testdata/binarynotags.ct
+++ b/cmd/govulncheck/testdata/binarynotags.ct
@@ -1,4 +1,4 @@
# Test of trying to run -mode=binary with -tags flag
$ govulncheck -tags=foo -mode=binary ${vuln_binary} --> FAIL 1
-govulncheck: the -tags flag is not supported in binary mode
+the -tags flag is not supported in binary mode
diff --git a/cmd/govulncheck/testdata/binarynotest.ct b/cmd/govulncheck/testdata/binarynotest.ct
index 15fda5b..bd5ade8 100644
--- a/cmd/govulncheck/testdata/binarynotest.ct
+++ b/cmd/govulncheck/testdata/binarynotest.ct
@@ -1,4 +1,4 @@
# Test of trying to run -mode=binary with the -test flag
$ govulncheck -test -mode=binary ${vuln_binary} --> FAIL 1
-govulncheck: the -test flag is not supported in binary mode
+the -test flag is not supported in binary mode
diff --git a/cmd/govulncheck/testdata/binarynoverbose.ct b/cmd/govulncheck/testdata/binarynoverbose.ct
index b3cf767..27168fd 100644
--- a/cmd/govulncheck/testdata/binarynoverbose.ct
+++ b/cmd/govulncheck/testdata/binarynoverbose.ct
@@ -1,4 +1,4 @@
# Test of trying to run -mode=binary with -v flag
$ govulncheck -v -mode=binary ${vuln_binary} --> FAIL 1
-govulncheck: the -v flag is not supported in binary mode
+the -v flag is not supported in binary mode
diff --git a/cmd/govulncheck/testdata/jsonnoverbose.ct b/cmd/govulncheck/testdata/jsonnoverbose.ct
index 12e7f02..1cf3628 100644
--- a/cmd/govulncheck/testdata/jsonnoverbose.ct
+++ b/cmd/govulncheck/testdata/jsonnoverbose.ct
@@ -1,4 +1,4 @@
# Test of trying to run -json with -v flag
$ govulncheck -C ${moddir}/vuln -v -json . --> FAIL 1
-govulncheck: the -v flag is not supported for JSON output
+the -v flag is not supported for JSON output