2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
24 "github.com/fsnotify/fsnotify"
25 "github.com/spf13/viper"
30 type Configurator struct {
33 type ConfigChangeCB func(filename string)
35 var ConfigChangeListeners []ConfigChangeCB
37 func parseCmd() string {
39 fileName = flag.String("f", os.Getenv("CFG_FILE"), "Specify the configuration file.")
45 func LoadConfig() (l *Log) {
46 l = NewLogger(filepath.Base(os.Args[0]))
47 viper.SetConfigFile(parseCmd())
49 if err := viper.ReadInConfig(); err != nil {
50 l.Error("Reading config file failed: %v", err.Error())
52 l.Info("Using config file: %s", viper.ConfigFileUsed())
55 viper.OnConfigChange(func(e fsnotify.Event) {
56 l.Info("config file %s changed ", e.Name)
58 Logger.SetLevel(viper.GetInt("logger.level"))
59 if len(ConfigChangeListeners) > 0 {
60 for _, f := range ConfigChangeListeners {
69 func AddConfigChangeListener(f ConfigChangeCB) {
70 if ConfigChangeListeners == nil {
71 ConfigChangeListeners = make([]ConfigChangeCB, 0)
73 ConfigChangeListeners = append(ConfigChangeListeners, f)
76 func (*Configurator) GetString(key string) string {
77 return viper.GetString(key)
80 func (*Configurator) GetInt(key string) int {
81 return viper.GetInt(key)
84 func (*Configurator) GetUint32(key string) uint32 {
85 return viper.GetUint32(key)
88 func (*Configurator) GetBool(key string) bool {
89 return viper.GetBool(key)
92 func (*Configurator) Get(key string) interface{} {
96 func (*Configurator) GetStringSlice(key string) []string {
97 return viper.GetStringSlice(key)
100 func (*Configurator) GetStringMap(key string) map[string]interface{} {
101 return viper.GetStringMap(key)