mirror of
https://github.com/securego/gosec.git
synced 2024-12-24 11:35:52 +00:00
Merge pull request #22 from csstaub/cs/csv
Use encoding/csv for CSV output
This commit is contained in:
commit
e86addbfea
1 changed files with 23 additions and 11 deletions
|
@ -15,9 +15,11 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"io"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
gas "github.com/HewlettPackard/gas/core"
|
||||
)
|
||||
|
@ -45,22 +47,13 @@ Summary:
|
|||
|
||||
`
|
||||
|
||||
var csv = `{{ range $index, $issue := .Issues -}}
|
||||
{{- $issue.File -}},
|
||||
{{- $issue.Line -}},
|
||||
{{- $issue.What -}},
|
||||
{{- $issue.Severity -}},
|
||||
{{- $issue.Confidence -}},
|
||||
{{- printf "%q" $issue.Code }}
|
||||
{{ end }}`
|
||||
|
||||
func CreateReport(w io.Writer, format string, data *gas.Analyzer) error {
|
||||
var err error
|
||||
switch format {
|
||||
case "json":
|
||||
err = reportJSON(w, data)
|
||||
case "csv":
|
||||
err = reportFromTemplate(w, csv, data)
|
||||
err = reportCSV(w, data)
|
||||
case "text":
|
||||
err = reportFromTemplate(w, text, data)
|
||||
default:
|
||||
|
@ -82,6 +75,25 @@ func reportJSON(w io.Writer, data *gas.Analyzer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func reportCSV(w io.Writer, data *gas.Analyzer) error {
|
||||
out := csv.NewWriter(w)
|
||||
defer out.Flush()
|
||||
for _, issue := range data.Issues {
|
||||
err := out.Write([]string{
|
||||
issue.File,
|
||||
strconv.Itoa(issue.Line),
|
||||
issue.What,
|
||||
issue.Severity.String(),
|
||||
issue.Confidence.String(),
|
||||
issue.Code,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func reportFromTemplate(w io.Writer, reportTemplate string, data *gas.Analyzer) error {
|
||||
t, e := template.New("gas").Parse(reportTemplate)
|
||||
if e != nil {
|
||||
|
|
Loading…
Reference in a new issue