internal/scan: change the way convert mode works

This changes convert mode to just modify the source of vulnerabilities
This means it builds the output and handles exit codes like normal.
This fixes the fact that convert does not obey -show flags, or return failure on vulnerabilities.

Change-Id: Ia264d0acbfe49f2ff814be2305e108f144a17d73
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/517156
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Maceo Thompson <maceothompson@google.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Auto-Submit: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/cmd/govulncheck/testdata/convert_text.ct b/cmd/govulncheck/testdata/convert_text.ct
index e12cbc2..5e4d460 100644
--- a/cmd/govulncheck/testdata/convert_text.ct
+++ b/cmd/govulncheck/testdata/convert_text.ct
@@ -1,6 +1,6 @@
 #####
 # Test using the conversion from json on stdin to text on stdout
-$ govulncheck -mode=convert < convert_input.json
+$ govulncheck -mode=convert < convert_input.json --> FAIL 3
 Scanning your code and P packages across M dependent modules for known vulnerabilities...
 
 Vulnerability #1: GO-2021-0265
diff --git a/internal/scan/run.go b/internal/scan/run.go
index 83bc88d..fa7fe37 100644
--- a/internal/scan/run.go
+++ b/internal/scan/run.go
@@ -27,9 +27,6 @@
 	if err := parseFlags(cfg, stderr, args); err != nil {
 		return err
 	}
-	if cfg.mode == modeConvert {
-		return convertJSONToText(r, stdout)
-	}
 
 	client, err := client.NewClient(cfg.db, nil)
 	if err != nil {
@@ -60,6 +57,8 @@
 		err = runBinary(ctx, handler, cfg, client)
 	case modeQuery:
 		err = runQuery(ctx, handler, cfg, client)
+	case modeConvert:
+		err = govulncheck.HandleJSON(r, handler)
 	}
 	if err != nil {
 		return err
@@ -132,14 +131,3 @@
 	}
 	cfg.ScannerVersion = buf.String()
 }
-
-// convertJSONToText converts r, which is expected to be the JSON output of govulncheck,
-// into the text output, and writes the output to w.
-func convertJSONToText(r io.Reader, w io.Writer) error {
-	h := NewTextHandler(w)
-	if err := govulncheck.HandleJSON(r, h); err != nil {
-		return err
-	}
-	Flush(h)
-	return nil
-}