-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (144 loc) · 4.34 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
package main
import (
helping_tool "converter/helping-tool"
reading_tool "converter/reading-tool"
"converter/sorting-tool"
"fmt"
"log"
"math/big"
"os"
"regexp"
"strings"
"time"
)
type Client struct {
code string
nom string
email string
adress string
}
func main() {
start := time.Now()
r := new(big.Int)
fmt.Println(r.Binomial(1000, 10))
var Arguments []string
var InputFile string
var OutputFile string
Arguments = os.Args[1:]
// --- Arguments --- //
if len(Arguments) == 0 {
fmt.Println("There is no argument, please execute './converter help'\n")
} else if len(Arguments) == 1 {
if Arguments[0] == "help" {
helping_tool.Help()
os.Exit(1)
} else if Arguments[0] != "help" {
fmt.Printf("The only argument " + Arguments[0] + " is unknown\n")
}
} else if len(Arguments) == 2 {
RegexInput, _ := regexp.MatchString(`^[\w,\s-]+\.(csv)$`, Arguments[0])
RegexOutput, _ := regexp.MatchString(`^[\w,\s-]+\.(txt)$`, Arguments[1])
if RegexInput {
InputFile = Arguments[0]
fmt.Printf("Your input file: " + InputFile + " is valid\n")
} else {
fmt.Printf("Your input filename must has [dot]csv extension and no special char !\n")
}
if RegexOutput {
OutputFile = Arguments[1]
fmt.Printf("Your output file: " + OutputFile + " is valid\n")
} else {
fmt.Printf("Your output filename must has [dot]txt extension and no special char !\n")
}
} else if len(Arguments) > 2 {
fmt.Printf("There is too much arguments\n")
}
// ---End Arguments--- //
//Setup log file
LogFile, errLogs := os.OpenFile("log.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if errLogs != nil {
log.Fatalf("Error opening file: %v", errLogs.Error())
}
defer func(LogFile *os.File) {
errDeferCloseLogFile := LogFile.Close()
if errDeferCloseLogFile != nil {
log.Printf(errDeferCloseLogFile.Error())
}
}(LogFile)
// Setup temp file
TempFile, errTmpFile := os.OpenFile("tmp.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if errTmpFile != nil {
log.Fatalf("Error opening file: %v", errTmpFile.Error())
}
defer func(TempFile *os.File) {
errDeferClose := TempFile.Close()
if errDeferClose != nil {
log.Fatal(errDeferClose.Error())
}
}(TempFile)
// Setup final file
FinalFileList, errFinalFile := os.OpenFile(OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if errFinalFile != nil {
log.Fatalf("Error opening file: %v", errFinalFile.Error())
}
defer func(FinalFileList *os.File) {
errDeferClose := FinalFileList.Close()
if errDeferClose != nil {
log.Fatal(errDeferClose.Error())
}
}(FinalFileList)
log.SetOutput(LogFile)
log.Println("Start")
fmt.Println("Start ...")
// Read Data's, stock in records var
records, errRecords := reading_tool.ReadData(InputFile)
if errRecords != nil {
log.Fatal(errRecords.Error())
}
// Read records line by line as record var
for _, record := range records {
matched, _ := regexp.MatchString(`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`, record[2])
if record[2] != "mail@nomail.com" {
if matched == true {
client := Client{
code: record[0],
nom: record[1],
email: record[2],
adress: record[3],
}
FinalEmail := strings.TrimSpace(client.email)
_, errWriteEmail := TempFile.WriteString(FinalEmail + "\n")
if errWriteEmail != nil {
log.Fatal(errWriteEmail.Error())
}
log.Printf("Email %+v was successfully wroten\n", client.email)
}
}
}
log.Println("Start sorting email")
// TODO : fix the sort -> All uppercase email are not sort with precedent lowercase email
MailList := sorting_tool.SortEmail("tmp.txt")
for _, MailItem := range MailList {
MailItem = strings.TrimSpace(MailItem)
MailItem = strings.ToLower(MailItem)
_, ErrorWriteFinalEmail := FinalFileList.WriteString(MailItem + "\n")
if ErrorWriteFinalEmail != nil {
log.Fatal(ErrorWriteFinalEmail.Error())
}
}
errTempFileClose := TempFile.Close()
if errTempFileClose != nil {
log.Fatalf("Err to close: %+v\n", errTempFileClose.Error())
} // If TempFile not close, he's supposed to be used by another process
errRemoveTmpFile := os.Remove(TempFile.Name())
if errRemoveTmpFile != nil {
log.Fatal(errRemoveTmpFile.Error())
}
log.Println("End")
fmt.Println("End !")
// Calculate time elapsed
elapsed := time.Since(start)
log.Printf("Elapsed time : %s\n", elapsed)
fmt.Printf("Elapsed time : %s\n", elapsed)
os.Exit(0)
}