-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
175 lines (147 loc) · 4.77 KB
/
process.go
File metadata and controls
175 lines (147 loc) · 4.77 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
/*
1 Listen for (create and write)Changes on a specific file(*.summary) in the input directory
2 Once they is a change, Read current content of the File
3 Create a slice with the Content
4 Identify specific transaction data in the content slice to be removed
4.1 Create new Unique file
4.2 Move each identified transaction data into the unique file
5 Remove identified transaction data from content slice, creating a new slice
6 convert new slice back to string
7 save new slice back to the watched file in the input directory
TODO :
4. Write test
*/
func startProcess() {
var inDir, outDir, backupDir string = config.InDir, config.OutDir, config.BackupDir
if _, err := os.Stat(inDir); os.IsNotExist(err) {
fmt.Println("Input directory does not exists")
logToFile("Input directory does not exists \n")
return
}
if _, err := os.Stat(outDir); os.IsNotExist(err) {
fmt.Println("Output directory does not exists")
return
}
if backupDir != "" {
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
fmt.Println("Backup directory does not exists")
logToFile("Backup directory does not exists \n")
return
}
}
//start monitoring input directory
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("ERROR", err)
logToFile(fmt.Sprintf("\n\nERROR : %s \n", err))
}
defer watcher.Close()
tick := time.Tick(time.Second)
eventMap := make(map[string]fsnotify.Event)
go func() {
type fileNamer struct {
day time.Weekday
count int
}
fileNameCounter := fileNamer{time.Now().Weekday(), 0}
for {
select {
case <-tick:
for name, event := range eventMap {
delete(eventMap, name)
var newFileSlice []string
fileSlice := fileToSlice(event.Name)
for _, content := range fileSlice {
if !strings.Contains(content, config.Keyword) {
newFileSlice = append(newFileSlice, content)
continue
}
println("Found Transaction:", content[:len(content)/5])
trimmerIndex := strings.Index(content, config.TrimerIndex)
transactionLine := strings.Split(content[trimmerIndex:], "\n")
fileName := fmt.Sprintf("STLB_Transact_%v_%v_%v.txt", time.Now().Format("20060102"), time.Now().Format("150405"), fileNameCounter.count)
//Proces New Transaction to a New File
sliceToFile(transactionLine, outDir+string(os.PathSeparator)+fileName)
println("Created Unique Transaction File: ", fileName)
logToFile(fmt.Sprintf("\n\nCreated Unique Transaction File: %s \n", fileName))
if backupDir != "" {
backupToFile(fmt.Sprintf("%s\n\n", content), fmt.Sprintf("%s%sbackup_%v.txt", backupDir, string(os.PathSeparator), time.Now().Format("20060102")))
}
if today := &fileNameCounter; today.day == time.Now().Weekday() {
today.count++
} else {
today.day = time.Now().Weekday()
today.count = 0
}
}
sliceToFile(newFileSlice, event.Name)
}
// watch for events
case event := <-watcher.Events:
if strings.HasSuffix(event.Name, config.Suffix) {
switch {
case event.Op&fsnotify.Write == fsnotify.Write,
event.Op&fsnotify.Create == fsnotify.Create:
eventMap[event.Name] = event
}
}
// watch for errors
case err := <-watcher.Errors:
fmt.Println("ERROR:", err)
logToFile(fmt.Sprintf("\n\nERROR: %s \n", err))
}
}
}()
if err := watcher.Add(inDir); err != nil {
fmt.Println("FATAL ERROR:", err)
logToFile(fmt.Sprintf("\n\nFATAL ERROR: %s \n", err))
os.Exit(0)
}
//This section exits the process
var exit string
fmt.Print("\n Type 'exit' and press 'Enter' to stop process...")
logToFile(fmt.Sprintf("\n Type 'exit' and press 'Enter' to stop process... \n"))
for {
if fmt.Scanln(&exit); exit == "exit" {
os.Exit(0)
}
}
}
func fileToSlice(fileName string) (fileSlice []string) {
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
return
}
fileString := string(fileBytes)
fileSlice = strings.Split(fileString, "\n")
return
}
func sliceToFile(fileSlice []string, fileName string) {
fileString := strings.Join(fileSlice, "\n")
ioutil.WriteFile(fileName, []byte(fileString), 666)
}
func backupToFile(transactionLine string, fileName string) {
// If the file doesn't exist, create it, or append to the file
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
println("ERROR", err.Error())
logToFile(fmt.Sprintf("\n\nERROR: %s \n", err.Error()))
}
if _, err := file.Write([]byte(transactionLine)); err != nil {
println("ERROR", err.Error())
logToFile(fmt.Sprintf("\n\nERROR: %s \n", err.Error()))
}
if err := file.Close(); err != nil {
println("ERROR", err.Error())
logToFile(fmt.Sprintf("\n\nERROR: %s \n", err.Error()))
}
}