gosec/output/formatter.go

117 lines
2.7 KiB
Go
Raw Normal View History

2016-07-20 11:02:01 +01:00
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"encoding/csv"
2016-07-26 00:39:55 +01:00
"encoding/json"
2016-10-18 06:36:35 +01:00
htmlTemplate "html/template"
2016-07-20 11:02:01 +01:00
"io"
"strconv"
2016-10-18 06:36:35 +01:00
plainTemplate "text/template"
2016-07-20 11:02:01 +01:00
gas "github.com/GoASTScanner/gas/core"
2016-07-20 11:02:01 +01:00
)
// The output format for reported issues
type ReportFormat int
const (
ReportText ReportFormat = iota // Plain text format
ReportJSON // Json format
ReportCSV // CSV format
)
var text = `Results:
{{ range $index, $issue := .Issues }}
[{{ $issue.File }}:{{ $issue.Line }}] - {{ $issue.What }} (Confidence: {{ $issue.Confidence}}, Severity: {{ $issue.Severity }})
> {{ $issue.Code }}
{{ end }}
Summary:
Files: {{.Stats.NumFiles}}
Lines: {{.Stats.NumLines}}
Nosec: {{.Stats.NumNosec}}
Issues: {{.Stats.NumFound}}
`
func CreateReport(w io.Writer, format string, data *gas.Analyzer) error {
2016-07-26 00:39:55 +01:00
var err error
2016-07-20 11:02:01 +01:00
switch format {
case "json":
2016-07-26 00:39:55 +01:00
err = reportJSON(w, data)
case "csv":
err = reportCSV(w, data)
2016-10-18 06:36:35 +01:00
case "html":
err = reportFromHTMLTemplate(w, html, data)
2016-07-20 11:02:01 +01:00
case "text":
2016-10-18 06:36:35 +01:00
err = reportFromPlaintextTemplate(w, text, data)
2016-07-20 11:02:01 +01:00
default:
2016-10-18 06:36:35 +01:00
err = reportFromPlaintextTemplate(w, text, data)
2016-07-26 00:39:55 +01:00
}
return err
}
func reportJSON(w io.Writer, data *gas.Analyzer) error {
raw, err := json.MarshalIndent(data, "", "\t")
if err != nil {
panic(err)
2016-07-20 11:02:01 +01:00
}
2016-07-26 00:39:55 +01:00
_, err = w.Write(raw)
if err != nil {
panic(err)
}
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
}
2016-10-18 06:36:35 +01:00
func reportFromPlaintextTemplate(w io.Writer, reportTemplate string, data *gas.Analyzer) error {
t, e := plainTemplate.New("gas").Parse(reportTemplate)
if e != nil {
return e
}
return t.Execute(w, data)
}
func reportFromHTMLTemplate(w io.Writer, reportTemplate string, data *gas.Analyzer) error {
t, e := htmlTemplate.New("gas").Parse(reportTemplate)
2016-07-20 11:02:01 +01:00
if e != nil {
return e
}
return t.Execute(w, data)
}