From 6067137878df26550030a80407e5db6991bef03c Mon Sep 17 00:00:00 2001 From: Greg Szabo Date: Sat, 4 Jul 2020 21:52:36 -0400 Subject: [PATCH] WriteConfig does not fail on unknown file extensions when the config type is set --- viper.go | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/viper.go b/viper.go index f61f4ed755..cb6cc27ecd 100644 --- a/viper.go +++ b/viper.go @@ -1411,7 +1411,7 @@ func (v *Viper) WriteConfig() error { if err != nil { return err } - return v.writeConfig(filename, true) + return v.writeConfig(filename, v.getConfigType(), true) } // SafeWriteConfig writes current configuration to file only if the file does not exist. @@ -1426,7 +1426,7 @@ func (v *Viper) SafeWriteConfig() error { // WriteConfigAs writes current configuration to a given filename. func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) } func (v *Viper) WriteConfigAs(filename string) error { - return v.writeConfig(filename, true) + return v.writeConfig(filename, filepath.Ext(filename), true) } // SafeWriteConfigAs writes current configuration to a given filename if it does not exist. @@ -1436,22 +1436,11 @@ func (v *Viper) SafeWriteConfigAs(filename string) error { if alreadyExists && err == nil { return ConfigFileAlreadyExistsError(filename) } - return v.writeConfig(filename, false) + return v.writeConfig(filename, filepath.Ext(filename), false) } -func (v *Viper) writeConfig(filename string, force bool) error { +func (v *Viper) writeConfig(filename string, configType string, force bool) error { jww.INFO.Println("Attempting to write configuration to file.") - var configType string - - ext := filepath.Ext(filename) - if ext != "" { - configType = ext[1:] - } else { - configType = v.configType - } - if configType == "" { - return fmt.Errorf("config type could not be determined for %s", filename) - } if !stringInSlice(configType, SupportedExts) { return UnsupportedConfigError(configType)