-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (106 loc) · 3.41 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
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/psanchezg/inbox-extract-data/config"
"github.com/psanchezg/inbox-extract-data/extractors"
"github.com/psanchezg/inbox-extract-data/modules/bolt"
mmonitencoders "github.com/psanchezg/inbox-extract-data/modules/mmonit-encoders"
"github.com/psanchezg/inbox-extract-data/outputs"
"github.com/psanchezg/inbox-extract-data/utils"
"github.com/spf13/viper"
)
var (
// destinationDir is the path to the directory where the attachments will be saved.
afterDate = os.Getenv("AFTER_DATE")
)
func processData(configurations config.Configurations) {
// TODO: Iterate over all processes
for _, process := range configurations.Processes {
query := process.Query
currentYear := time.Now().Year()
// Extract date
rx := `after:(?P<After>\d{4}\/\d{2}\/\d{2})`
params := utils.GetParams(rx, query)
// Convert date
l := "2006/01/02"
tt, err := time.Parse(l, params["After"])
if err == nil {
currentYear = tt.Year()
}
msgs, err := extractors.ExtractMails(query)
if err != nil {
fmt.Println(err)
}
// Human export
var lines []string
var values [][]interface{}
if process.Module == "bolt" {
fmt.Println("========================================================")
fmt.Printf("PROCESSING... %v\n", process.Name)
fmt.Println("========================================================")
planes, err := bolt.ProcessRawData(msgs, currentYear)
if err != nil {
fmt.Println(err)
return
}
var serialized []map[string]interface{}
inrec, _ := json.Marshal(planes)
json.Unmarshal(inrec, &serialized)
if lines, values, err = bolt.ExportData[map[string]interface{}](serialized); err != nil {
fmt.Println(err)
}
} else if process.Module == "mmonit-encoders" {
fmt.Println("========================================================")
fmt.Printf("PROCESSING... %v\n", process.Name)
fmt.Println("========================================================")
data, err := mmonitencoders.ProcessRawData(msgs, currentYear)
if err != nil {
fmt.Println(err)
return
}
if err != nil {
fmt.Println(err)
return
}
var serialized map[string][]map[string]interface{}
inrec, _ := json.Marshal(data)
json.Unmarshal(inrec, &serialized)
if lines, values, err = mmonitencoders.ExportData[map[string]interface{}](serialized); err != nil {
fmt.Println(err)
}
}
for _, output := range process.Outputs {
if output.Type == "stdout" && len(lines) > 0 {
outputs.ConsoleOutput(lines)
} else if output.Type == "file" && len(lines) > 0 {
outputs.FileOutput(lines, output.Path)
} else if output.Type == "sheet" && len(values) > 0 {
outputs.SheetsOutput(values, output.Path)
}
}
fmt.Println("========================================================")
}
}
func main() {
// Set the file name of the configurations file
viper.SetConfigName("config")
// Set the path to look for the configurations file
viper.AddConfigPath(".")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetConfigType("yml")
var configuration config.Configurations
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
// Set undefined variables
// viper.SetDefault("database.dbname", "test_db")
err := viper.Unmarshal(&configuration)
if err != nil {
fmt.Printf("Unable to decode into struct, %v", err)
}
processData(configuration)
}