-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling.go
249 lines (203 loc) · 5.49 KB
/
file_handling.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/gocarina/gocsv"
"github.com/pkg/errors"
)
type fileConfig struct {
baseDir string
currentFolderName string
previousFolderName string
}
func (fc *fileConfig) getSourcePath(fileName string) string {
return filepath.Join(fc.baseDir, "in", fileName)
}
func (fc *fileConfig) getCurrentSourcePath(fileName string) string {
return filepath.Join(fc.baseDir, "in", fc.currentFolderName, fileName)
}
func (fc *fileConfig) getCurrentDestinationPath(fileName string) string {
return filepath.Join(fc.baseDir, "out", fc.currentFolderName, fileName)
}
func (fc *fileConfig) getPreviousDestinationPath(fileName string) string {
return filepath.Join(fc.baseDir, "out", fc.previousFolderName, fileName)
}
func loadMembershipDetailsFromCsv(path string) (members map[string]*Member, err error) {
memberFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed to open %s", path)
}
defer memberFile.Close()
loadedMembers := []*Member{}
err = gocsv.UnmarshalFile(memberFile, &loadedMembers)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse membership from %s", path)
}
members = make(map[string]*Member)
for _, member := range loadedMembers {
members[member.MemberID] = member
}
return members, nil
}
func loadAllMembersFromCsv(path string) (members []*Member, err error) {
memberFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed to open %s", path)
}
defer memberFile.Close()
err = gocsv.UnmarshalFile(memberFile, &members)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse membership from %s", path)
}
return members, nil
}
type CsvBankTxn struct {
Junk1 string `csv:"Date"`
TxnType string `csv:"Type"`
Description string `csv:"Description"`
Junk2 string `csv:"Paid Out"`
Amount string `csv:"Paid In"`
Junk3 string `csv:"Balance"`
}
func loadTxnsFromCsv(path string) (txns []*bankTxn, err error) {
txnFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed to open %s", path)
}
defer txnFile.Close()
loadedTxns := []*CsvBankTxn{}
err = gocsv.UnmarshalFile(txnFile, &loadedTxns)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse transactions from %s", path)
}
for _, loadedTxn := range loadedTxns {
if loadedTxn.TxnType == "CR" {
txn, err := newBankTxn(loadedTxn.Description, loadedTxn.Amount)
if err != nil {
return nil, fmt.Errorf("Failed to parse transaction (%v): %v", *loadedTxn, err)
}
txns = append(txns, txn)
}
}
return txns, nil
}
type MemberReference struct {
Reference string `csv:"Reference"`
MemberIDs string `csv:"MemberIds"`
}
func loadMemberReferencesFromCsv(path string) (references map[string][]string, err error) {
referenceFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed to open %s", path)
}
defer referenceFile.Close()
loadedReferences := []*MemberReference{}
err = gocsv.UnmarshalFile(referenceFile, &loadedReferences)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse membership from %s", path)
}
references = map[string][]string{}
for _, loadedReference := range loadedReferences {
reference := strings.TrimSpace(strings.ToUpper(loadedReference.Reference))
membershipIds := strings.Split(loadedReference.MemberIDs, "|")
_, ok := references[reference]
if !ok {
references[reference] = membershipIds
} else {
fmt.Fprintf(os.Stderr, "Loaded duplicate reference %v\n", reference)
}
}
return references, nil
}
func loadEmailsFromCsv(path string) (emails []string, err error) {
emailsFile, err := os.Open(path)
if err != nil {
return nil, err
}
defer emailsFile.Close()
emails = []string{}
csvReader := csv.NewReader(bufio.NewReader(emailsFile))
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
emails = append(emails, line[0])
}
return emails, nil
}
func writeTxnsToCsv(path string, txns []*bankTxn) error {
txnFile, err := os.Create(path)
if err != nil {
return err
}
defer txnFile.Close()
csvWriter := csv.NewWriter(bufio.NewWriter(txnFile))
err = csvWriter.Write([]string{"Description", "Amount"})
if err != nil {
return err
}
for _, txn := range txns {
err := csvWriter.Write([]string{txn.description, txn.amount.String()})
if err != nil {
return err
}
}
csvWriter.Flush()
return nil
}
func writeMembersToCsv(path string, members []*Member) error {
memberFile, err := os.Create(path)
if err != nil {
return err
}
defer memberFile.Close()
err = gocsv.MarshalFile(members, memberFile)
if err != nil {
return err
}
return nil
}
func writeMemberIdsToCsv(path string, memberIds []string) error {
targetFile, err := os.Create(path)
if err != nil {
return err
}
defer targetFile.Close()
csvWriter := csv.NewWriter(bufio.NewWriter(targetFile))
err = csvWriter.Write([]string{"MemberId"})
if err != nil {
return err
}
for _, line := range memberIds {
err := csvWriter.Write([]string{line})
if err != nil {
return err
}
}
csvWriter.Flush()
return nil
}
func writeStringsToCsv(path string, content []string) error {
targetFile, err := os.Create(path)
if err != nil {
return err
}
defer targetFile.Close()
csvWriter := csv.NewWriter(bufio.NewWriter(targetFile))
for _, line := range content {
err := csvWriter.Write([]string{line})
if err != nil {
return err
}
}
csvWriter.Flush()
return nil
}