mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
Fix for #549 Add O_CREATE flag to the non-force branch of writeConfig
Also add test for the method
This commit is contained in:
parent
62edee3196
commit
15be5e8700
2 changed files with 21 additions and 1 deletions
2
viper.go
2
viper.go
|
@ -1310,7 +1310,7 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
||||||
} else {
|
} else {
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
flags = os.O_WRONLY
|
flags = os.O_CREATE | os.O_WRONLY
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
|
return fmt.Errorf("File: %s exists. Use WriteConfig to overwrite.", filename)
|
||||||
}
|
}
|
||||||
|
|
|
@ -987,6 +987,26 @@ p_ppu = 0.55
|
||||||
p_batters.batter.type = Regular
|
p_batters.batter.type = Regular
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
func TestSafeWriteConfigProperties(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
v.SetFs(fs)
|
||||||
|
v.SetConfigName("c")
|
||||||
|
v.SetConfigType("properties")
|
||||||
|
err := v.ReadConfig(bytes.NewBuffer(propertiesExample))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := v.SafeWriteConfigAs("c.properties"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
read, err := afero.ReadFile(fs, "c.properties")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, propertiesWriteExpected, read)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteConfigProperties(t *testing.T) {
|
func TestWriteConfigProperties(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
Loading…
Reference in a new issue