github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/viperx/viperx.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package viperx 21 22 import ( 23 "strings" 24 "sync" 25 "time" 26 27 "github.com/fsnotify/fsnotify" 28 "github.com/spf13/pflag" 29 "github.com/spf13/viper" 30 ) 31 32 var lock sync.RWMutex 33 34 func IsSet(key string) bool { 35 return rCall(key, viper.IsSet) 36 } 37 38 func Get(key string) interface{} { 39 return rCall(key, viper.Get) 40 } 41 42 func GetBool(key string) bool { 43 return rCall(key, viper.GetBool) 44 } 45 46 func GetInt(key string) int { 47 return rCall(key, viper.GetInt) 48 } 49 50 func GetInt32(key string) int32 { 51 return rCall(key, viper.GetInt32) 52 } 53 54 func GetString(key string) string { 55 return rCall(key, viper.GetString) 56 } 57 58 func GetStringSlice(key string) []string { 59 return rCall(key, viper.GetStringSlice) 60 } 61 62 func GetDuration(key string) time.Duration { 63 return rCall(key, viper.GetDuration) 64 } 65 66 func AllSettings() map[string]interface{} { 67 lock.RLock() 68 defer lock.RUnlock() 69 return viper.AllSettings() 70 } 71 72 func rCall[T interface{}](key string, f func(string) T) T { 73 lock.RLock() 74 defer lock.RUnlock() 75 return f(key) 76 } 77 78 func Set(key string, value interface{}) { 79 lock.Lock() 80 defer lock.Unlock() 81 viper.Set(key, value) 82 } 83 84 func SetDefault(key string, value interface{}) { 85 lock.Lock() 86 defer lock.Unlock() 87 viper.SetDefault(key, value) 88 } 89 90 func MergeConfigMap(cfg map[string]interface{}) error { 91 lock.Lock() 92 defer lock.Unlock() 93 return viper.MergeConfigMap(cfg) 94 } 95 96 func AutomaticEnv() { 97 viper.AutomaticEnv() 98 } 99 100 func BindEnv(input ...string) error { 101 return viper.BindEnv(input...) 102 } 103 104 func SetEnvPrefix(in string) { 105 viper.SetEnvPrefix(in) 106 } 107 108 func SetEnvKeyReplacer(r *strings.Replacer) { 109 viper.SetEnvKeyReplacer(r) 110 } 111 112 func SetConfigName(in string) { 113 viper.SetConfigName(in) 114 } 115 116 func SetConfigType(in string) { 117 viper.SetConfigType(in) 118 } 119 120 func AddConfigPath(in string) { 121 viper.AddConfigPath(in) 122 } 123 124 func SetConfigFile(in string) { 125 viper.SetConfigFile(in) 126 } 127 128 func BindPFlags(flags *pflag.FlagSet) error { 129 return viper.BindPFlags(flags) 130 } 131 132 func ReadInConfig() error { 133 return viper.ReadInConfig() 134 } 135 136 func ConfigFileUsed() string { 137 return viper.ConfigFileUsed() 138 } 139 140 func OnConfigChange(run func(in fsnotify.Event)) { 141 viper.OnConfigChange(run) 142 } 143 144 func GetViper() *viper.Viper { 145 return viper.GetViper() 146 } 147 148 func WatchConfig() { 149 viper.WatchConfig() 150 } 151 152 func Reset() { 153 viper.Reset() 154 }