Skip to content

Add --jq flag for filtering JSON output#22

Merged
jclusso merged 1 commit into
masterfrom
add-jq-flag
May 31, 2026
Merged

Add --jq flag for filtering JSON output#22
jclusso merged 1 commit into
masterfrom
add-jq-flag

Conversation

@jclusso
Copy link
Copy Markdown
Member

@jclusso jclusso commented May 30, 2026

Summary

Adds a persistent --jq flag that filters JSON output through a jq expression, powered by an embedded gojq engine — no external jq binary required (handy on Windows and in minimal containers). --jq implies --json.

emailable verify x@y.com --jq '.state'                 # → deliverable
emailable account status --jq '.available_credits'
emailable batch get 5cfc... --jq '.emails[] | select(.state=="deliverable") | .email'
  • String results print raw (unquoted, one per line) like jq -r, so they drop into scripts; objects/arrays print as JSON.
  • The filter runs on the verbatim API body via the existing RawJSONProvider passthrough.
  • Compiled once in PersistentPreRunE, so a malformed expression fails fast before any network call.
  • Wired through every JSON-emitting command (verify, batch, account, version, status, logout, skill) for uniform behavior.

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:

  • --stream events are now colorized on a TTY, like all other JSON output.
  • --stream now supports 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.
emailable batch verify emails.csv --stream \
  --jq 'select(.event == "complete") | .emails[] | .email'

A shared marshalDocument helper now backs both the formatter and file writes (replacing the old rawIndented).

Testing

  • internal/output/jq_test.go — raw strings, objects, multi/zero results, raw passthrough, runtime FilterError, 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.
  • Full suite + go vet + gofmt clean; TTY colorization verified through a pty.

Copilot AI review requested due to automatic review settings May 30, 2026 13:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 --jq flag 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.

Comment thread cmd/root.go
@jclusso jclusso force-pushed the add-jq-flag branch 2 times, most recently from 6b47802 to 0c224f8 Compare May 30, 2026 13:35
@jclusso jclusso requested a review from Copilot May 30, 2026 13:45
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 18 changed files in this pull request and generated 1 comment.

Comment thread cmd/root.go Outdated
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.
@jclusso jclusso merged commit 1bc4366 into master May 31, 2026
3 checks passed
@jclusso jclusso deleted the add-jq-flag branch May 31, 2026 14:28
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