-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
129 lines (109 loc) · 2.83 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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/blang/semver"
"github.com/mpchadwick/dbanon/src"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/sirupsen/logrus"
"io/ioutil"
"log"
"os"
"runtime/pprof"
)
var version string
const slug = "mpchadwick/dbanon"
func selfUpdate() error {
previous := semver.MustParse(version)
latest, err := selfupdate.UpdateSelf(previous, slug)
if err != nil {
return err
}
if previous.Equals(latest.Version) {
fmt.Println("Current binary is the latest version", version)
} else {
fmt.Println("Update successfully done to version", latest.Version)
fmt.Println("Release note:\n", latest.ReleaseNotes)
}
return nil
}
func main() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
requested := flag.String("config", "", "Configuration to use. magento2 is included out-of-box. Alternately, supply path to file")
update := flag.Bool("update", false, "Auto update dbanon to the newest version")
ver := flag.Bool("version", false, "Get current version")
silent := flag.Bool("silent", false, "Disable all logging")
logFile := flag.String("log-file", "", "File to write logs to")
logLevel := flag.String("log-level", "", "Specify desired log level")
profile := flag.Bool("profile", false, "Generate a profile")
flag.Parse()
if *profile {
profF, _ := os.Create("dbanon.prof")
pprof.StartCPUProfile(profF)
defer pprof.StopCPUProfile()
}
if *ver {
fmt.Println(version)
os.Exit(0)
}
if *update {
if err := selfUpdate(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
dbanonLogger := dbanon.GetLogger()
if !*silent {
f := "dbanon.log"
if *logFile != "" {
f = *logFile
}
file, _ := os.OpenFile(f, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
dbanonLogger.SetOutput(file)
} else {
dbanonLogger.SetOutput(ioutil.Discard)
}
if *logLevel != "" {
level, err := logrus.ParseLevel(*logLevel)
if err != nil {
dbanonLogger.Error(err)
} else {
dbanonLogger.SetLevel(level)
}
}
config, err := dbanon.NewConfig(*requested)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// sqlparser can be noisy
// https://github.com/xwb1989/sqlparser/blob/120387863bf27d04bc07db8015110a6e96d0146c/ast.go#L52
// We don't want to hear about it
log.SetOutput(ioutil.Discard)
reader := bufio.NewReader(os.Stdin)
args := flag.Args()
mode := "anonymize"
if len(args) > 0 && args[0] == "map-eav" {
mode = "map-eav"
}
provider := dbanon.NewProvider()
eav := dbanon.NewEav(config)
processor := dbanon.NewLineProcessor(mode, config, provider, eav)
for {
text, err := reader.ReadString('\n')
result := processor.ProcessLine(text)
if mode == "anonymize" {
fmt.Print(result)
}
if err != nil {
break
}
}
if mode == "map-eav" {
out, _ := eav.Config.String()
fmt.Print(string(out))
os.Exit(0)
}
}