Use config paths for reading directory trees

This commit is contained in:
Dana NicCaluim 2015-07-14 17:40:09 -07:00
parent 5adc40fc58
commit 786d9a9b95
2 changed files with 13 additions and 7 deletions

View file

@ -751,14 +751,19 @@ func (v *Viper) ReadConfig(in io.Reader) error {
// Loads configuration from a directory tree where filenames are keys
// and file contents are values.
func ReadDir(dirname string) error { return v.ReadDir(dirname) }
func (v *Viper) ReadDir(dirname string) error {
config, err := v.readDir(dirname, "")
func ReadInConfigDir() error { return v.ReadInConfigDir() }
func (v *Viper) ReadInConfigDir() error {
for _, cp := range v.configPaths {
if _, err := os.Stat(cp); err == nil {
config, err := v.readDir(cp, "")
if err == nil {
v.config = config
}
return err
}
}
return nil
}
func (v *Viper) readDir(dirname, keyPrefix string) (map[string]interface{}, error) {
entries, err := ioutil.ReadDir(dirname)

View file

@ -579,7 +579,8 @@ func TestReadDir(t *testing.T) {
}
v := New()
v.ReadDir(dir)
v.AddConfigPath(dir)
v.ReadInConfigDir()
t.Log(v.AllSettings())
assert.True(t, v.GetBool("app.debug"))