-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
213 lines (167 loc) · 4.55 KB
/
handlers.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
package main
import (
"database/sql"
"encoding/csv"
"flag"
"fmt"
"os"
"strings"
"github.com/olekukonko/tablewriter"
)
func handleFilePath(filepath string) (*os.File, error) {
if strings.Contains(filepath, "/") {
path := strings.Split(filepath, "/")
path = path[:len(path)-1]
finalPath := strings.Join(path, "/")
err := os.MkdirAll(finalPath, 0755)
if err != nil {
fmt.Printf("encountered error while writing directory. %s", err.Error())
os.Exit(FileDirectoryError)
}
}
return os.Create(filepath)
}
func exportToCsv(rows *sql.Rows, filepath string) {
file, err := handleFilePath(filepath)
if err != nil {
fmt.Printf("encountered error while creating file. %s", err.Error())
}
writer := csv.NewWriter(file)
defer rows.Close()
defer file.Close()
defer writer.Flush()
columns, err := rows.Columns()
if err != nil {
fmt.Printf("encountered error while reading columns. %s\n", err.Error())
}
writer.Write(columns)
for rows.Next() {
values := make([]interface{}, len(columns))
scanArgs := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
if err := rows.Scan(scanArgs...); err != nil {
fmt.Printf("error while scanning rows. %s\n", err.Error())
}
record := make([]string, 0)
for _, value := range values {
switch v := value.(type) {
case nil:
record = append(record, "")
case []byte:
record = append(record, string(v))
default:
record = append(record, fmt.Sprintf("%v", v))
}
}
writer.Write(record)
}
fmt.Printf("Succesfully exported! Path: %s\n", filepath)
}
func printTable(rows *sql.Rows) {
columns, _ := rows.Columns()
columnCount := len(columns)
values := make([]string, columnCount)
valuePtrs := make([]interface{}, columnCount)
for i := range values {
valuePtrs[i] = &values[i]
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(columns)
for rows.Next() {
err := rows.Scan(valuePtrs...)
if err != nil {
fmt.Println("Error:", err)
return
}
table.Append(values)
}
table.Render()
}
func handleQueryError(err error, query string) {
if err != nil {
fmt.Printf("Encountered error while executing the query.\nQuery: %s\nError:%s\n", query, err.Error())
os.Exit(SQLQueryError)
}
}
func handleQueryOutput(rows *sql.Rows, err error) {
handleQueryError(err, ViewMonthGetDatThirtyBroOvertimeQuery)
printTable(rows)
rows.Close()
}
func handleFlags() bool {
db := openSql()
defer db.Close()
lsPtr := flag.Bool("ls", false, "List all overtime for the current month")
gdtbPtr := flag.Bool("gdtb", false, "Include 29/30/31 from prev month")
namesPtr := flag.String("names", "all", "Filter list by name")
exportPtr := flag.String("e", "", "export to csv")
flag.Parse()
if *lsPtr {
hasNames := *namesPtr != "all"
if hasNames {
strName := *namesPtr
strName = strings.ReplaceAll(strName, " ", "")
arrNames := strings.Split(strName, ",")
sqlNames := make([]interface{}, 0, len(arrNames))
placeholders := make([]string, 0, len(arrNames))
for _, v := range arrNames {
placeholders = append(placeholders, "?")
sqlNames = append(sqlNames, v)
}
finalQuery := fmt.Sprintf(ViewMonthOvertimeWithNamesQueryFmt, strings.Join(placeholders, ","))
if *gdtbPtr {
finalQuery = fmt.Sprintf(ViewMonthGetDatThirtyBroOvertimeQueryFmt, strings.Join(placeholders, ","))
}
rows, err := db.Query(finalQuery, sqlNames...)
if *exportPtr != "" {
exportToCsv(rows, *exportPtr)
return true
}
handleQueryOutput(rows, err)
return true
}
if *gdtbPtr {
rows, err := db.Query(ViewMonthGetDatThirtyBroOvertimeQuery)
if *exportPtr != "" {
exportToCsv(rows, *exportPtr)
return true
}
handleQueryOutput(rows, err)
return true
}
rows, err := db.Query(ViewMonthOvertimeQuery)
if *exportPtr != "" {
exportToCsv(rows, *exportPtr)
return true
}
handleQueryOutput(rows, err)
return true
}
return false
}
func handleInsert() {
args := os.Args[1:]
ot, err := govertime(args)
if err != nil {
fmt.Printf("encountered error: %s", err.Error())
os.Exit(69)
}
db := openSql()
defer db.Close()
if _, err := db.Exec(CreateTableQuery); err != nil {
handleQueryError(err, CreateTableQuery)
os.Exit(SQLQueryError)
}
for _, v := range ot {
res, err := db.Exec(InsertOvertimeQuery, v.Name, v.TimeIn, v.TimeOut, v.HoursOT, v.Reason)
handleQueryError(err, InsertOvertimeQuery)
rows, err := res.RowsAffected()
if err != nil {
fmt.Printf("Failed to insert record. Error %s", err.Error())
os.Exit(SQLQueryError)
}
fmt.Printf("Inserted %d row! %v\n", rows, v)
}
}