mirror of
https://github.com/spf13/viper
synced 2024-12-23 20:17:03 +00:00
Use defaultConfigFile if configFile is not exist
This commit is contained in:
parent
15738813a0
commit
c21acdd422
1 changed files with 20 additions and 2 deletions
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 WriteConfig() error { return v.WriteConfig() }
|
||||||
func (v *Viper) WriteConfig() error {
|
func (v *Viper) WriteConfig() error {
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
filename, err = v.getDefaultConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return v.writeConfig(filename, true)
|
return v.writeConfig(filename, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1220,12 +1223,27 @@ func (v *Viper) WriteConfig() error {
|
||||||
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
func SafeWriteConfig() error { return v.SafeWriteConfig() }
|
||||||
func (v *Viper) SafeWriteConfig() error {
|
func (v *Viper) SafeWriteConfig() error {
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
filename, err = v.getDefaultConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return v.writeConfig(filename, false)
|
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.
|
// WriteConfigAs writes current configuration to a given filename.
|
||||||
func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
|
func WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }
|
||||||
func (v *Viper) WriteConfigAs(filename string) error {
|
func (v *Viper) WriteConfigAs(filename string) error {
|
||||||
|
|
Loading…
Reference in a new issue