Skip to content

Commit 803055a

Browse files
committed
Fix atomicwrite for Windows
os.Rename does not overwrite on Windows
1 parent e639ee7 commit 803055a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

config/config.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package config
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
67
"path/filepath"
78
"strings"
8-
"io/ioutil"
99

1010
"github.com/fnproject/fn_go/provider"
1111
"github.com/spf13/viper"
@@ -72,6 +72,7 @@ func ensureConfiguration() error {
7272
if err != nil {
7373
return fmt.Errorf("error creating config.yaml file %v", err)
7474
}
75+
defer file.Close()
7576

7677
err = WriteYamlFile(file.Name(), defaultRootConfigContents)
7778
if err != nil {
@@ -87,10 +88,11 @@ func ensureConfiguration() error {
8788

8889
defaultContextPath := filepath.Join(contextsPath, defaultContextFileName)
8990
if _, err := os.Stat(defaultContextPath); os.IsNotExist(err) {
90-
_, err = os.Create(defaultContextPath)
91+
f, err := os.Create(defaultContextPath)
9192
if err != nil {
9293
return fmt.Errorf("error creating default.yaml context file %v", err)
9394
}
95+
defer f.Close()
9496

9597
err = WriteYamlFile(defaultContextPath, DefaultContextConfigContents)
9698
if err != nil {
@@ -149,13 +151,7 @@ func WriteConfigValueToConfigFile(key, value string) error {
149151
home := GetHomeDir()
150152

151153
configFilePath := filepath.Join(home, rootConfigPathName, contextConfigFileName)
152-
f, err := os.OpenFile(configFilePath, os.O_RDWR, ReadWritePerms)
153-
if err != nil {
154-
return err
155-
}
156-
defer f.Close()
157-
158-
file, err := DecodeYAMLFile(f.Name())
154+
file, err := DecodeYAMLFile(configFilePath)
159155
if err != nil {
160156
return err
161157
}
@@ -170,7 +166,7 @@ func WriteConfigValueToConfigFile(key, value string) error {
170166
}
171167
configValues[key] = value
172168

173-
err = atomicwrite(f.Name(), &configValues)
169+
err = atomicwrite(configFilePath, &configValues)
174170
if err != nil {
175171
return err
176172
}
@@ -187,13 +183,14 @@ func atomicwrite(file string, c *ContextMap) (err error) {
187183
return fmt.Errorf("cannot create temp file: %v", err)
188184
}
189185

190-
defer f.Close()
191186
defer os.Remove(f.Name())
192187

193188
err = WriteYamlFile(f.Name(), c)
194189
if err != nil {
190+
f.Close()
195191
return err
196192
}
193+
f.Close()
197194

198195
info, err := os.Stat(file)
199196
if err != nil {
@@ -202,10 +199,12 @@ func atomicwrite(file string, c *ContextMap) (err error) {
202199
_ = os.Chmod(f.Name(), info.Mode())
203200
}
204201

202+
os.Remove(file)
203+
205204
// replace file with the tempfile
206205
err = os.Rename(f.Name(), file)
207-
if err !=nil {
208-
return fmt.Errorf("error replacing file with tempfile")
206+
if err != nil {
207+
return fmt.Errorf("error replacing file with tempfile: %v", err)
209208
}
210209
return nil
211-
}
210+
}

0 commit comments

Comments
 (0)