Skip to content

feat(filter): add --jq global flag for jq-style output filtering#590

Merged
platinummonkey merged 4 commits into
mainfrom
feat/jq-filter-features
Jun 18, 2026
Merged

feat(filter): add --jq global flag for jq-style output filtering#590
platinummonkey merged 4 commits into
mainfrom
feat/jq-filter-features

Conversation

@platinummonkey

Copy link
Copy Markdown
Collaborator

Summary

Adds a global --jq <EXPR> flag that applies a jq expression to any command's JSON response before output formatting. Works with every -o format (json, yaml, table, csv, tsv). Built on jaq — a pure-Rust, security-audited jq implementation with no C dependencies (cross-compiles to all 4 release targets and the browser WASM feature).

pup monitors list --jq '.[].name'
pup monitors list --jq '.[] | select(.name | endswith("prod"))' -o table
pup logs search --query="status:error" --jq '.data | length'

Changes

  • src/filter.rs (new) — apply_jq(value, expr) encapsulates the jaq v3 API (Loader, Arena, Compiler, Ctx); collapses output stream: 0 → null, 1 → unwrapped, ≥2 → array. 8 unit tests covering happy paths, edge cases, and invalid-expression error handling.
  • Cargo.toml — adds jaq-core 3.1.0, jaq-std 3.0.1, jaq-json 2.0.1 (with features = ["serde"])
  • src/formatter.rs — serialize-once refactor: serde_json::to_value(data) called once in format_and_print, then &serde_json::Value passed to all 5 renderers and build_agent_envelope; --jq filter applied before envelope/sort/rendering
  • src/config.rs — adds pub jq: Option<String> field
  • src/main.rs — adds #[arg(long, global = true)] jq: Option<String> to Cli
  • src/extensions/mod.rsPreParsedGlobals parses --jq / --jq= forms; apply_to sets cfg.jq
  • src/extensions/exec.rs — injects PUP_FILTER env var so extensions can self-apply the filter
  • All 14 direct format_and_print call sites updated to pass cfg.jq.as_deref()
  • docs/COMMANDS.md — documents --jq, cardinality-collapse behavior, and the bypass caveat for commands that print directly

Testing

  • cargo build
  • cargo test ✅ — 1284 passed; 7 pre-existing DNS/platform failures unrelated to this change
  • cargo clippy -- -D warnings
  • cargo fmt --check

Related Issues

Closes #N/A — new feature per internal request


🤖 Generated with Claude Code

Add a global `--jq <EXPR>` flag that applies a jq expression to any
command's JSON response before output formatting, making it composable
with every `-o` format (json, yaml, table, csv, tsv).

- src/filter.rs (new): encapsulates jaq engine (jaq-core + jaq-std +
  jaq-json). Single apply_jq(value, expr) function; collapses output
  stream: 0 → Null, 1 → unwrapped, ≥2 → Array. 8 unit tests.
- Cargo.toml: add jaq-core 3.1.0, jaq-std 3.0.1, jaq-json 2.0.1
  (features = ["serde"])
- src/config.rs: add pub jq: Option<String> field
- src/formatter.rs: serialize-once refactor — all 5 renderers +
  build_agent_envelope now take &serde_json::Value; filter applied in
  format_and_print before envelope/sort/rendering
- src/main.rs: #[arg(long, global = true)] jq: Option<String> on Cli
- src/extensions/mod.rs: PreParsedGlobals parses --jq / --jq=
- src/extensions/exec.rs: injects PUP_FILTER env var for extensions
- All direct format_and_print call sites updated (cfg.jq.as_deref())
- docs/COMMANDS.md: document --jq, cardinality behavior, bypass caveat

Environment: Datadog workspace

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@platinummonkey platinummonkey requested a review from a team as a code owner June 16, 2026 20:38
@platinummonkey platinummonkey marked this pull request as draft June 16, 2026 20:50
@platinummonkey

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b76072f1b3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/formatter.rs Outdated
platinummonkey and others added 3 commits June 16, 2026 21:20
…n agent mode

When --jq rewrites the payload before the agent envelope is built, the
caller's count/truncated fields (computed pre-filter) no longer describe
.data. This misleads agents relying on the envelope's count field.

- strip_counts_after_filter: drops count/truncated when --jq active;
  keeps command/next_action. Metadata.skip_serializing_if makes the
  omitted fields disappear from JSON.
- append_jq_note: extends metadata.note with JQ_FILTER_NOTE when --jq
  ran, so agents know --jq targets the raw payload (write .[], not
  .data[]).
- format_and_print agent-mode JSON branch: threads metadata through
  strip_counts_after_filter then calls append_jq_note on the envelope.
  The jq == None path is byte-for-byte identical to before.
- 5 new unit tests: strip_counts (None→None, populated→stripped),
  append_jq_note (both notes present), integration (count omitted +
  both notes), regression (no --jq keeps count and note unchanged).
- docs/COMMANDS.md: documents that --jq targets the payload (write
  .[], not .data[]) and that count/truncated are omitted in agent mode.

Environment: Datadog workspace

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Three test-only Config struct initializers were missing the newly-added
`jq: Option<String>` field, causing `error[E0063]` when CI built with
`--all-targets`. Local `cargo clippy` without that flag skipped test
binaries, so the breakage only surfaced in CI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@platinummonkey platinummonkey marked this pull request as ready for review June 17, 2026 19:55
@platinummonkey platinummonkey merged commit 8b65c7c into main Jun 18, 2026
6 checks passed
@platinummonkey platinummonkey deleted the feat/jq-filter-features branch June 18, 2026 16:59
jack-edmonds-dd added a commit that referenced this pull request Jun 18, 2026
Config gained a jq field in PR #590 but one test initializer in
config.rs was missed, causing clippy --all-targets to fail.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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