This commit is contained in:
Cédric Fabianski 2024-05-16 15:53:38 +02:00 committed by GitHub
commit 56238ae2f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View file

@ -2053,6 +2053,12 @@ func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]any, erro
return v.kvstore, err
}
func AllEnvVar() map[string][]string { return v.AllEnvVar() }
func (v *Viper) AllEnvVar() map[string][]string {
return v.env
}
// AllKeys returns all keys holding a value, regardless of where they are set.
// Nested keys are returned with a v.keyDelim separator.
func AllKeys() []string { return v.AllKeys() }

View file

@ -27,6 +27,7 @@ import (
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/spf13/viper/internal/features"
"github.com/spf13/viper/internal/testutil"
@ -659,6 +660,25 @@ func TestEmptyEnv_Allowed(t *testing.T) {
assert.Equal(t, "Cake", v.Get("name"))
}
func TestAllEnvVar(t *testing.T) {
initJSON()
SetEnvPrefix("foo") // will be uppercased automatically
BindEnv("id")
BindEnv("f", "FOOD", "BAR")
expectedEnvVariables := []byte(`f:
- FOO_FOOD
- FOO_BAR
id:
- FOO_ID
`)
yamlEnv, _ := yaml.Marshal(AllEnvVar())
assert.Equal(t, string(expectedEnvVariables), string(yamlEnv))
AutomaticEnv()
}
func TestEnvPrefix(t *testing.T) {
v := New()
v.SetConfigType("json")