Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensionless files only allowed when config type is set #827

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -101,6 +101,7 @@ where a configuration file is expected.

```go
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("$HOME/.appname") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
Expand All @@ -124,7 +125,7 @@ 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`
*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

Expand Down
2 changes: 1 addition & 1 deletion viper.go
Expand Up @@ -1976,7 +1976,7 @@ func (v *Viper) searchInPath(in string) (filename string) {
}
}

if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b {
if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b && v.configType != "" {
pedromss marked this conversation as resolved.
Show resolved Hide resolved
return filepath.Join(in, v.configName)
}

Expand Down
18 changes: 18 additions & 0 deletions viper_test.go
Expand Up @@ -307,11 +307,29 @@ func TestBasics(t *testing.T) {
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) {
filename := ".dotfilenoext"
path := "/tmp"
file := filepath.Join(path, filename)
SetConfigName(filename)
SetConfigType("yaml")
AddConfigPath(path)
_, createErr := v.fs.Create(file)
defer func() {
Expand Down