-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (111 loc) · 3.11 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
123
124
125
126
127
128
package main
import (
"encoding/csv"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
// Category represents a category for bank transactions
type Category struct {
Name string
Regex *regexp.Regexp
}
func main() {
if len(os.Args) != 6 {
fmt.Printf("Wrong number of arguments: %v\n", len(os.Args))
fmt.Println("Usage: go run main.go categories.csv transactions.csv output.csv column_index separator")
return
}
columnIndicesSplit := strings.Split(os.Args[4], ",")
var columnIndices []int
for _, i := range columnIndicesSplit {
columnIndex, err := strconv.Atoi(i)
if err != nil {
fmt.Println("Error converting column index:", err)
return
}
columnIndices = append(columnIndices, columnIndex)
}
separator := os.Args[5]
categoriesFile, err := os.Open(os.Args[1])
if err != nil {
fmt.Println("Error opening categories file:", err)
return
}
defer categoriesFile.Close()
categoriesReader := csv.NewReader(categoriesFile)
categoriesRecords, err := categoriesReader.ReadAll()
if err != nil {
fmt.Println("Error reading categories file:", err)
return
}
var categories []Category
for _, record := range categoriesRecords {
if len(record) == 2 {
regex, err := regexp.Compile(record[1])
if err != nil {
fmt.Println("Error compiling regex for category", record[0], ":", err)
continue
}
categories = append(categories, Category{Name: record[0], Regex: regex})
}
}
transactionsFile, err := os.Open(os.Args[2])
if err != nil {
fmt.Println("Error opening transactions file:", err)
return
}
defer transactionsFile.Close()
transactionsReader := csv.NewReader(transactionsFile)
transactionsReader.Comma = rune(separator[0])
transactionsRecords, err := transactionsReader.ReadAll()
if err != nil {
fmt.Println("Error reading transactions file:", err)
return
}
// Extract and store the header
header := transactionsRecords[0]
transactionsRecords = transactionsRecords[1:]
var updatedTransactions [][]string
for _, record := range transactionsRecords {
var matchedCategory string
for _, columnIndex := range columnIndices {
if columnIndex < 0 || columnIndex >= len(record) {
fmt.Println("Invalid column index")
return
}
for _, category := range categories {
if category.Regex.MatchString(record[columnIndex]) {
matchedCategory = category.Name
break
}
}
}
record = append(record, matchedCategory)
updatedTransactions = append(updatedTransactions, record)
}
// Write the output with header
outputFile, err := os.Create(os.Args[3])
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outputFile.Close()
outputWriter := csv.NewWriter(outputFile)
defer outputWriter.Flush()
// Write the header
if err := outputWriter.Write(append(header, "Category")); err != nil {
fmt.Println("Error writing header:", err)
return
}
// Write the transaction data
for _, record := range updatedTransactions {
if err := outputWriter.Write(record); err != nil {
fmt.Println("Error writing transaction data:", err)
return
}
}
fmt.Printf("Categories successfully added and transactions saved to '%s'.\n", os.Args[3])
}