From dbcf4b53b84c52d7c18976e25d85b31f01f266aa Mon Sep 17 00:00:00 2001 From: orxobo <48706811+orxobo@users.noreply.github.com> Date: Tue, 11 Feb 2020 17:05:34 +1000 Subject: [PATCH] Fixed Write bugs This should address a fix #93 and #94 This has not been addressed in any tests. --- godotenv.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/godotenv.go b/godotenv.go index 330f1c2..69e816c 100644 --- a/godotenv.go +++ b/godotenv.go @@ -147,15 +147,20 @@ func Exec(filenames []string, cmd string, cmdArgs []string) error { // Write serializes the given environment and writes it to a file func Write(envMap map[string]string, filename string) error { - content, error := Marshal(envMap) - if error != nil { - return error + content, err := Marshal(envMap) + if err != nil { + return err + } + file, err := os.Create(filename) + if err != nil { + return err } - file, error := os.Create(filename) - if error != nil { - return error + defer file.Close() + _, err = file.WriteString(content) + if err != nil { + return err } - _, err := file.WriteString(content) + file.Sync() return err }