Skip to content

Commit

Permalink
Add support to save file with no extension (#813)
Browse files Browse the repository at this point in the history
* Add support to save file with no extension

The support introduced for files with no file extension is only partial as trying to save the config file would fail with `<file name> requires valid extension`
This adds support to saving such files
  • Loading branch information
gssbzn committed Feb 19, 2020
1 parent b31a492 commit 97ee7ad
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 146 deletions.
13 changes: 10 additions & 3 deletions viper.go
Expand Up @@ -1413,11 +1413,18 @@ func (v *Viper) SafeWriteConfigAs(filename string) error {

func (v *Viper) writeConfig(filename string, force bool) error {
jww.INFO.Println("Attempting to write configuration to file.")
var configType string

ext := filepath.Ext(filename)
if len(ext) <= 1 {
return fmt.Errorf("filename: %s requires valid extension", filename)
if ext != "" {
configType = ext[1:]
} else {
configType = v.configType
}
configType := ext[1:]
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 97ee7ad

Please sign in to comment.