Skip to content

Commit

Permalink
WriteConfig does not fail on unknown file extensions when the config …
Browse files Browse the repository at this point in the history
…type is set
  • Loading branch information
greg-szabo committed Jul 5, 2020
1 parent 13df721 commit 6067137
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions viper.go
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit 6067137

Please sign in to comment.