package config import ( "fmt" "strings" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/config" "github.com/spf13/viper" ) const ( Separator = "." // EnvSeparator is a section separator in ENV variables. EnvSeparator = "_" ) func CreateViper(opts ...Option) (*viper.Viper, *Opts, error) { v := viper.New() o := DefaultOpts() for i := range opts { opts[i](o) } if o.EnvPrefix != "" { v.SetEnvPrefix(o.EnvPrefix) v.AutomaticEnv() v.SetEnvKeyReplacer(strings.NewReplacer(Separator, EnvSeparator)) } if o.Path != "" { v.SetConfigFile(o.Path) err := v.ReadInConfig() if err != nil { return nil, nil, fmt.Errorf("failed to read config: %w", err) } } if o.ConfigDir != "" { if err := config.ReadConfigDir(v, o.ConfigDir); err != nil { return nil, nil, fmt.Errorf("failed to read config dir: %w", err) } } return v, o, nil } func ReloadViper(v *viper.Viper, o Opts) error { if o.Path != "" { err := v.ReadInConfig() if err != nil { return fmt.Errorf("rereading configuration file: %w", err) } } if o.ConfigDir != "" { if err := config.ReadConfigDir(v, o.ConfigDir); err != nil { return fmt.Errorf("rereading configuration dir: %w", err) } } return nil }