Skip to content

Commit cd726b2

Browse files
authored
fix: make sure file close returns any errors (#251)
Using the simple defer is a nice way to close the file, but if the file write/close returns an error we won't know that. Using the named return value allows returning the close error.
1 parent 73640ad commit cd726b2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

cloud/config/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ func FromDir(path string) (*AuthConfig, error) {
6262
}
6363
}
6464

65-
func getConfigFromFile(path string) (*AuthConfig, error) {
66-
f, err := os.Open(path)
65+
func getConfigFromFile(path string) (authConfig *AuthConfig, err error) {
66+
var f *os.File
67+
f, err = os.Open(path)
6768
if err != nil {
6869
return nil, err
6970
}
7071

71-
defer f.Close()
72+
defer func() {
73+
if closeErr := f.Close(); closeErr != nil {
74+
err = closeErr
75+
}
76+
}()
7277

7378
if err != nil {
7479
return nil, err

0 commit comments

Comments
 (0)