From 1d9f816ca5d8224320a72b8aefbdb6f5d3692da6 Mon Sep 17 00:00:00 2001 From: cosmincojocar Date: Mon, 5 Mar 2018 13:20:24 +0100 Subject: [PATCH] Add support for YAML output format (#177) * Add YAML output format * Update README --- Godeps/Godeps.json | 2 +- README.md | 2 +- cmd/gas/main.go | 2 +- output/formatter.go | 12 ++++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 7763c31..4b73dbf 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -296,7 +296,7 @@ }, { "ImportPath": "gopkg.in/yaml.v2", - "Rev": "eb3733d160e74a9c7e442f435eb3bea458e1d19f" + "Rev": "d670f9405373e636a5a2765eea47fac0c9bc91a4" } ] } diff --git a/README.md b/README.md index 7dace9e..d2c3bbc 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ $ gas -nosec=true ./... ### Output formats -Gas currently supports text, json, csv and JUnit XML output formats. By default +Gas currently supports text, json, yaml, csv and JUnit XML output formats. By default results will be reported to stdout, but can also be written to an output file. The output format is controlled by the '-fmt' flag, and the output file is controlled by the '-out' flag as follows: diff --git a/cmd/gas/main.go b/cmd/gas/main.go index 03dc30c..a04c151 100644 --- a/cmd/gas/main.go +++ b/cmd/gas/main.go @@ -59,7 +59,7 @@ var ( flagIgnoreNoSec = flag.Bool("nosec", false, "Ignores #nosec comments when set") // format output - flagFormat = flag.String("fmt", "text", "Set output format. Valid options are: json, csv, junit-xml, html, or text") + flagFormat = flag.String("fmt", "text", "Set output format. Valid options are: json, yaml, csv, junit-xml, html, or text") // output file flagOutput = flag.String("out", "", "Set output file for results") diff --git a/output/formatter.go b/output/formatter.go index d829c53..0485161 100644 --- a/output/formatter.go +++ b/output/formatter.go @@ -23,6 +23,7 @@ import ( plainTemplate "text/template" "github.com/GoASTScanner/gas" + "gopkg.in/yaml.v2" ) // ReportFormat enumrates the output format for reported issues @@ -72,6 +73,8 @@ func CreateReport(w io.Writer, format string, issues []*gas.Issue, metrics *gas. switch format { case "json": err = reportJSON(w, data) + case "yaml": + err = reportYAML(w, data) case "csv": err = reportCSV(w, data) case "junit-xml": @@ -99,6 +102,15 @@ func reportJSON(w io.Writer, data *reportInfo) error { return err } +func reportYAML(w io.Writer, data *reportInfo) error { + raw, err := yaml.Marshal(data) + if err != nil { + return err + } + _, err = w.Write(raw) + return err +} + func reportCSV(w io.Writer, data *reportInfo) error { out := csv.NewWriter(w) defer out.Flush()