Skip to content

[medium] Honour -o for backends returning dict results - #102

Open
elhoim wants to merge 1 commit into
SigmaHQ:mainfrom
elhoim:fix/dict-output-file
Open

elhoim wants to merge 1 commit into
SigmaHQ:mainfrom
elhoim:fix/dict-output-file

Conversation

@elhoim

@elhoim elhoim commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

BLUF

  • Problem: when a backend returns a dict from convert(), the result is always printed to stdout. --output/-o is ignored.
  • Impact: sigma convert -t <backend> -f <dict format> -o result.json rules/ exits 0, but result.json is never created and the JSON goes to the terminal or pipe instead.
  • Fix: pass output to click.echo() in the dict branch, like every other result branch already does. This is a one-line change.
  • Tests: a new CliRunner test (the test backend's convert() is monkeypatched to return a dict) checks that the JSON lands in the -o file and not on stdout. It fails on main and passes with the fix.

Priority: medium

Details

Root cause: sigma/cli/convert.py:584-585

elif isinstance(result, dict):
    click.echo(bytes(json.dumps(result, indent=json_indent), encoding))   # no `output`

The str, bytes, list-of-str and list-of-dict branches (convert.py:556, 563, 569, 575-583) all pass output. After the fix:

    click.echo(bytes(json.dumps(result, indent=json_indent), encoding), output)

No backend in this repository's test environment returns a plain dict, so the test simulates one by monkeypatching TextQueryTestBackend.convert. The rest of the CLI code runs unchanged.

Testing

  • New test: tests/test_convert.py::test_convert_output_dict_to_file. It fails on main because the JSON is on stdout and the file is empty.
  • Full suite, run the way CI runs it (Python 3.12, dependencies pinned to poetry.lock: pySigma 1.4.0, click 8.4.2, pyparsing 3.3.2): pytest --cov=sigma → 119 passed, 1 skipped.

🤖 Generated with Claude Code

The dict branch of the single-output conversion called click.echo() without the output file, so the JSON went to stdout and the file given with --output/-o was never written, although the command exited 0. Pass the output file like all other result types do.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The change is minimal, consistent with existing output handling, and includes a focused regression test that fails on main and passes with the fix.

Review effort: Lite
Findings: None

What changed in this PR

This PR fixes sigma convert so that when a backend’s convert() returns a plain dict, the CLI correctly honors --output/-o by writing the JSON to the provided output file instead of always emitting to stdout.

Changes:

  • Pass the output file handle to click.echo() for the dict result branch in sigma/cli/convert.py.
  • Add a regression test that monkeypatches the test backend to return a dict and asserts the JSON is written to the -o file (and not stdout).
File Description
sigma/​cli/​convert.py Fixes the dict output path to write via the configured --output/-o stream.
tests/​test_convert.py Adds coverage ensuring dict results are written to the output file rather than stdout.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants