|
@@ -0,0 +1,85 @@
|
|
|
+package config
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/spf13/cast"
|
|
|
+ viperLib "github.com/spf13/viper"
|
|
|
+ "path"
|
|
|
+ "rinne.dev/doh-resolver/pkg/helpers"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+var viper *viperLib.Viper
|
|
|
+
|
|
|
+func init() {
|
|
|
+ viper = viperLib.New()
|
|
|
+ viper.SetConfigType("yaml")
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func InitConfig(filePath string) {
|
|
|
+
|
|
|
+ viper.SetConfigName(path.Base(filePath))
|
|
|
+ viper.AddConfigPath(path.Dir(filePath))
|
|
|
+ if err := viper.ReadInConfig(); err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ viper.WatchConfig()
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func Get(path string, defaultValue ...interface{}) string {
|
|
|
+ return GetString(path, defaultValue...)
|
|
|
+}
|
|
|
+
|
|
|
+func internalGet(path string, defaultValue ...interface{}) interface{} {
|
|
|
+
|
|
|
+ if !viper.IsSet(path) || helpers.Empty(viper.Get(path)) {
|
|
|
+ if len(defaultValue) > 0 {
|
|
|
+ return defaultValue[0]
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return viper.Get(path)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetString(path string, defaultValue ...interface{}) string {
|
|
|
+ return cast.ToString(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetInt(path string, defaultValue ...interface{}) int {
|
|
|
+ return cast.ToInt(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetFloat64(path string, defaultValue ...interface{}) float64 {
|
|
|
+ return cast.ToFloat64(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetInt64(path string, defaultValue ...interface{}) int64 {
|
|
|
+ return cast.ToInt64(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetUint(path string, defaultValue ...interface{}) uint {
|
|
|
+ return cast.ToUint(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetBool(path string, defaultValue ...interface{}) bool {
|
|
|
+ return cast.ToBool(internalGet(path, defaultValue...))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetStringMapString(path string) map[string]string {
|
|
|
+ return viper.GetStringMapString(path)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func GetStringArray(path string) []string {
|
|
|
+ return cast.ToStringSlice(internalGet(path))
|
|
|
+}
|