generated from koddr/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_filtered.go
46 lines (38 loc) · 1.03 KB
/
save_filtered.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
package main
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"time"
)
// saveFilteredPKToCSV provides save process to filtered PK.
func (app *App) saveFilteredPKToCSV() error {
// Create name for CSV file.
fileName := fmt.Sprintf("./filtered-%d.csv", time.Now().Unix())
// Create a new CSV file in the current dir.
file, err := os.Create(filepath.Clean(fileName))
if err != nil {
return err
}
defer file.Close()
// Create a new CSV writer instance.
writer := csv.NewWriter(file)
defer writer.Flush()
// Write the header with PK column name of the CSV file.
if errWriter := writer.Write([]string{app.Config.ColumnsOrder[0]}); errWriter != nil {
return errWriter
}
// Write data body of the CSV file.
for _, data := range app.Outputs.Data {
// Take only PK column data.
if errWriter := writer.Write([]string{data.ID}); errWriter != nil {
return errWriter
}
}
printStyled(
fmt.Sprintf("– Saving filtered transactions to CSV file '%s' in the current dir... OK!", fileName),
"margin-top",
)
return nil
}