forked from wuchunfu/kubespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
69 lines (61 loc) · 1.93 KB
/
viper.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
/*
Copyright 2021 The DnsJia Authors.
WebSite: https://github.com/dnsjia/luban
Email: OpenSource@dnsjia.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tools
import (
"flag"
"fmt"
"os"
"github.com/foce123/kubespace/common"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func Viper(path ...string) *viper.Viper {
var config string
if len(path) == 0 {
flag.StringVar(&config, "c", "", "choose config file.")
flag.Parse()
if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
if configEnv := os.Getenv(common.ConfigEnv); configEnv == "" {
config = common.ConfigFile
fmt.Printf("您正在使用config的默认值, 配置路径为%v\n", common.ConfigFile)
} else {
config = configEnv
fmt.Printf("您正在使用GVA_CONFIG环境变量, 配置路径为%v\n", config)
}
} else {
fmt.Printf("您正在使用命令行的-c参数传递的值, 配置路径为%v\n", config)
}
} else {
config = path[0]
fmt.Printf("您正在使用func Viper()传递的值,config的路径为%v\n", config)
}
v := viper.New()
v.SetConfigFile(config)
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&common.CONFIG); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&common.CONFIG); err != nil {
fmt.Println(err)
}
return v
}