From b2123bfd1a0487d41d9b1d0868242d2d6f4e36dd Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Thu, 20 Apr 2023 12:17:03 +0300 Subject: [PATCH] [#125] node: Add new option `WithEnvPrefix` Signed-off-by: Anton Nikiforov --- cmd/frostfs-node/config/config.go | 10 ++++++---- cmd/frostfs-node/config/opts.go | 9 +++++++++ cmd/frostfs-node/config/test/config.go | 4 ++-- cmd/frostfs-node/main.go | 4 +++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/frostfs-node/config/config.go b/cmd/frostfs-node/config/config.go index dba3d12e7..cee6a1ea6 100644 --- a/cmd/frostfs-node/config/config.go +++ b/cmd/frostfs-node/config/config.go @@ -45,15 +45,17 @@ type Prm struct{} func New(_ Prm, opts ...Option) *Config { v := viper.New() - v.SetEnvPrefix(EnvPrefix) - v.AutomaticEnv() - v.SetEnvKeyReplacer(strings.NewReplacer(separator, EnvSeparator)) - 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) diff --git a/cmd/frostfs-node/config/opts.go b/cmd/frostfs-node/config/opts.go index 009c6efef..f49e1db69 100644 --- a/cmd/frostfs-node/config/opts.go +++ b/cmd/frostfs-node/config/opts.go @@ -3,6 +3,7 @@ package config type opts struct { path string configDir string + envPrefix string } func defaultOpts() *opts { @@ -27,3 +28,11 @@ func WithConfigDir(path string) Option { o.configDir = path } } + +// WithEnvPrefix returns an option to defines +// a prefix that ENVIRONMENT variables will use. +func WithEnvPrefix(envPrefix string) Option { + return func(o *opts) { + o.envPrefix = envPrefix + } +} diff --git a/cmd/frostfs-node/config/test/config.go b/cmd/frostfs-node/config/test/config.go index cd10edaec..808a481f9 100644 --- a/cmd/frostfs-node/config/test/config.go +++ b/cmd/frostfs-node/config/test/config.go @@ -25,7 +25,7 @@ func fromEnvFile(t testing.TB, path string) *config.Config { loadEnv(t, path) // github.com/joho/godotenv can do that as well - return config.New(p) + return config.New(p, config.WithEnvPrefix(config.EnvPrefix)) } func forEachFile(paths []string, f func(*config.Config)) { @@ -53,7 +53,7 @@ func ForEnvFileType(t testing.TB, pref string, f func(*config.Config)) { func EmptyConfig() *config.Config { var p config.Prm - return config.New(p) + return config.New(p, config.WithEnvPrefix(config.EnvPrefix)) } // loadEnv reads .env file, parses `X=Y` records and sets OS ENVs. diff --git a/cmd/frostfs-node/main.go b/cmd/frostfs-node/main.go index a6ee52464..b8bec87b5 100644 --- a/cmd/frostfs-node/main.go +++ b/cmd/frostfs-node/main.go @@ -46,7 +46,9 @@ func main() { os.Exit(SuccessReturnCode) } - appCfg := config.New(config.Prm{}, config.WithConfigFile(*configFile), config.WithConfigDir(*configDir)) + appCfg := config.New(config.Prm{}, + config.WithConfigFile(*configFile), config.WithConfigDir(*configDir), + config.WithEnvPrefix(config.EnvPrefix)) err := validateConfig(appCfg) fatalOnErr(err)