feat: make env variables accessible

This commit is contained in:
Cédric Fabianski 2023-12-20 11:22:58 +01:00
parent e36638d878
commit 53a0f7864c
No known key found for this signature in database
GPG key ID: 189A0D6C64F9E873
2 changed files with 26 additions and 0 deletions

View file

@ -2059,6 +2059,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"
@ -654,6 +655,25 @@ func TestEmptyEnv_Allowed(t *testing.T) {
assert.Equal(t, "Cake", 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) {
initJSON()