From 1a21c18fd4a32f85e8395724e7afbf30755505b7 Mon Sep 17 00:00:00 2001 From: Sven Lechner Date: Fri, 6 Sep 2019 02:55:03 +0200 Subject: [PATCH] add json flag to output results as json This commit adds the ability to output the check results as json data. This functionality is intended for scripting purposes. Therefore the binary will always exit with code 0. The threshold flag is also ignored when '-j' has been specified. --- cmd/goreportcard-cli/main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/goreportcard-cli/main.go b/cmd/goreportcard-cli/main.go index db1785f..cb57c46 100644 --- a/cmd/goreportcard-cli/main.go +++ b/cmd/goreportcard-cli/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "encoding/json" "log" "os" @@ -13,6 +14,7 @@ var ( dir = flag.String("d", ".", "Root directory of your Go application") verbose = flag.Bool("v", false, "Verbose output") th = flag.Float64("t", 0, "Threshold of failure command") + jsn = flag.Bool("j", false, "JSON output. The binary will always exit with code 0") ) func main() { @@ -23,6 +25,12 @@ func main() { log.Fatalf("Fatal error checking %s: %s", *dir, err.Error()) } + if *jsn { + marshalledResults, _ := json.Marshal(result) + fmt.Println(string(marshalledResults)) + os.Exit(0) + } + fmt.Printf("Grade: %s (%.1f%%)\n", result.Grade, result.Average*100) fmt.Printf("Files: %d\n", result.Files) fmt.Printf("Issues: %d\n", result.Issues)