-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (102 loc) · 2.43 KB
/
main.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
type Transaction struct {
Date string
CounterParty string
Amount float64
Balance float64
Reference string
Kind string
SpendingCategory string
Notes string
}
const (
date int = iota
counterParty
reference
kind
amount
balance
spendingCategory
notes
)
func main() {
if len(os.Args) <= 1 {
log.Fatal("Must pass a CSV file path")
}
input, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
inputFileName := input.Name()
outputFileName := fmt.Sprintf("%v-result.csv", strings.TrimSuffix(inputFileName, filepath.Ext(inputFileName)))
output, err := os.Create(outputFileName)
if err != nil {
log.Fatal(err)
}
defer input.Close()
defer output.Close()
generateCSV(input, output)
}
func generateCSV(input, output *os.File) {
reader := csv.NewReader(bufio.NewReader(input))
writer := csv.NewWriter(bufio.NewWriter(output))
defer writer.Flush()
// Ignore CSV header
_, err := reader.Read()
if err != nil {
log.Fatal(err)
}
for {
row, err := reader.Read()
if err == io.EOF || err != nil {
break
}
// Convert amount and balance string values to floats
rowAmount, err := strconv.ParseFloat(row[amount], 64)
if err != nil {
log.Fatal(err)
}
rowBalance, err := strconv.ParseFloat(row[balance], 64)
if err != nil {
log.Fatal(err)
}
transaction := Transaction{
Date: row[date],
CounterParty: row[counterParty],
Reference: row[reference],
Kind: row[kind],
SpendingCategory: row[spendingCategory],
Notes: row[notes],
Amount: rowAmount,
Balance: rowBalance,
}
notes := buildNotes(transaction)
csvRow := []string{transaction.Date, fmt.Sprint(transaction.Amount), notes}
writer.Write(csvRow)
}
log.Printf("Statement suceessfully extracted.")
}
// FreeAgent's CSV imports are quite basic, this function retains the data from Starling
// by adding it as a single "note" in FreeAgent's accepted CSV format
// See: https://support.freeagent.com/hc/en-gb/articles/115001222564
func buildNotes(transaction Transaction) string {
transactionNotes := []string{
transaction.CounterParty,
transaction.Reference,
transaction.Kind,
transaction.SpendingCategory,
transaction.Notes,
}
return strings.Join(transactionNotes, " ")
}