Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make env variables accessible #1725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
20 changes: 20 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
Loading