refactor(report): format-csv (#1072)

This commit is contained in:
Kota Kanbe
2020-11-05 21:10:35 +09:00
committed by GitHub
parent ebd3834a35
commit 75fceff5f7
2 changed files with 11 additions and 22 deletions

View File

@@ -98,17 +98,12 @@ func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) {
}
if c.Conf.FormatCsvList {
var p string
p := path + "_short.csv"
if c.Conf.Diff {
p = path + "_short_diff.csv"
} else {
p = path + "_short.csv"
}
err := formatCsvList(r, p)
if err == "" {
return xerrors.Errorf("Failed to write CSV. path: %s", p)
if err := formatCsvList(r, p); err != nil {
return xerrors.Errorf("Failed to write CSV: %s, %w", p, err)
}
}

View File

@@ -383,9 +383,8 @@ No CVE-IDs are found in updatable packages.
return
}
func formatCsvList(r models.ScanResult, path string) string {
func formatCsvList(r models.ScanResult, path string) error {
data := [][]string{{"CVE-ID", "CVSS", "Attack", "PoC", "CERT", "Fixed", "NVD"}}
for _, vinfo := range r.ScannedCves.ToSortedSlice() {
max := vinfo.MaxCvssScore().Value.Score
@@ -404,28 +403,23 @@ func formatCsvList(r models.ScanResult, path string) string {
data = append(data, []string{
vinfo.CveID,
fmt.Sprintf("%4.1f", max),
fmt.Sprintf("%s", vinfo.AttackVector()),
vinfo.AttackVector(),
exploits,
vinfo.AlertDict.FormatSource(),
fmt.Sprintf("%s", vinfo.PatchStatus(r.Packages)),
vinfo.PatchStatus(r.Packages),
link,
})
}
file, err := os.Create(path)
if err != nil {
return fmt.Sprintf("Unable to create file: %s", err)
return xerrors.Errorf("Failed to create a file: %s, err: %w", path, err)
}
defer file.Close()
writer := csv.NewWriter(file)
err = writer.WriteAll(data)
if err != nil {
return fmt.Sprintf("Cannot write to file: %s", err)
if err := csv.NewWriter(file).WriteAll(data); err != nil {
return xerrors.Errorf("Failed to write to file: %s, err: %w", path, err)
}
return fmt.Sprintf("%s", data)
return nil
}
func cweURL(cweID string) string {