forked from TrueCloudLab/frostfs-node
[#125] node: Add new option WithEnvPrefix
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
ee01275d25
commit
b2123bfd1a
4 changed files with 20 additions and 7 deletions
|
@ -45,15 +45,17 @@ type Prm struct{}
|
||||||
func New(_ Prm, opts ...Option) *Config {
|
func New(_ Prm, opts ...Option) *Config {
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
|
|
||||||
v.SetEnvPrefix(EnvPrefix)
|
|
||||||
v.AutomaticEnv()
|
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(separator, EnvSeparator))
|
|
||||||
|
|
||||||
o := defaultOpts()
|
o := defaultOpts()
|
||||||
for i := range opts {
|
for i := range opts {
|
||||||
opts[i](o)
|
opts[i](o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if o.envPrefix != "" {
|
||||||
|
v.SetEnvPrefix(o.envPrefix)
|
||||||
|
v.AutomaticEnv()
|
||||||
|
v.SetEnvKeyReplacer(strings.NewReplacer(separator, EnvSeparator))
|
||||||
|
}
|
||||||
|
|
||||||
if o.path != "" {
|
if o.path != "" {
|
||||||
v.SetConfigFile(o.path)
|
v.SetConfigFile(o.path)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package config
|
||||||
type opts struct {
|
type opts struct {
|
||||||
path string
|
path string
|
||||||
configDir string
|
configDir string
|
||||||
|
envPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOpts() *opts {
|
func defaultOpts() *opts {
|
||||||
|
@ -27,3 +28,11 @@ func WithConfigDir(path string) Option {
|
||||||
o.configDir = path
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
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)) {
|
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 {
|
func EmptyConfig() *config.Config {
|
||||||
var p config.Prm
|
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.
|
// loadEnv reads .env file, parses `X=Y` records and sets OS ENVs.
|
||||||
|
|
|
@ -46,7 +46,9 @@ func main() {
|
||||||
os.Exit(SuccessReturnCode)
|
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)
|
err := validateConfig(appCfg)
|
||||||
fatalOnErr(err)
|
fatalOnErr(err)
|
||||||
|
|
Loading…
Reference in a new issue