-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
74 lines (56 loc) · 1.17 KB
/
processor.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Processor struct {
File string
Output string
Fields []string
}
func (p Processor) Run() {
mappings := make([]Mapping, len(p.Fields))
for index, field := range p.Fields {
col := getColumn(field)
faker := getFaker(field)
if faker == "" {
faker = col
}
mappings[index] = Mapping{Col: col, Faker: faker}
}
fmt.Fprintln(os.Stdout, "Running...")
iterator := Iterator{Mappings: mappings}
file, err := os.Open(p.File)
check(err)
defer file.Close()
output, err := os.Create(p.Output)
check(err)
defer output.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
_, err := output.WriteString(fmt.Sprintf("%s\n", iterator.ProcessLine(scanner.Text())))
check(err)
}
err = scanner.Err()
check(err)
output.Sync()
fmt.Fprintln(os.Stdout, "Done")
}
func getColumn(field string) string {
col := Replace(field, ":(?:.*)$", "")
return strings.ToLower(col)
}
func getFaker(field string) string {
if !strings.Contains(field, ":") {
return ""
}
return Replace(field, ":(?:.*)$", "")
}
func check(e error) {
if e != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", e)
os.Exit(1)
}
}