From d1c60d9e69e7499dc680a8552b4ea75c2fd2e91b Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Wed, 6 Nov 2019 13:54:13 +0000 Subject: [PATCH] Support config files with no extensions (#722) * Support config files with no extensions * Update README informing config files without extension are supported --- README.md | 2 ++ viper.go | 4 ++++ viper_test.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 171f51c..fe292d0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/viper.go b/viper.go index e9010bc..c64094a 100644 --- a/viper.go +++ b/viper.go @@ -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 "" } diff --git a/viper_test.go b/viper_test.go index f8364a0..8dcfd1a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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"))