forked from future-architect/vuls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalfile.go
95 lines (83 loc) · 2.21 KB
/
localfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package report
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
c "github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
"golang.org/x/xerrors"
)
// LocalFileWriter writes results to a local file.
type LocalFileWriter struct {
CurrentDir string
}
func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) {
if c.Conf.FormatOneLineText {
path := filepath.Join(w.CurrentDir, "summary.txt")
text := formatOneLineSummary(rs...)
if err := writeFile(path, []byte(text), 0600); err != nil {
return xerrors.Errorf(
"Failed to write to file. path: %s, err: %w",
path, err)
}
}
for _, r := range rs {
path := filepath.Join(w.CurrentDir, r.ReportFileName())
if c.Conf.FormatJSON {
p := path + ".json"
if c.Conf.DiffPlus || c.Conf.DiffMinus {
p = path + "_diff.json"
}
var b []byte
if b, err = json.MarshalIndent(r, "", " "); err != nil {
return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
}
if err := writeFile(p, b, 0600); err != nil {
return xerrors.Errorf("Failed to write JSON. path: %s, err: %w", p, err)
}
}
if c.Conf.FormatList {
p := path + "_short.txt"
if c.Conf.DiffPlus || c.Conf.DiffMinus {
p = path + "_short_diff.txt"
}
if err := writeFile(
p, []byte(formatList(r)), 0600); err != nil {
return xerrors.Errorf(
"Failed to write text files. path: %s, err: %w", p, err)
}
}
if c.Conf.FormatFullText {
p := path + "_full.txt"
if c.Conf.DiffPlus || c.Conf.DiffMinus {
p = path + "_full_diff.txt"
}
if err := writeFile(
p, []byte(formatFullPlainText(r)), 0600); err != nil {
return xerrors.Errorf(
"Failed to write text files. path: %s, err: %w", p, err)
}
}
if c.Conf.FormatCsvList {
p := path + ".csv"
if c.Conf.DiffPlus || c.Conf.DiffMinus {
p = path + "_diff.csv"
}
if err := formatCsvList(r, p); err != nil {
return xerrors.Errorf("Failed to write CSV: %s, %w", p, err)
}
}
}
return nil
}
func writeFile(path string, data []byte, perm os.FileMode) error {
var err error
if c.Conf.GZIP {
if data, err = gz(data); err != nil {
return err
}
path += ".gz"
}
return ioutil.WriteFile(path, []byte(data), perm)
}