-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathconfig.go
149 lines (136 loc) · 3.42 KB
/
config.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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
ini "github.com/dlintw/goconf"
)
// UseStdout means that if the flag `output` have this value, the dump will be written to the Stdout
const UseStdout = "-"
type config struct {
dsn string
maxOpenConns int
output string
file string
verbose bool
selectMap map[string]map[string]string
whereMap map[string]string
filterMap map[string]string
useTableLock bool
extendedInsRows int
cfg *ini.ConfigFile
}
func newConfig() *config {
return &config{
whereMap: make(map[string]string, 0),
selectMap: make(map[string]map[string]string, 0),
filterMap: make(map[string]string, 0),
}
}
func (c *config) usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] [config file]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nFlags:\n")
flag.PrintDefaults()
os.Exit(1)
}
func (c *config) parseAll() (err error) {
if err = c.parseCommandLine(); err != nil {
return
}
if err = c.parseConfigFile(); err != nil {
return
}
return
}
func (c *config) getVerboseLogger() *log.Logger {
w := ioutil.Discard
if c.verbose {
w = os.Stdout
}
return log.New(w, "mysqlsuperdump: ", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
}
func (c *config) parseCommandLine() (err error) {
flag.Usage = c.usage
flag.StringVar(&(c.output), "o", UseStdout, "Output path. Default is stdout")
flag.BoolVar(&(c.verbose), "v", false, "Enable printing status information")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
return errors.New("Missing parameters")
}
c.file = flag.Arg(0)
return
}
func (c *config) parseConfigFile() (err error) {
if c.cfg, err = ini.ReadConfigFile(c.file); err != nil {
return
}
if c.dsn, err = c.cfg.GetString("mysql", "dsn"); err != nil {
return
}
if c.extendedInsRows, err = c.cfg.GetInt("mysql", "extended_insert_rows"); err != nil {
c.extendedInsRows = 100
}
if c.useTableLock, err = c.cfg.GetBool("mysql", "use_table_lock"); err != nil {
c.useTableLock = true
}
if c.maxOpenConns, err = c.cfg.GetInt("mysql", "max_open_conns"); err != nil {
c.maxOpenConns = 50
}
var selects []string
if selects, err = c.cfg.GetOptions("select"); err != nil {
return
}
for _, tableCol := range selects {
var table, column string
if table, column, err = c.splitTableColumn(tableCol); err != nil {
return
}
if c.selectMap[table] == nil {
c.selectMap[table] = make(map[string]string, 0)
}
if c.selectMap[table][column], err = c.cfg.GetString("select", tableCol); err != nil {
return
}
}
if c.loadOptions("where", c.whereMap); err != nil {
return
}
if c.loadOptions("filter", c.filterMap); err != nil {
return
}
return
}
func (c *config) loadOptions(section string, optMap map[string]string) error {
var opts []string
var err error
if opts, err = c.cfg.GetOptions(section); err != nil {
return err
}
for _, key := range opts {
if optMap[key], err = c.cfg.GetString(section, key); err != nil {
return err
}
}
return nil
}
func (c *config) splitTableColumn(tableCol string) (table, column string, err error) {
split := strings.Split(tableCol, ".")
if len(split) != 2 {
err = errors.New("Expected 'table.column' format. Got wrong one:" + tableCol)
return
}
table = split[0]
column = split[1]
return
}
func (c *config) initOutput() (*os.File, error) {
if c.output == UseStdout {
return os.Stdout, nil
}
return os.Create(c.output)
}