mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +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
11
viper.go
11
viper.go
|
@ -751,14 +751,19 @@ 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 := os.Stat(cp); err == nil {
|
||||||
|
config, err := v.readDir(cp, "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
v.config = config
|
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) {
|
||||||
entries, err := ioutil.ReadDir(dirname)
|
entries, err := ioutil.ReadDir(dirname)
|
||||||
|
|
|
@ -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