forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
display_csv.go
53 lines (39 loc) · 901 Bytes
/
display_csv.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
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"encoding/csv"
"fmt"
"os"
"reflect"
)
type displayCSV struct {
}
func (d *displayCSV) logEntryToSlice(entry LogEntry) []string {
var record []string
v := reflect.ValueOf(entry)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
value := fmt.Sprintf("%v", field.Interface())
record = append(record, value)
}
return record
}
func (d *displayCSV) Display(entries *LogEntries, fieldNames []string, file *os.File) error {
writer := csv.NewWriter(file)
// header showing the format of the subsequent records
if err := writer.Write(fieldNames); err != nil {
return err
}
for _, entry := range entries.Entries {
record := d.logEntryToSlice(entry)
if err := writer.Write(record); err != nil {
return err
}
}
writer.Flush()
return writer.Error()
}