mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
feat: make env variables accessible
This commit is contained in:
parent
e36638d878
commit
53a0f7864c
2 changed files with 26 additions and 0 deletions
6
viper.go
6
viper.go
|
@ -2059,6 +2059,12 @@ func (v *Viper) watchRemoteConfig(provider RemoteProvider) (map[string]any, erro
|
||||||
return v.kvstore, err
|
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.
|
// AllKeys returns all keys holding a value, regardless of where they are set.
|
||||||
// Nested keys are returned with a v.keyDelim separator.
|
// Nested keys are returned with a v.keyDelim separator.
|
||||||
func AllKeys() []string { return v.AllKeys() }
|
func AllKeys() []string { return v.AllKeys() }
|
||||||
|
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/spf13/viper/internal/features"
|
"github.com/spf13/viper/internal/features"
|
||||||
"github.com/spf13/viper/internal/testutil"
|
"github.com/spf13/viper/internal/testutil"
|
||||||
|
@ -654,6 +655,25 @@ func TestEmptyEnv_Allowed(t *testing.T) {
|
||||||
assert.Equal(t, "Cake", Get("name"))
|
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) {
|
func TestEnvPrefix(t *testing.T) {
|
||||||
initJSON()
|
initJSON()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue