diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index c52e2526..df8b15bc 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -39,7 +39,7 @@ Note that it consumes more memory than eval. } return cmdEvalAll } -func evaluateAll(cmd *cobra.Command, args []string) error { +func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) { // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence @@ -67,7 +67,11 @@ func evaluateAll(cmd *cobra.Command, args []string) error { } // need to indirectly call the function so that completedSuccessfully is // passed when we finish execution as opposed to now - defer func() { writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) }() + defer func() { + if cmdError == nil { + cmdError = writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) + } + }() } format, err := yqlib.OutputFormatFromString(outputFormat) diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index 2a371ed4..e0e14d27 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -53,7 +53,7 @@ func processExpression(expression string) string { return expression } -func evaluateSequence(cmd *cobra.Command, args []string) error { +func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) { // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence @@ -80,7 +80,11 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { } // need to indirectly call the function so that completedSuccessfully is // passed when we finish execution as opposed to now - defer func() { writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) }() + defer func() { + if cmdError == nil { + cmdError = writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) + } + }() } format, err := yqlib.OutputFormatFromString(outputFormat) diff --git a/pkg/yqlib/file_utils.go b/pkg/yqlib/file_utils.go index 9e252c01..c9a7014e 100644 --- a/pkg/yqlib/file_utils.go +++ b/pkg/yqlib/file_utils.go @@ -7,7 +7,7 @@ import ( "os" ) -func tryRenameFile(from string, to string) { +func tryRenameFile(from string, to string) error { if renameError := os.Rename(from, to); renameError != nil { log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to) log.Debug(renameError.Error()) @@ -15,11 +15,11 @@ func tryRenameFile(from string, to string) { // can't do this rename when running in docker to a file targeted in a mounted volume, // so gracefully degrade to copying the entire contents. if copyError := copyFileContents(from, to); copyError != nil { - panic(fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)) - } else { - tryRemoveTempFile(from) + return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError) } + tryRemoveTempFile(from) } + return nil } func tryRemoveTempFile(filename string) { diff --git a/pkg/yqlib/write_in_place_handler.go b/pkg/yqlib/write_in_place_handler.go index 5460e338..d3bbb487 100644 --- a/pkg/yqlib/write_in_place_handler.go +++ b/pkg/yqlib/write_in_place_handler.go @@ -6,7 +6,7 @@ import ( type writeInPlaceHandler interface { CreateTempFile() (*os.File, error) - FinishWriteInPlace(evaluatedSuccessfully bool) + FinishWriteInPlace(evaluatedSuccessfully bool) error } type writeInPlaceHandlerImpl struct { @@ -39,13 +39,14 @@ func (w *writeInPlaceHandlerImpl) CreateTempFile() (*os.File, error) { return file, err } -func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) { +func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) error { log.Debug("Going to write-inplace, evaluatedSuccessfully=%v, target=%v", evaluatedSuccessfully, w.inputFilename) safelyCloseFile(w.tempFile) if evaluatedSuccessfully { log.Debug("Moving temp file to target") - tryRenameFile(w.tempFile.Name(), w.inputFilename) - } else { - tryRemoveTempFile(w.tempFile.Name()) + return tryRenameFile(w.tempFile.Name(), w.inputFilename) } + tryRemoveTempFile(w.tempFile.Name()) + + return nil }