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
11
viper.go
11
viper.go
|
@ -751,13 +751,18 @@ 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) {
|
||||
|
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in a new issue