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.
This commit is contained in:
Sven Lechner
2019-09-06 02:55:03 +02:00
parent ca77711a94
commit 1a21c18fd4

View File

@@ -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)