-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
369 lines (332 loc) · 13.1 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
firebase "firebase.google.com/go"
"github.com/avii09/hookit/pkg/config"
"github.com/avii09/hookit/pkg/input"
"github.com/avii09/hookit/pkg/output"
"github.com/avii09/hookit/pkg/transform"
"google.golang.org/api/option"
)
func main() {
// Define the pipeline type flag
pipelineType := flag.String("pipeline", "", "Specify the pipeline type: csv, json, or firebase")
flag.Parse()
// Validate if the flag is provided
if *pipelineType == "" {
fmt.Println("Error: Missing required flag '-pipeline'. Use '-pipeline=csv', '-pipeline=json', or '-pipeline=firebase'.")
os.Exit(1)
}
// Determine configuration file based on the pipeline type
var configFilePath string
switch *pipelineType {
case "csv":
configFilePath = "pipelines/csv.yaml"
case "json":
configFilePath = "pipelines/json.yaml"
case "csv-to-json":
configFilePath = "pipelines/csv_to_json.yaml"
case "json-to-csv":
configFilePath = "pipelines/csv_to_json.yaml"
case "firebase":
configFilePath = "pipelines/firebase.yaml"
default:
fmt.Println("Error: Invalid pipeline type. Use '-pipeline=csv', '-pipeline=json', or '-pipeline=firebase'.")
os.Exit(1)
}
// Load the configuration file
cfg, err := config.LoadConfig(configFilePath)
if err != nil {
log.Fatalf("error loading config file: %v", err)
}
// Run the appropriate pipeline based on the flag
switch *pipelineType {
case "csv":
runCSVPipeline(cfg)
case "json":
runJSONPipeline(cfg)
case "csv-to-json":
runCSVToJSONPipeline(cfg)
case "json-to-csv":
runCSVToJSONPipeline(cfg)
case "firebase":
runFirebasePipeline(cfg)
}
}
func runCSVPipeline(cfg config.Config) {
var data []map[string]interface{}
// Determine input type and read data accordingly
switch cfg.Pipeline.Input.Type {
case "firebase":
// Initialize Firebase app
opt := option.WithCredentialsFile("firebase-adminsdk.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing Firebase app: %v", err)
}
// Initialize Firestore client
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error initializing Firestore client: %v", err)
}
defer client.Close()
// Read data from Firebase
data, err = input.ReadFirebase(client, cfg.Pipeline.Input.Config.Collection)
if err != nil {
log.Fatalf("error reading data from Firebase: %v", err)
}
case "csv":
// Read data from CSV
csvData, err := input.ReadCSV(cfg.Pipeline.Input.Config.FilePath)
if err != nil {
log.Fatalf("error reading data from CSV: %v", err)
}
// Convert CSV data to map[string]interface{}
data = make([]map[string]interface{}, len(csvData))
for i, row := range csvData {
data[i] = make(map[string]interface{})
for key, value := range row {
data[i][key] = value
}
}
default:
log.Fatalf("unsupported input type: %v", cfg.Pipeline.Input.Type)
}
// Apply transformations
transformedData := transform.ApplyFirebaseTransformations(data, cfg.Pipeline.Transformations.Mapping)
// Determine output type and write data accordingly
switch cfg.Pipeline.Output.Type {
case "firebase":
// Initialize Firebase app
opt := option.WithCredentialsFile("firebase-adminsdk.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing Firebase app: %v", err)
}
// Initialize Firestore client
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error initializing Firestore client: %v", err)
}
defer client.Close()
// Write transformed data back to Firebase
if err := output.WriteFirebase(client, cfg.Pipeline.Output.Config.Collection, transformedData); err != nil {
log.Fatalf("error writing data to Firebase: %v", err)
}
fmt.Println("Data transformed and written to Firebase successfully!")
case "json":
// Convert data to map[string]string for JSON output
stringData, err := input.ConvertMapToStringMap(transformedData)
if err != nil {
log.Fatalf("error converting data: %v", err)
}
// Write transformed data to JSON
if err := output.WriteJSON(cfg.Pipeline.Output.Config.FilePath, stringData); err != nil {
log.Fatalf("error writing data to JSON: %v", err)
}
fmt.Println("Data transformed and written to JSON successfully!")
case "csv":
// Convert data to map[string]string for CSV output
stringData, err := input.ConvertMapToStringMap(transformedData)
if err != nil {
log.Fatalf("error converting data: %v", err)
}
// Write transformed data to CSV
if err := output.WriteCSV(cfg.Pipeline.Output.Config.FilePath, stringData); err != nil {
log.Fatalf("error writing data to CSV: %v", err)
}
fmt.Println("Data transformed and written to CSV successfully!")
default:
log.Fatalf("unsupported output type: %v", cfg.Pipeline.Output.Type)
}
}
func runJSONPipeline(cfg config.Config) {
var data []map[string]interface{}
var err error
// Determine input type and read data accordingly
switch cfg.Pipeline.Input.Type {
case "firebase":
// Initialize Firebase app
opt := option.WithCredentialsFile("firebase-adminsdk.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing Firebase app: %v", err)
}
// Initialize Firestore client
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error initializing Firestore client: %v", err)
}
defer client.Close()
// Read data from Firebase
data, err = input.ReadFirebase(client, cfg.Pipeline.Input.Config.Collection)
if err != nil {
log.Fatalf("error reading data from Firebase: %v", err)
}
case "json":
// Read data from JSON
data, err = input.ReadJSON(cfg.Pipeline.Input.Config.FilePath)
if err != nil {
log.Fatalf("error reading data from JSON: %v", err)
}
default:
log.Fatalf("unsupported input type: %v", cfg.Pipeline.Input.Type)
}
// Convert data to map[string]string
stringData, err := input.ConvertMapToStringMap(data)
if err != nil {
log.Fatalf("error converting data: %v", err)
}
// Apply transformations
transformedData := transform.ApplyTransformations(stringData, cfg.Pipeline.Transformations)
// Convert transformedData back to []map[string]interface{} for Firebase output
interfaceData := make([]map[string]interface{}, len(transformedData))
for i, row := range transformedData {
interfaceData[i] = make(map[string]interface{})
for key, value := range row {
interfaceData[i][key] = value
}
}
// Determine output type and write data accordingly
switch cfg.Pipeline.Output.Type {
case "firebase":
// Initialize Firebase app
opt := option.WithCredentialsFile("firebase-adminsdk.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing Firebase app: %v", err)
}
// Initialize Firestore client
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error initializing Firestore client: %v", err)
}
defer client.Close()
// Write transformed data back to Firebase
if err := output.WriteFirebase(client, cfg.Pipeline.Output.Config.Collection, interfaceData); err != nil {
log.Fatalf("error writing data to Firebase: %v", err)
}
fmt.Println("Data transformed and written to Firebase successfully!")
case "json":
// Write transformed data to JSON
if err := output.WriteJSON(cfg.Pipeline.Output.Config.FilePath, transformedData); err != nil {
log.Fatalf("error writing data to JSON: %v", err)
}
fmt.Println("Data transformed and written to JSON successfully!")
default:
log.Fatalf("unsupported output type: %v", cfg.Pipeline.Output.Type)
}
}
func runCSVToJSONPipeline(cfg config.Config) {
inputFilePath := cfg.Pipeline.Input.Config.FilePath
if strings.HasSuffix(inputFilePath, ".csv") {
// CSV Input
fmt.Println("Detected CSV input file")
data, err := input.ReadCSV(cfg.Pipeline.Input.Config.FilePath)
if err != nil {
log.Fatalf("error reading data from CSV: %v", err)
}
// Apply transformations (including conversion to JSON)
transformedData := transform.ApplyTransformations(data, cfg.Pipeline.Transformations)
// Convert transformedData (of type []map[string]string) to []map[string]interface{}
interfaceData := make([]map[string]interface{}, len(transformedData))
for i, row := range transformedData {
interfaceData[i] = make(map[string]interface{})
for key, value := range row {
interfaceData[i][key] = value
}
}
// Convert transformed CSV data to JSON format
jsonData := transform.CSVToJSON(interfaceData)
var jsonMap []map[string]string
if err := json.Unmarshal(jsonData, &jsonMap); err != nil {
log.Fatalf("error unmarshalling JSON data: %v", err)
}
// Write transformed data to JSON
if err := output.WriteJSON(cfg.Pipeline.Output.Config.FilePath, jsonMap); err != nil {
log.Fatalf("error writing data to JSON: %v", err)
}
fmt.Println("CSV data successfully transformed to JSON and written!")
} else if strings.HasSuffix(inputFilePath, ".json") {
data, err := input.ReadJSON(cfg.Pipeline.Input.Config.FilePath)
if err != nil {
log.Fatalf("error reading data from JSON: %v", err)
}
stringData := make([]map[string]string, len(data))
for i, row := range data {
stringData[i] = make(map[string]string)
for key, value := range row {
stringData[i][key] = fmt.Sprintf("%v", value) // Convert interface{} to string
}
}
transformedData := transform.ApplyTransformations(stringData, cfg.Pipeline.Transformations)
if err := output.WriteCSV(cfg.Pipeline.Output.Config.FilePath, transformedData); err != nil {
log.Fatalf("error writing data to CSV: %v", err)
}
fmt.Println("Data transformed and written to CSV successfully!")
}
}
func runFirebasePipeline(cfg config.Config) {
// Initialize Firebase app
opt := option.WithCredentialsFile("firebase-adminsdk.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("Error initializing Firebase app: %v", err)
}
// Initialize Firestore client
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("Error initializing Firestore client: %v", err)
}
defer func() {
if cerr := client.Close(); cerr != nil {
log.Printf("Error closing Firestore client: %v", cerr)
}
}()
// Read data from Firebase
data, err := input.ReadFirebase(client, cfg.Pipeline.Input.Config.Collection)
if err != nil {
log.Fatalf("Error reading data from Firebase collection '%s': %v", cfg.Pipeline.Input.Config.Collection, err)
}
// Apply transformations
transformedData := transform.ApplyFirebaseTransformations(data, cfg.Pipeline.Transformations.Mapping)
// Determine output type and write data accordingly
switch cfg.Pipeline.Output.Type {
case "firebase":
// Write transformed data back to Firebase
if err := output.WriteFirebase(client, cfg.Pipeline.Output.Config.Collection, transformedData); err != nil {
log.Fatalf("Error writing transformed data to Firebase collection '%s': %v", cfg.Pipeline.Output.Config.Collection, err)
}
log.Println("Data transformed and written to Firebase successfully!")
case "json":
// Convert data to map[string]string for JSON output
stringData, err := input.ConvertMapToStringMap(transformedData)
if err != nil {
log.Fatalf("Error converting data for JSON output: %v", err)
}
// Write transformed data to JSON
if err := output.WriteJSON(cfg.Pipeline.Output.Config.FilePath, stringData); err != nil {
log.Fatalf("Error writing transformed data to JSON file '%s': %v", cfg.Pipeline.Output.Config.FilePath, err)
}
log.Println("Data transformed and written to JSON successfully!")
case "csv":
// Convert data to map[string]string for CSV output
stringData, err := input.ConvertMapToStringMap(transformedData)
if err != nil {
log.Fatalf("Error converting data for CSV output: %v", err)
}
// Write transformed data to CSV
if err := output.WriteCSV(cfg.Pipeline.Output.Config.FilePath, stringData); err != nil {
log.Fatalf("Error writing transformed data to CSV file '%s': %v", cfg.Pipeline.Output.Config.FilePath, err)
}
log.Println("Data transformed and written to CSV successfully!")
default:
log.Fatalf("Unsupported output type '%s'. Supported types are: 'firebase', 'json', 'csv'", cfg.Pipeline.Output.Type)
}
}