Skip to content

Commit 5250a8a

Browse files
committed
fix: Cleanup temp file on failure
1 parent e552e84 commit 5250a8a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

cli/configssh.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func configSSH() *cobra.Command {
309309
// directory as path and renames the temp file to the file provided in
310310
// path. This ensure we avoid trashing the file we are writing due to
311311
// unforeseen circumstance like filesystem full, command killed, etc.
312-
func writeWithTempFileAndMove(path string, r io.Reader) error {
312+
func writeWithTempFileAndMove(path string, r io.Reader) (err error) {
313313
dir := filepath.Dir(path)
314314
name := filepath.Base(path)
315315

@@ -319,6 +319,11 @@ func writeWithTempFileAndMove(path string, r io.Reader) error {
319319
if err != nil {
320320
return xerrors.Errorf("create temp file failed: %w", err)
321321
}
322+
defer func() {
323+
if err != nil {
324+
_ = os.Remove(f.Name()) // Cleanup in case a step failed.
325+
}
326+
}()
322327

323328
_, err = io.Copy(f, r)
324329
if err != nil {

0 commit comments

Comments
 (0)