forked from TrueCloudLab/frostfs-node
1aa88159ca
In some cases viper doesn't interpret `section.value` as a subsection with `section` name, but value is value still can be accessed through full pathname. Fix `Config.Sub` method implementation in order to always interpret configuration like described above as a subsection. From now method never returns nil, therefore an additional check has been removed from the `Value` method. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
59 lines
1 KiB
Go
59 lines
1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Config represents a group of named values structured
|
|
// by tree type.
|
|
//
|
|
// Sub-trees are named configuration sub-sections,
|
|
// leaves are named configuration values.
|
|
// Names are of string type.
|
|
type Config struct {
|
|
v *viper.Viper
|
|
|
|
path []string
|
|
}
|
|
|
|
const (
|
|
separator = "."
|
|
envPrefix = "neofs"
|
|
envSeparator = "_"
|
|
)
|
|
|
|
// Prm groups required parameters of the Config.
|
|
type Prm struct{}
|
|
|
|
// New creates a new Config instance.
|
|
//
|
|
// If file option is provided (WithConfigFile),
|
|
// configuration values are read from it.
|
|
// Otherwise, Config is a degenerate tree.
|
|
func New(_ Prm, opts ...Option) *Config {
|
|
v := viper.New()
|
|
|
|
o := defaultOpts()
|
|
for i := range opts {
|
|
opts[i](o)
|
|
}
|
|
|
|
if o.path != "" {
|
|
v.SetEnvPrefix(envPrefix)
|
|
v.AutomaticEnv()
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(separator, envSeparator))
|
|
v.SetConfigFile(o.path)
|
|
|
|
err := v.ReadInConfig()
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to read config: %v", err))
|
|
}
|
|
}
|
|
|
|
return &Config{
|
|
v: v,
|
|
}
|
|
}
|