1 | /* Vuls - Vulnerability Scanner |
||
2 | Copyright (C) 2016 Future Corporation , Japan. |
||
3 | |||
4 | This program is free software: you can redistribute it and/or modify |
||
5 | it under the terms of the GNU General Public License as published by |
||
6 | the Free Software Foundation, either version 3 of the License, or |
||
7 | (at your option) any later version. |
||
8 | |||
9 | This program is distributed in the hope that it will be useful, |
||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | GNU General Public License for more details. |
||
13 | |||
14 | You should have received a copy of the GNU General Public License |
||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | package report |
||
19 | |||
20 | import ( |
||
21 | "bytes" |
||
22 | "encoding/json" |
||
23 | "encoding/xml" |
||
24 | "io/ioutil" |
||
25 | "os" |
||
26 | "path/filepath" |
||
27 | |||
28 | c "github.com/future-architect/vuls/config" |
||
29 | "github.com/future-architect/vuls/models" |
||
30 | "golang.org/x/xerrors" |
||
31 | ) |
||
32 | |||
33 | // LocalFileWriter writes results to a local file. |
||
34 | type LocalFileWriter struct { |
||
35 | CurrentDir string |
||
36 | } |
||
37 | |||
38 | func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) { |
||
39 | if c.Conf.FormatOneLineText { |
||
40 | path := filepath.Join(w.CurrentDir, "summary.txt") |
||
41 | text := formatOneLineSummary(rs...) |
||
42 | if err := writeFile(path, []byte(text), 0600); err != nil { |
||
43 | return xerrors.Errorf( |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
44 | "Failed to write to file. path: %s, err: %w", |
||
45 | path, err) |
||
46 | } |
||
47 | } |
||
48 | |||
49 | for _, r := range rs { |
||
50 | path := filepath.Join(w.CurrentDir, r.ReportFileName()) |
||
51 | |||
52 | if c.Conf.FormatJSON { |
||
53 | var p string |
||
54 | if c.Conf.Diff { |
||
55 | p = path + "_diff.json" |
||
56 | } else { |
||
57 | p = path + ".json" |
||
58 | } |
||
59 | |||
60 | var b []byte |
||
61 | if c.Conf.Debug { |
||
62 | if b, err = json.MarshalIndent(r, "", " "); err != nil { |
||
63 | return xerrors.Errorf("Failed to Marshal to JSON: %w", err) |
||
0 ignored issues
–
show
|
|||
64 | } |
||
65 | } else { |
||
66 | if b, err = json.Marshal(r); err != nil { |
||
67 | return xerrors.Errorf("Failed to Marshal to JSON: %w", err) |
||
0 ignored issues
–
show
|
|||
68 | } |
||
69 | } |
||
70 | if err := writeFile(p, b, 0600); err != nil { |
||
71 | return xerrors.Errorf("Failed to write JSON. path: %s, err: %w", p, err) |
||
0 ignored issues
–
show
|
|||
72 | } |
||
73 | } |
||
74 | |||
75 | if c.Conf.FormatList { |
||
76 | var p string |
||
77 | if c.Conf.Diff { |
||
78 | p = path + "_short_diff.txt" |
||
79 | } else { |
||
80 | p = path + "_short.txt" |
||
81 | } |
||
82 | |||
83 | if err := writeFile( |
||
84 | p, []byte(formatList(r)), 0600); err != nil { |
||
85 | return xerrors.Errorf( |
||
0 ignored issues
–
show
|
|||
86 | "Failed to write text files. path: %s, err: %w", p, err) |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if c.Conf.FormatFullText { |
||
91 | var p string |
||
92 | if c.Conf.Diff { |
||
93 | p = path + "_full_diff.txt" |
||
94 | } else { |
||
95 | p = path + "_full.txt" |
||
96 | } |
||
97 | |||
98 | if err := writeFile( |
||
99 | p, []byte(formatFullPlainText(r)), 0600); err != nil { |
||
100 | return xerrors.Errorf( |
||
0 ignored issues
–
show
|
|||
101 | "Failed to write text files. path: %s, err: %w", p, err) |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if c.Conf.FormatXML { |
||
106 | var p string |
||
107 | if c.Conf.Diff { |
||
108 | p = path + "_diff.xml" |
||
109 | } else { |
||
110 | p = path + ".xml" |
||
111 | } |
||
112 | |||
113 | var b []byte |
||
114 | if b, err = xml.Marshal(r); err != nil { |
||
115 | return xerrors.Errorf("Failed to Marshal to XML: %w", err) |
||
0 ignored issues
–
show
|
|||
116 | } |
||
117 | allBytes := bytes.Join([][]byte{[]byte(xml.Header + vulsOpenTag), b, []byte(vulsCloseTag)}, []byte{}) |
||
118 | if err := writeFile(p, allBytes, 0600); err != nil { |
||
119 | return xerrors.Errorf("Failed to write XML. path: %s, err: %w", p, err) |
||
0 ignored issues
–
show
|
|||
120 | } |
||
121 | } |
||
122 | } |
||
123 | return nil |
||
124 | } |
||
125 | |||
126 | func writeFile(path string, data []byte, perm os.FileMode) error { |
||
127 | var err error |
||
128 | if c.Conf.GZIP { |
||
129 | if data, err = gz(data); err != nil { |
||
130 | return err |
||
131 | } |
||
132 | path += ".gz" |
||
133 | } |
||
134 | return ioutil.WriteFile(path, []byte(data), perm) |
||
135 | } |
||
136 |