-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.go
More file actions
92 lines (77 loc) · 2.28 KB
/
Copy pathconfig.go
File metadata and controls
92 lines (77 loc) · 2.28 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
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
)
// Debug debug mode.
var Debug bool
// Log log.println
func Log(args ...interface{}) {
if Debug {
log.Println(args...)
}
}
// Config : config info
type Config struct {
Source string `json:"source"`
Target string `json:"target"`
DiffType string `json:"diff_type"`
Include string `json:"include"`
Exclude string `json:"exclude"`
Output string `json:"output"`
IgnoreColumn string `json:"ignore_column"`
}
// IsValid : valid check config
func (c *Config) IsValid() bool {
if c.DiffType == "schema" || c.DiffType == "data" {
return c.Source != "" && c.Target != ""
} else if c.DiffType == "md" || c.DiffType == "wiki" || c.DiffType == "sql" {
return c.Source != ""
}
return false
}
// LoadConfig load config from file.
func LoadConfig(path string) (*Config, error) {
if path == "" {
ex, _ := os.Executable()
exePath := filepath.Dir(ex)
path = exePath + "/conf.json"
}
dat, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
if err := json.Unmarshal(dat, &config); err != nil {
return nil, err
}
if !config.IsValid() {
return nil, errors.New("config file is invalid")
}
Log("load config file...", path, config)
return &config, nil
}
// ParseArgs parse argument.
func ParseArgs() (*Config, error) {
configPath := flag.String("conf", "", "config file path")
var config Config
flag.StringVar(&config.Source, "source", "", "source db connection string ex) [uid]:[pwd]@tcp([ip]:[port])/[dbname]")
flag.StringVar(&config.Target, "target", "", "source db connection string ex) [uid]:[pwd]@tcp([ip]:[port])/[dbname]")
flag.StringVar(&config.DiffType, "diff_type", "schema", "schema or data")
flag.StringVar(&config.Include, "include", "", "include object name,name,....")
flag.StringVar(&config.Exclude, "exclude", "", "exclude object name,name,name,...")
flag.StringVar(&config.IgnoreColumn, "ignore_column", "", "ignore column split column,column,... ")
flag.StringVar(&config.Output, "output", "", "result file")
flag.BoolVar(&Debug, "debug", false, "debug mode")
flag.Parse()
if *configPath != "" || !config.IsValid() {
return LoadConfig(*configPath)
}
Log("load config args", config)
return &config, nil
}