diff --git a/flow/record/tools/rdump.py b/flow/record/tools/rdump.py index 974cbf6..3ec4c40 100644 --- a/flow/record/tools/rdump.py +++ b/flow/record/tools/rdump.py @@ -321,8 +321,14 @@ def main(argv: list[str] | None = None) -> int: root_logger.handlers.clear() root_logger.addHandler(handler) - fields_to_exclude = args.exclude.split(",") if args.exclude else [] - fields = args.fields.split(",") if args.fields else [] + fields_to_exclude = list(filter(None, map(str.strip, args.exclude.split(",")))) if args.exclude else [] + fields = list(filter(None, map(str.strip, args.fields.split(",")))) if args.fields else [] + + writer_options = {} + if fields: + writer_options["fields"] = fields + if fields_to_exclude: + writer_options["exclude"] = fields_to_exclude if args.list_adapters: list_adapters() @@ -340,8 +346,6 @@ def main(argv: list[str] | None = None) -> int: } uri = mode_to_uri.get(args.mode, uri) qparams = { - "fields": args.fields, - "exclude": args.exclude, "format_spec": args.format, } query = urlencode({k: v for k, v in qparams.items() if v}) @@ -393,7 +397,7 @@ def main(argv: list[str] | None = None) -> int: ret = 0 try: - with RecordWriter(uri) as record_writer: + with RecordWriter(uri, **writer_options) as record_writer: for count, rec in enumerate(record_iterator, start=1): # noqa: B007 if args.record_source is not None: rec._source = args.record_source diff --git a/tests/tools/test_rdump.py b/tests/tools/test_rdump.py index 6a364bd..6e06726 100644 --- a/tests/tools/test_rdump.py +++ b/tests/tools/test_rdump.py @@ -904,3 +904,40 @@ def test_rdump_print_error_notes( rdump.main([str(path), "-vvv"]) capsys.readouterr() + + +def test_rdump_fields_with_spaces(tmp_path: Path, capsysbinary: pytest.CaptureFixture) -> None: + """Test if rdump handles spaces in field names gracefully.""" + TestRecord = RecordDescriptor( + "test/record", + [ + ("varint", "count"), + ("string", "foo"), + ("string", "bar"), + ], + ) + + path = tmp_path / "test.records" + out_path = tmp_path / "out.records" + with RecordWriter(path) as writer: + writer.write(TestRecord(count=0, foo="bar", bar="baz")) + + # test if fields works with spaces in the name + rdump.main([str(path), "--fields", "foo, count ", "-w", str(out_path)]) + with RecordReader(out_path) as reader: + records = list(reader) + assert len(records) == 1 + assert list(records[0]._desc.fields.keys()) == ["foo", "count"] + + # test if exclude works with spaces in the field names + rdump.main([str(path), "--exclude", " foo, bar ", "-w", str(out_path)]) + with RecordReader(out_path) as reader: + records = list(reader) + assert len(records) == 1 + assert list(records[0]._desc.fields.keys()) == ["count"] + + # also test an adapter + rdump.main([str(path), "--exclude", " foo, bar ", "--csv"]) + captured = capsysbinary.readouterr() + assert captured.err == b"" + assert b"count,_source,_classification,_generated,_version\r\n" in captured.out