Extensionless files only allowed when config type is set (#827)

* Only consider files without extension if the config type is explicitly specified

* Hides unused variable in test

* First check for config type then for file without extension
This commit is contained in:
Pedro Silva 2020-01-16 18:23:50 +00:00 committed by Márk Sági-Kazár
parent 06ab5a4b62
commit 9cd571279d
3 changed files with 24 additions and 3 deletions

View File

@ -101,6 +101,7 @@ where a configuration file is expected.
```go ```go
viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/appname/") // path to look for the config file in viper.AddConfigPath("/etc/appname/") // path to look for the config file in
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory viper.AddConfigPath(".") // optionally look for config in the working directory
@ -124,7 +125,7 @@ if err := viper.ReadInConfig(); err != nil {
// Config file found and successfully parsed // 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` *NOTE [since 1.6]:* 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 ### Writing Config Files

View File

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

View File

@ -307,11 +307,29 @@ func TestBasics(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestSearchInPath_WithoutConfigTypeSet(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)
_, err := v.getConfigFile()
// unless config type is set, files without extension
// are not considered
assert.Error(t, err)
}
func TestSearchInPath(t *testing.T) { func TestSearchInPath(t *testing.T) {
filename := ".dotfilenoext" filename := ".dotfilenoext"
path := "/tmp" path := "/tmp"
file := filepath.Join(path, filename) file := filepath.Join(path, filename)
SetConfigName(filename) SetConfigName(filename)
SetConfigType("yaml")
AddConfigPath(path) AddConfigPath(path)
_, createErr := v.fs.Create(file) _, createErr := v.fs.Create(file)
defer func() { defer func() {