diff options
Diffstat (limited to 'plugins/config')
-rw-r--r-- | plugins/config/provider.go | 4 | ||||
-rw-r--r-- | plugins/config/tests/plugin1.go | 9 | ||||
-rw-r--r-- | plugins/config/viper.go | 9 |
3 files changed, 17 insertions, 5 deletions
diff --git a/plugins/config/provider.go b/plugins/config/provider.go index bec417e9..580231fd 100644 --- a/plugins/config/provider.go +++ b/plugins/config/provider.go @@ -10,6 +10,10 @@ type Provider interface { // } // } UnmarshalKey(name string, out interface{}) error + // Get used to get config section Get(name string) interface{} + + // Has checks if config section exists. + Has(name string) bool } diff --git a/plugins/config/tests/plugin1.go b/plugins/config/tests/plugin1.go index 7573dc82..7c5f2afd 100644 --- a/plugins/config/tests/plugin1.go +++ b/plugins/config/tests/plugin1.go @@ -15,18 +15,17 @@ type ReloadConfig struct { } type ServiceConfig struct { - Enabled bool + Enabled bool Recursive bool - Patterns []string - Dirs []string - Ignore []string + Patterns []string + Dirs []string + Ignore []string } type Foo struct { configProvider config.Provider } - // Depends on S2 and DB (S3 in the current case) func (f *Foo) Init(p config.Provider) error { f.configProvider = p diff --git a/plugins/config/viper.go b/plugins/config/viper.go index 0362e79b..b276dbe2 100644 --- a/plugins/config/viper.go +++ b/plugins/config/viper.go @@ -17,17 +17,21 @@ type ViperProvider struct { //////// ENDURE ////////// func (v *ViperProvider) Init() error { v.viper = viper.New() + // read in environment variables that match v.viper.AutomaticEnv() if v.Prefix == "" { return errors.New("prefix should be set") } + v.viper.SetEnvPrefix(v.Prefix) if v.Path == "" { return errors.New("path should be set") } + v.viper.SetConfigFile(v.Path) v.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + return v.viper.ReadInConfig() } @@ -62,6 +66,11 @@ func (v *ViperProvider) Get(name string) interface{} { return v.viper.Get(name) } +// Has checks if config section exists. +func (v *ViperProvider) Has(name string) bool { + return v.viper.IsSet(name) +} + /////////// PRIVATE ////////////// func parseFlag(flag string) (string, string, error) { |