/
export.go
50 lines (42 loc) · 1.04 KB
/
export.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
package cmd
import (
"encoding/csv"
"log"
"os"
"time"
)
// Import imports the games from a csv file
// The file must be a csv file with ; as separator and # as comment
func Import(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
csvReader.Comma = ';'
csvReader.Comment = '#'
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
return records
}
// Export exports the games to a csv file
// The file name is the same as the input file with a timestamp
func Export() {
f := csvFile + "." + time.Now().Format("20060102150405")
file, err := os.Create(f)
if err != nil {
log.Fatal("Cannot create file "+f, err)
}
defer file.Close()
writer := csv.NewWriter(file)
writer.Comma = ';'
defer writer.Flush()
for _, value := range games {
if err := writer.Write(value.ToRecord()); err != nil {
log.Fatal("Cannot write games into "+f, err)
}
}
}