Dmitrii Stepanov
d433b49265
All checks were successful
DCO action / DCO (pull_request) Successful in 2m40s
Vulncheck / Vulncheck (pull_request) Successful in 3m41s
Build / Build Components (1.20) (pull_request) Successful in 4m27s
Build / Build Components (1.21) (pull_request) Successful in 5m6s
Tests and linters / Staticcheck (pull_request) Successful in 6m16s
Tests and linters / gopls check (pull_request) Successful in 6m23s
Tests and linters / Lint (pull_request) Successful in 6m48s
Tests and linters / Tests (1.20) (pull_request) Successful in 9m4s
Tests and linters / Tests with -race (pull_request) Successful in 9m9s
Tests and linters / Tests (1.21) (pull_request) Successful in 9m23s
`fmt.Errorf can be replaced with errors.New` and `fmt.Sprintf can be replaced with string addition` Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"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 = "_"
|
|
)
|
|
|
|
var errProvideViperInOpts = errors.New("provide viper in opts")
|
|
|
|
func CreateViper(opts ...Option) (*viper.Viper, error) {
|
|
o := defaultOpts()
|
|
for i := range opts {
|
|
opts[i](o)
|
|
}
|
|
|
|
var v *viper.Viper
|
|
if o.v != nil {
|
|
v = o.v
|
|
} else {
|
|
v = viper.New()
|
|
}
|
|
|
|
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, fmt.Errorf("failed to read config: %w", err)
|
|
}
|
|
}
|
|
|
|
if o.configDir != "" {
|
|
if err := config.ReadConfigDir(v, o.configDir); err != nil {
|
|
return nil, fmt.Errorf("failed to read config dir: %w", err)
|
|
}
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
func ReloadViper(opts ...Option) error {
|
|
o := defaultOpts()
|
|
for i := range opts {
|
|
opts[i](o)
|
|
}
|
|
|
|
if o.v == nil {
|
|
return errProvideViperInOpts
|
|
}
|
|
|
|
if o.path != "" {
|
|
err := o.v.ReadInConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("rereading configuration file: %w", err)
|
|
}
|
|
}
|
|
|
|
if o.configDir != "" {
|
|
if err := config.ReadConfigDir(o.v, o.configDir); err != nil {
|
|
return fmt.Errorf("rereading configuration dir: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|