mirror of
https://github.com/spf13/viper
synced 2025-04-28 08:17:17 +00:00
Add BindEnvSliceValue()
# Conflicts: # viper.go
This commit is contained in:
parent
9c07e0f063
commit
965cc033db
1 changed files with 24 additions and 0 deletions
24
viper.go
24
viper.go
|
@ -174,6 +174,7 @@ type Viper struct {
|
||||||
kvstore map[string]any
|
kvstore map[string]any
|
||||||
pflags map[string]FlagValue
|
pflags map[string]FlagValue
|
||||||
env map[string][]string
|
env map[string][]string
|
||||||
|
envSliceValues map[string]bool
|
||||||
aliases map[string]string
|
aliases map[string]string
|
||||||
typeByDefValue bool
|
typeByDefValue bool
|
||||||
|
|
||||||
|
@ -204,6 +205,7 @@ func New() *Viper {
|
||||||
v.kvstore = make(map[string]any)
|
v.kvstore = make(map[string]any)
|
||||||
v.pflags = make(map[string]FlagValue)
|
v.pflags = make(map[string]FlagValue)
|
||||||
v.env = make(map[string][]string)
|
v.env = make(map[string][]string)
|
||||||
|
v.envSliceValues = make(map[string]bool)
|
||||||
v.aliases = make(map[string]string)
|
v.aliases = make(map[string]string)
|
||||||
v.typeByDefValue = false
|
v.typeByDefValue = false
|
||||||
v.logger = slog.New(&discardHandler{})
|
v.logger = slog.New(&discardHandler{})
|
||||||
|
@ -1103,6 +1105,7 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
|
||||||
// If more arguments are provided, they will represent the env variable names that
|
// If more arguments are provided, they will represent the env variable names that
|
||||||
// should bind to this key and will be taken in the specified order.
|
// should bind to this key and will be taken in the specified order.
|
||||||
// EnvPrefix will be used when set when env name is not provided.
|
// EnvPrefix will be used when set when env name is not provided.
|
||||||
|
// Use BindEnvSliceValue() for binding slice variables.
|
||||||
func BindEnv(input ...string) error { return v.BindEnv(input...) }
|
func BindEnv(input ...string) error { return v.BindEnv(input...) }
|
||||||
|
|
||||||
func (v *Viper) BindEnv(input ...string) error {
|
func (v *Viper) BindEnv(input ...string) error {
|
||||||
|
@ -1132,6 +1135,20 @@ func (v *Viper) MustBindEnv(input ...string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BindEnvSliceValue is identical to BindEnv but converts the value of the environment variable into
|
||||||
|
// a slice using strings.Fields(), e. g. variable with value " a b" is converted into ["a", "b"].
|
||||||
|
func BindEnvSliceValue(input ...string) error { return v.BindEnvSliceValue(input...) }
|
||||||
|
func (v *Viper) BindEnvSliceValue(input ...string) error {
|
||||||
|
if len(input) == 0 {
|
||||||
|
return fmt.Errorf("missing key to bind to")
|
||||||
|
}
|
||||||
|
|
||||||
|
key := strings.ToLower(input[0])
|
||||||
|
_ = v.BindEnv(input...)
|
||||||
|
v.envSliceValues[key] = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Given a key, find the value.
|
// Given a key, find the value.
|
||||||
//
|
//
|
||||||
// Viper will check to see if an alias exists first.
|
// Viper will check to see if an alias exists first.
|
||||||
|
@ -1209,6 +1226,9 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
||||||
// even if it hasn't been registered, if automaticEnv is used,
|
// even if it hasn't been registered, if automaticEnv is used,
|
||||||
// check any Get request
|
// check any Get request
|
||||||
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
||||||
|
if v.envSliceValues[envKey] {
|
||||||
|
return strings.Fields(val)
|
||||||
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
if nested && v.isPathShadowedInAutoEnv(path) != "" {
|
if nested && v.isPathShadowedInAutoEnv(path) != "" {
|
||||||
|
@ -1219,6 +1239,9 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
|
||||||
if exists {
|
if exists {
|
||||||
for _, envkey := range envkeys {
|
for _, envkey := range envkeys {
|
||||||
if val, ok := v.getEnv(envkey); ok {
|
if val, ok := v.getEnv(envkey); ok {
|
||||||
|
if v.envSliceValues[envkey] {
|
||||||
|
return strings.Fields(val)
|
||||||
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2025,6 +2048,7 @@ func (v *Viper) DebugTo(w io.Writer) {
|
||||||
fmt.Fprintf(w, "Override:\n%#v\n", v.override)
|
fmt.Fprintf(w, "Override:\n%#v\n", v.override)
|
||||||
fmt.Fprintf(w, "PFlags:\n%#v\n", v.pflags)
|
fmt.Fprintf(w, "PFlags:\n%#v\n", v.pflags)
|
||||||
fmt.Fprintf(w, "Env:\n%#v\n", v.env)
|
fmt.Fprintf(w, "Env:\n%#v\n", v.env)
|
||||||
|
fmt.Fprintf(w, "Env Slice Values:\n%#v\n", v.envSliceValues)
|
||||||
fmt.Fprintf(w, "Key/Value Store:\n%#v\n", v.kvstore)
|
fmt.Fprintf(w, "Key/Value Store:\n%#v\n", v.kvstore)
|
||||||
fmt.Fprintf(w, "Config:\n%#v\n", v.config)
|
fmt.Fprintf(w, "Config:\n%#v\n", v.config)
|
||||||
fmt.Fprintf(w, "Defaults:\n%#v\n", v.defaults)
|
fmt.Fprintf(w, "Defaults:\n%#v\n", v.defaults)
|
||||||
|
|
Loading…
Add table
Reference in a new issue