Add --jq flag for filtering JSON output#22
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an embedded --jq JSON filtering capability to the CLI, using gojq so users can filter machine output without an external jq binary. The change routes JSON-emitting commands and batch stream events through shared formatter helpers so filtering is applied consistently.
Changes:
- Adds
output.Query, jq compilation/evaluation, filtered JSON rendering, and compact NDJSON formatter support. - Wires the persistent
--jqflag through root command setup and JSON output call sites. - Updates docs and tests for raw string output, compact streaming, filter errors, and file/formatter behavior.
Reviewed changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents --jq usage and stream filtering examples. |
| go.mod | Adds gojq and its indirect dependency. |
| go.sum | Records checksums for new dependencies. |
| internal/output/jq.go | Adds compiled jq query wrapper and result collection. |
| internal/output/json.go | Adds filtering, compact rendering, shared marshaling, and filter error handling. |
| internal/output/file.go | Reuses shared JSON document marshaling for file writes. |
| internal/output/jq_test.go | Adds formatter and jq behavior tests. |
| cmd/root.go | Adds persistent --jq flag, compilation, and formatter constructors. |
| cmd/verify.go | Routes verify output through jq-aware formatter helper. |
| cmd/batch.go | Routes batch output/stream events through jq-aware JSON formatter. |
| cmd/account.go | Routes account output through jq-aware formatter helper. |
| cmd/version.go | Routes version JSON output through jq-aware formatter helper. |
| cmd/status.go | Routes status JSON output through jq-aware formatter helper. |
| cmd/logout.go | Routes logout JSON output through jq-aware formatter helper. |
| cmd/skill.go | Routes skill JSON output through jq-aware formatter helper. |
| cmd/jq_test.go | Adds command-level jq behavior and streaming tests. |
| cmd/batch_e2e_test.go | Updates streamer tests for compact JSON formatter usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6b47802 to
0c224f8
Compare
Adds a persistent --jq flag that filters JSON output through a jq expression, powered by an embedded gojq engine so no external jq binary is required (useful on Windows and in minimal containers). --jq implies --json. String results print raw (unquoted, one per line) like `jq -r`; objects and arrays print as JSON. The filter operates on the verbatim API body via the existing RawJSONProvider passthrough, and is compiled once in PersistentPreRunE so a malformed expression fails fast before any network call. Unify the streaming path through the JSON formatter while here. The batch streamer previously marshaled NDJSON inline, bypassing both colorization and --jq; it now routes through a compact JSON formatter, so --stream events pick up TTY colorization and per-event jq filtering. Events whose filter errors (e.g. a complete-event filter hitting a progress event) are skipped rather than aborting the stream. Add a shared marshalDocument helper used by both the formatter and file writes, replacing the old rawIndented.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a persistent
--jqflag that filters JSON output through a jq expression, powered by an embedded gojq engine — no externaljqbinary required (handy on Windows and in minimal containers).--jqimplies--json.jq -r, so they drop into scripts; objects/arrays print as JSON.RawJSONProviderpassthrough.PersistentPreRunE, so a malformed expression fails fast before any network call.Streaming unification
The batch streamer previously marshaled NDJSON inline, bypassing both colorization and
--jq. It now routes through a compact JSON formatter, which fixes two things at once:--streamevents are now colorized on a TTY, like all other JSON output.--streamnow supports per-event--jqfiltering. Events whose filter errors (e.g. acomplete-event filter hitting aprogressevent) are skipped rather than aborting the stream.emailable batch verify emails.csv --stream \ --jq 'select(.event == "complete") | .emails[] | .email'A shared
marshalDocumenthelper now backs both the formatter and file writes (replacing the oldrawIndented).Testing
internal/output/jq_test.go— raw strings, objects, multi/zero results, raw passthrough, runtimeFilterError, no-HTML-escape, compact mode, bad-expr.cmd/jq_test.go— implies-JSON, fail-fast, per-event stream filtering, skip-on-error, no stale query across in-process runs.go vet+gofmtclean; TTY colorization verified through a pty.