-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
54 lines (46 loc) · 914 Bytes
/
conf.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
package conf
import (
"github.com/futuretea/yapidoc/logs"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
config *viper.Viper
)
func readString(keys ...string) string {
var result = ""
for _, key := range keys {
result = config.GetString(key)
if result != "" {
return result
}
}
return result
}
func readInt(keys ...string) int {
var result = 0
for _, key := range keys {
result = config.GetInt(key)
if result != 0 {
return result
}
}
return result
}
func init() {
config = viper.New()
//bind cmdline
pflag.String("config", "config.yaml", "config file")
pflag.Parse()
err := config.BindPFlags(pflag.CommandLine)
if err != nil {
logs.Fatal("bind cmdline err", err)
}
//read file
config.SetConfigFile(config.GetString("config"))
if err := config.ReadInConfig(); err != nil {
logs.Fatal("read config file err", err)
}
//get env
config.AutomaticEnv()
}