mirror of https://github.com/spf13/viper.git
Use defaultConfigFile if configFile is not exist
This commit is contained in:
parent
15738813a0
commit
c21acdd422
18
viper.go
18
viper.go
|
@ -1210,9 +1210,12 @@ func (v *Viper) MergeConfig(in io.Reader) error {
|
|||
func WriteConfig() error { return v.WriteConfig() }
|
||||
func (v *Viper) WriteConfig() error {
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
filename, err = v.getDefaultConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return v.writeConfig(filename, true)
|
||||
}
|
||||
|
||||
|
@ -1220,12 +1223,27 @@ func (v *Viper) WriteConfig() error {
|
|||
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
||||
func (v *Viper) SafeWriteConfig() error {
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
filename, err = v.getDefaultConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return v.writeConfig(filename, false)
|
||||
}
|
||||
|
||||
func (v *Viper) getDefaultConfigFile() (string, error) {
|
||||
if v.configName == "" {
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
}
|
||||
|
||||
if len(v.configPaths) == 0 {
|
||||
return v.configName + "." + SupportedExts[0], nil
|
||||
} else {
|
||||
return filepath.Join(v.configPaths[0], v.configName+"."+SupportedExts[0]), nil
|
||||
}
|
||||
}
|
||||
|
||||
// WriteConfigAs writes current configuration to a given filename.
|
||||
func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
|
||||
func (v *Viper) WriteConfigAs(filename string) error {
|
||||
|
|
Loading…
Reference in New Issue