From 19b0e4054cf34a63ccb3c10636b4faf0d78e3cc5 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Tue, 5 Feb 2019 14:04:58 +0100 Subject: [PATCH] If the extension is empty, do not append the dot when building the path This allows to append "" to the viper.SupportedExts, while still set the config type, and have viper not append anything instead of appending just a dot. --- viper.go | 14 ++++++++++---- viper_test.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 5133de7..7eecbbc 100644 --- a/viper.go +++ b/viper.go @@ -1874,10 +1874,16 @@ func (v *Viper) getConfigFile() (string, error) { func (v *Viper) searchInPath(in string) (filename string) { jww.DEBUG.Println("Searching for config in ", in) for _, ext := range SupportedExts { - jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext)) - if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b { - jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext)) - return filepath.Join(in, v.configName+"."+ext) + configFilePlusExt := "" + if ext != "" { + configFilePlusExt = v.configName + "." + ext + } else { + configFilePlusExt = v.configName + } + jww.DEBUG.Println("Checking for", filepath.Join(in, configFilePlusExt)) + if b, _ := exists(v.fs, filepath.Join(in, configFilePlusExt)); b { + jww.DEBUG.Println("Found: ", filepath.Join(in, configFilePlusExt)) + return filepath.Join(in, configFilePlusExt) } } diff --git a/viper_test.go b/viper_test.go index f8364a0..286f85c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1112,6 +1112,26 @@ func TestSub(t *testing.T) { assert.Equal(t, (*Viper)(nil), subv) } +func TestEmptyExtension(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigName("config") + v.SetConfigType("json") + v.AddConfigPath("/etc/app/") + SupportedExts = append(SupportedExts, "") + + err := afero.WriteFile(fs, "/etc/app/config", jsonExample, 0644) + if err != nil { + t.Fatal(err) + } + + err = v.ReadInConfig() + if err != nil { + t.Fatal(err) + } +} + var hclWriteExpected = []byte(`"foos" = { "foo" = { "key" = 1