From 8ffa618dbefb01b8528026c5b8db3db61e780ee5 Mon Sep 17 00:00:00 2001 From: JSCU-CNI <121175071+JSCU-CNI@users.noreply.github.com> Date: Thu, 7 Aug 2025 13:28:01 +0200 Subject: [PATCH] add --ignore-empty-record-stream flag --- flow/record/tools/rdump.py | 20 +++++++++++++++----- tests/test_rdump.py | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/flow/record/tools/rdump.py b/flow/record/tools/rdump.py index dc458a77..a4509d76 100644 --- a/flow/record/tools/rdump.py +++ b/flow/record/tools/rdump.py @@ -13,6 +13,7 @@ import flow.record.adapter from flow.record import RecordWriter, iter_timestamped_records, record_stream +from flow.record.exceptions import RecordAdapterNotFound from flow.record.selector import make_selector from flow.record.stream import RecordFieldRewriter from flow.record.utils import LOGGING_TRACE_LEVEL, catch_sigpipe @@ -143,6 +144,11 @@ def main(argv: list[str] | None = None) -> int: action="store_true", help="Show count of processed records", ) + output.add_argument( + "--ignore-empty-record-stream", + action="store_true", + help="Exit successfully if no records can be read", + ) advanced = parser.add_argument_group("advanced") advanced.add_argument( @@ -315,13 +321,17 @@ def main(argv: list[str] | None = None) -> int: record_writer.write(rec) except Exception as e: - print_error(e) + if args.ignore_empty_record_stream and count == 0 and isinstance(e, RecordAdapterNotFound): + log.warning("No records were read from the input stream") + ret = 0 + else: + print_error(e) - # Prevent throwing an exception twice when deconstructing the record writer. - if hasattr(record_writer, "exception") and record_writer.exception is e: - record_writer.exception = None + # Prevent throwing an exception twice when deconstructing the record writer. + if hasattr(record_writer, "exception") and record_writer.exception is e: + record_writer.exception = None - ret = 1 + ret = 1 finally: if record_writer: diff --git a/tests/test_rdump.py b/tests/test_rdump.py index 96b6025f..81d0eba4 100644 --- a/tests/test_rdump.py +++ b/tests/test_rdump.py @@ -90,6 +90,28 @@ def test_rdump_pipe(tmp_path: Path) -> None: assert len(records) == 3 assert {r.count for r in records} == {1, 3, 9} + # Test if rdump exits with exit code 1 when no records can be read from stdin + p1 = subprocess.Popen(["echo", ""], stdout=subprocess.PIPE) + p2 = subprocess.Popen(["rdump", "-"], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p2.communicate() + + assert p2.returncode == 1 + assert b"rdump encountered a fatal error: Could not find adapter for file-like object" in stderr + + # Test if rdump exits with exit code 0 when no records can be read from stdin + # and --ignore-empty-record-stream is set + p1 = subprocess.Popen(["echo", ""], stdout=subprocess.PIPE) + p2 = subprocess.Popen( + ["rdump", "-v", "-", "--ignore-empty-record-stream"], + stdin=p1.stdout, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + stdout, stderr = p2.communicate() + + assert p2.returncode == 0 + assert b"No records were read from the input stream" in stderr + def test_rdump_format_template(tmp_path: Path) -> None: TestRecord = RecordDescriptor(