Support config files with no extensions (#722)

* Support config files with no extensions

* Update README informing config files without extension are supported
This commit is contained in:
Pedro Silva 2019-11-06 13:54:13 +00:00 committed by Márk Sági-Kazár
parent 72b022eb35
commit d1c60d9e69
3 changed files with 22 additions and 0 deletions

View file

@ -121,6 +121,8 @@ if err := viper.ReadInConfig(); err != nil {
// Config file found and successfully parsed
```
*NOTE:* You can also have a file without an extension and specify the format programmaticaly. For those configuration files that lie in the home of the user without any extension like `.bashrc`
### Writing Config Files
Reading from config files is useful, but at times you want to store all modifications made at run time.

View file

@ -1881,6 +1881,10 @@ func (v *Viper) searchInPath(in string) (filename string) {
}
}
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
return filepath.Join(in, v.configName)
}
return ""
}

View file

@ -278,6 +278,22 @@ func TestBasics(t *testing.T) {
assert.NoError(t, err)
}
func TestSearchInPath(t *testing.T) {
filename := ".dotfilenoext"
path := "/tmp"
file := filepath.Join(path, filename)
SetConfigName(filename)
AddConfigPath(path)
_, createErr := v.fs.Create(file)
defer func() {
_ = v.fs.Remove(file)
}()
assert.NoError(t, createErr)
filename, err := v.getConfigFile()
assert.Equal(t, file, filename)
assert.NoError(t, err)
}
func TestDefault(t *testing.T) {
SetDefault("age", 45)
assert.Equal(t, 45, Get("age"))