mirror of https://github.com/spf13/viper.git
Merge b783f57e01
into 30f38f6360
This commit is contained in:
commit
bdfda6150f
22
viper.go
22
viper.go
|
@ -1644,17 +1644,27 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
if v.config == nil {
|
if v.config == nil {
|
||||||
v.config = make(map[string]any)
|
v.config = make(map[string]any)
|
||||||
}
|
}
|
||||||
flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
|
||||||
if !force {
|
exists, err := afero.Exists(v.fs, filename)
|
||||||
flags |= os.O_EXCL
|
if err != nil {
|
||||||
}
|
return err
|
||||||
f, err := v.fs.OpenFile(filename, flags, v.configPermissions)
|
}
|
||||||
|
if exists && !force {
|
||||||
|
return fmt.Errorf("file already exists: %s", filename)
|
||||||
|
}
|
||||||
|
f, err := afero.TempFile(v.fs, filepath.Dir(filename), filepath.Base(filename)+"-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
if err := v.marshalWriter(f, configType); err != nil {
|
if err := v.marshalWriter(f, configType); err != nil {
|
||||||
|
f.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
err = v.fs.Rename(f.Name(), filename)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue