mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
Use config paths for reading directory trees
This commit is contained in:
parent
5adc40fc58
commit
786d9a9b95
2 changed files with 13 additions and 7 deletions
17
viper.go
17
viper.go
|
@ -751,13 +751,18 @@ func (v *Viper) ReadConfig(in io.Reader) error {
|
||||||
|
|
||||||
// Loads configuration from a directory tree where filenames are keys
|
// Loads configuration from a directory tree where filenames are keys
|
||||||
// and file contents are values.
|
// and file contents are values.
|
||||||
func ReadDir(dirname string) error { return v.ReadDir(dirname) }
|
func ReadInConfigDir() error { return v.ReadInConfigDir() }
|
||||||
func (v *Viper) ReadDir(dirname string) error {
|
func (v *Viper) ReadInConfigDir() error {
|
||||||
config, err := v.readDir(dirname, "")
|
for _, cp := range v.configPaths {
|
||||||
if err == nil {
|
if _, err := os.Stat(cp); err == nil {
|
||||||
v.config = config
|
config, err := v.readDir(cp, "")
|
||||||
|
if err == nil {
|
||||||
|
v.config = config
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) readDir(dirname, keyPrefix string) (map[string]interface{}, error) {
|
func (v *Viper) readDir(dirname, keyPrefix string) (map[string]interface{}, error) {
|
||||||
|
|
|
@ -579,7 +579,8 @@ func TestReadDir(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
v := New()
|
v := New()
|
||||||
v.ReadDir(dir)
|
v.AddConfigPath(dir)
|
||||||
|
v.ReadInConfigDir()
|
||||||
t.Log(v.AllSettings())
|
t.Log(v.AllSettings())
|
||||||
|
|
||||||
assert.True(t, v.GetBool("app.debug"))
|
assert.True(t, v.GetBool("app.debug"))
|
||||||
|
|
Loading…
Reference in a new issue