Skip to content

Commit

Permalink
Adding IsConfigured method to Viper
Browse files Browse the repository at this point in the history
  • Loading branch information
hush-hush committed Dec 10, 2024
1 parent 7aae2e1 commit 13a1be5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,15 @@ func (v *Viper) IsSet(key string) bool {
return val != nil
}

// IsConfigured checks to see if the key has been set in any of the data locations but defaults.
// IsConfigured is case-insensitive for a key.
func IsConfigured(key string) bool { return v.IsConfigured(key) }
func (v *Viper) IsConfigured(key string) bool {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey, true)
return val != nil
}

// AutomaticEnv has Viper check ENV variables for all.
// keys set in config, default & flags
func AutomaticEnv() { v.AutomaticEnv() }
Expand Down
12 changes: 12 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,18 @@ func TestIsSet(t *testing.T) {
assert.True(t, v.IsSet("helloworld"))
}

func TestIsConfigured(t *testing.T) {
v := New()
v.SetDefault("clothing.shoes", true)
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(yamlExample))
assert.True(t, v.IsConfigured("clothing.jacket"))
assert.False(t, v.IsConfigured("clothing.jackets"))
assert.False(t, v.IsConfigured("clothing.shoes"))
v.Set("clothing.shoes", false)
assert.True(t, v.IsConfigured("clothing.shoes"))
}

func TestDirsSearch(t *testing.T) {

root, config, cleanup := initDirs(t)
Expand Down

0 comments on commit 13a1be5

Please sign in to comment.