-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
56 lines (48 loc) · 1.25 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
package main
import (
"io/fs"
"os"
"github.com/spf13/viper"
)
type Config struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
Tables []string `mapstructure:"tables"`
Output string `mapstructure:"output"`
Terminal bool `mapstructure:"terminal"`
BigCamelCaseWords []string `mapstructure:"bigCamelCaseWords"`
}
var Cfg = Config{
Host: "127.0.0.1",
Port: 3306,
Username: "root",
Password: "root",
DBName: "test",
Tables: nil,
Output: "./model",
Terminal: false,
BigCamelCaseWords: []string{""},
}
func initConfig() {
path, err := os.Getwd()
if err != nil {
panic(err)
}
viper.SetConfigFile(path + "/db.json")
if err = viper.ReadInConfig(); err != nil {
if _, ok := err.(*fs.PathError); ok {
panic("未找到配置文件,path=" + path + "/db.json")
} else {
panic(err)
}
}
if err = viper.Unmarshal(&Cfg); err != nil {
panic(err)
}
if Cfg.Output[len(Cfg.Output)-1] != '/' {
Cfg.Output += "/"
}
}