Do not defer Close() when writing to files
A problematic Write() error might only occur when Close() is called.
However the defer doesn't return an error.
Also, it's fine to close the file, once we're done writing.
@@ -138,10 +138,12 @@ func RenderJobTemplates(
if err != nil {
return err
}
- defer absDestFile.Close()
if err = renderPointer.Render(filepath.Join(jobSrcDir, "templates", source), absDestFile.Name()); err != nil {
return err
}
+ if err = absDestFile.Close(); err != nil {
+ return err
+ }
return nil
})
@@ -170,13 +172,16 @@ func runRenderScript(
return createErr(err)
}
defer os.Remove(tmpFile.Name())
- defer tmpFile.Close()
// Write the pre-render script contents.
if _, err = tmpFile.Write([]byte(script)); err != nil {
return createErr(err)
}
+ if err = tmpFile.Close(); err != nil {
+ return createErr(err)
+ }
+
// Run the pre-render script.
cmd := exec.Command("/bin/bash", tmpFile.Name())
