-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVlanLister_writers.go
359 lines (324 loc) · 9.56 KB
/
VlanLister_writers.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
/*
#### ## ## ######## ####### ######## ######## ######
## ### ### ## ## ## ## ## ## ## ## ##
## #### #### ## ## ## ## ## ## ## ##
## ## ### ## ######## ## ## ######## ## ######
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
#### ## ## ## ####### ## ## ## ######
*/
import (
"bufio"
"compress/gzip"
"fmt"
"os"
"strings"
"time"
excelize "github.com/360EntSecGroup-Skylar/excelize/v2"
)
/*
## ## ### ######## ######
## ## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ## ## ## ######## ######
## ## ######### ## ## ##
## ## ## ## ## ## ## ##
### ## ## ## ## ######
*/
var (
// File types that are valid for writing
validFiletypes = [...]string{"csv", "json", "stdout", "xlsx"}
)
/*
######## ## ## ## ## ###### ######
## ## ## ### ## ## ## ## ##
## ## ## #### ## ## ##
###### ## ## ## ## ## ## ######
## ## ## ## #### ## ##
## ## ## ## ### ## ## ## ##
## ####### ## ## ###### ######
*/
// Decides which actual writeResults* function shall be used based on filename pre- or suffix
func writeResults(filename string, resultsNew devicesWrapper) (errCode uint, err error) {
var compress bool
var writer func(string, devicesWrapper) (uint, error)
// Determine whether output should be compressed
compress = config.CompressOutput
if strings.HasSuffix(filename, ".gz") {
compress = true
filename = strings.TrimSuffix(filename, ".gz")
}
// Prefix checking
for _, filetype := range validFiletypes {
prefix := fmt.Sprintf("%s:", filetype)
if strings.HasPrefix(filename, prefix) {
filename = strings.TrimPrefix(filename, prefix)
switch filetype {
case "csv":
writer = writeResultsCSV
case "json":
writer = writeResultsJSON
case "stdout":
writer = writeResultsStdout
compress = false
case "xlsx":
writer = writeResultsXLSX
}
}
}
// Suffix checking
if writer == nil {
for _, filetype := range validFiletypes {
suffix := fmt.Sprintf(".%s", filetype)
if strings.HasSuffix(filename, suffix) {
switch filetype {
case "csv":
writer = writeResultsCSV
case "json":
writer = writeResultsJSON
case "stdout":
writer = writeResultsStdout
compress = false
case "xlsx":
writer = writeResultsXLSX
}
}
}
}
// Quit if unsupported file type was provided
if writer == nil {
return 0, fmt.Errorf("Could not determine file type for <%s>", filename)
}
// Actually write the file
errCode, err = writer(filename, resultsNew)
if compress {
if err == nil {
err = compressFile(filename)
if err == nil {
err = os.Remove(filename)
}
}
}
return
}
// Writes the results to outfile in CSV format
func writeResultsCSV(filename string, results devicesWrapper) (uint, error) {
var rowsWritten uint = 0
csvData, csvError := results.ToCSV()
if csvError != nil {
return rowsWritten, fmt.Errorf("Could not convert data to CSV: %s", csvError)
}
fileHandle, fileErr := os.Create(filename)
if fileErr != nil {
return rowsWritten, fmt.Errorf("Could not create outfile: %s", fileErr)
}
fileWriter := bufio.NewWriter(fileHandle)
for _, line := range strings.Split(csvData, "\n") {
_, writeErr := fileWriter.WriteString(fmt.Sprintf("%s\n", line))
if writeErr != nil {
return rowsWritten, fmt.Errorf("Could not write to outfile: %s", writeErr)
}
flushErr := fileWriter.Flush()
if flushErr != nil {
stdErr.Printf("Could not flush file buffer: %s\n", flushErr)
}
rowsWritten++
}
syncErr := fileHandle.Sync()
if syncErr != nil {
stdErr.Printf("Could not sync file handle: %s\n", syncErr)
}
fhErr := fileHandle.Close()
if fhErr != nil {
stdErr.Printf("Could not close file handle: %s\n", fhErr)
}
return rowsWritten, nil
}
// Writes the results to outfile in JSON format
func writeResultsJSON(filename string, results devicesWrapper) (uint, error) {
var rowsWritten uint = 0
jsonData, jsonErr := results.ToJSON()
if jsonErr != nil {
return rowsWritten, fmt.Errorf("Could not encode JSON: %s", jsonErr)
}
fileHandle, fileErr := os.Create(filename)
if fileErr != nil {
return rowsWritten, fmt.Errorf("Could not create outfile: %s", fileErr)
}
fileWriter := bufio.NewWriter(fileHandle)
for _, line := range strings.Split(jsonData, "\n") {
_, writeErr := fileWriter.WriteString(fmt.Sprintf("%s\n", line))
if writeErr != nil {
return rowsWritten, fmt.Errorf("Could not write to outfile: %s", writeErr)
}
flushErr := fileWriter.Flush()
if flushErr != nil {
stdErr.Printf("Could not flush file buffer: %s\n", flushErr)
}
rowsWritten++
}
syncErr := fileHandle.Sync()
if syncErr != nil {
stdErr.Printf("Could not sync file handle: %s\n", syncErr)
}
fhErr := fileHandle.Close()
if fhErr != nil {
stdErr.Printf("Could not close file handle: %s\n", fhErr)
}
return rowsWritten, nil
}
// Writes the results to stdout in CSV format
func writeResultsStdout(filename string, results devicesWrapper) (uint, error) {
var rowsWritten uint = 0
csvData, csvError := results.ToCSV()
if csvError != nil {
return rowsWritten, fmt.Errorf("Could not convert data to CSV: %s", csvError)
}
for _, line := range strings.Split(csvData, "\n") {
fmt.Printf("%s\n", line)
rowsWritten++
}
return rowsWritten, nil
}
// Writes the results to outfile in XLSX format
func writeResultsXLSX(filename string, results devicesWrapper) (uint, error) {
var rowsWritten uint = 0
var colIndex int = 1
var rowIndex int = 1
var colors map[int]map[int]string
var cellStyles map[int]map[int]int
var devCount int = 0
var rowCount int = 0
var devStyleID int = 0
var rowStyleID int = 0
xlsx := excelize.NewFile()
if !config.NoColor {
colors = make(map[int]map[int]string)
// Grey
colors[0] = make(map[int]string)
colors[0][0] = "#F2F2F2"
colors[0][1] = "#E6E6E6"
// Yellow
colors[1] = make(map[int]string)
colors[1][0] = "#FFFFE6"
colors[1][1] = "#FFFFCC"
// Green
colors[2] = make(map[int]string)
colors[2][0] = "#E6FFE6"
colors[2][1] = "#CCFFCC"
// Turqoise
colors[3] = make(map[int]string)
colors[3][0] = "#E6FFFF"
colors[3][1] = "#CCFFFF"
// Blue
colors[4] = make(map[int]string)
colors[4][0] = "#E6E6FF"
colors[4][1] = "#CCCCFF"
// Purple
colors[5] = make(map[int]string)
colors[5][0] = "#FFE6FF"
colors[5][1] = "#FFCCFF"
// Red
colors[6] = make(map[int]string)
colors[6][0] = "#FFE6E6"
colors[6][1] = "#FFCCCC"
cellStyles = make(map[int]map[int]int)
for baseColor := range colors {
cellStyles[baseColor] = make(map[int]int)
for rowColor := range colors[baseColor] {
styleString := fmt.Sprintf(`{"fill":{"type":"pattern","color":["%s"],"pattern":1}}`, colors[baseColor][rowColor])
styleFormat, styleFormatErr := xlsx.NewStyle(styleString)
if styleFormatErr != nil {
return rowsWritten, styleFormatErr
}
cellStyles[baseColor][rowColor] = styleFormat
}
}
}
for _, columnName := range csvColumns {
position, positionErr := excelize.CoordinatesToCellName(colIndex, rowIndex)
if positionErr != nil {
return rowsWritten, positionErr
}
valueErr := xlsx.SetCellValue("Sheet1", position, columnName)
if valueErr != nil {
stdErr.Printf("Could not set value for %s: %s", position, valueErr)
}
colIndex++
}
rowsWritten++
for _, dev := range results.Devices {
csvRows, csvRowsErr := dev.ToCSVRows()
if csvRowsErr != nil {
stdErr.Printf("Could not convert device to CSV rows: %s", csvRowsErr)
continue
}
for _, row := range csvRows {
colIndex = 1
rowIndex++
if !config.NoColor {
devStyleID = devCount % len(cellStyles)
rowStyleID = rowCount % len(cellStyles[devStyleID])
}
for _, element := range strings.Split(row, `","`) {
element = strings.Trim(element, `"`)
position, positionErr := excelize.CoordinatesToCellName(colIndex, rowIndex)
if positionErr != nil {
return rowsWritten, positionErr
}
valueErr := xlsx.SetCellValue("Sheet1", position, element)
if valueErr != nil {
stdErr.Printf("Could not set value for %s: %s", position, valueErr)
}
if !config.NoColor {
styleErr := xlsx.SetCellStyle("Sheet1", position, position, cellStyles[devStyleID][rowStyleID])
if styleErr != nil {
stdErr.Printf("Could not set style for cell %s: %s", position, styleErr)
}
}
colIndex++
}
rowsWritten++
rowCount++
}
devCount++
rowCount = 0
}
xlsx.SetSheetName("Sheet1", time.Now().Format(time.RFC3339))
if saveErr := xlsx.SaveAs(filename); saveErr != nil {
return rowsWritten, saveErr
}
return rowsWritten, nil
}
// Compresses an file using gzip
func compressFile(filename string) (err error) {
var data []byte
var gzFile *os.File
var gzWriter *gzip.Writer
data, err = os.ReadFile(filename)
if err != nil {
return fmt.Errorf("could not read file: %s", err)
}
gzFile, err = os.Create(fmt.Sprintf("%s.gz", filename))
if err != nil {
return fmt.Errorf("could not create file: %s", err)
}
gzWriter, err = gzip.NewWriterLevel(gzFile, gzip.BestCompression)
if err != nil {
return fmt.Errorf("could create initialize compression: %s", err)
}
_, err = gzWriter.Write(data)
if err != nil {
return fmt.Errorf("could not write compressed data: %s", err)
}
err = gzWriter.Close()
if err != nil {
return fmt.Errorf("could not write compressed data: %s", err)
}
err = gzFile.Close()
if err != nil {
return fmt.Errorf("could not close file: %s", err)
}
return
}