-
Notifications
You must be signed in to change notification settings - Fork 20
Add built-in --jq flag via gojq #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
291b655
Add built-in --jq flag via gojq
robzolkos 543fd7c
Address PR review feedback
robzolkos e411a3b
Pass error envelopes through jqWriter unfiltered
robzolkos 596d96b
Add jq error types, early validation, env access, and compact output
robzolkos 69fd551
Reject --jq for version command, output plain text
robzolkos 41df91a
Address PR review feedback
robzolkos d807623
Address PR review feedback
robzolkos d9c2cb8
Compile jq expression once, reuse in writer
robzolkos 8770751
Deduplicate jq compilation logic
robzolkos 99875a2
Fix jq error bypass to use resolved format and outWriter
robzolkos f3a568a
Fall back to JSON format when resolveFormat fails in jq error bypass
robzolkos 4c53371
Fix commands help text after rebase
robzolkos 0db6008
Clarify jq JSON-only semantics
robzolkos b2cc641
Document jq command exceptions in skill docs
robzolkos f829257
Fix jq error propagation, restore structured version output, and reje…
robzolkos 2d64393
Rename jq output error tracker for lint
robzolkos bbf6372
Clarify --jq skill command exception in docs
robzolkos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/basecamp/fizzy-cli/internal/errors" | ||
| "github.com/itchyny/gojq" | ||
| ) | ||
|
|
||
| // jqWriter wraps an io.Writer and applies a compiled jq filter to JSON output. | ||
| // Non-JSON writes pass through unchanged. | ||
| type jqWriter struct { | ||
| dest io.Writer | ||
| code *gojq.Code | ||
| } | ||
|
|
||
| // newJQWriter parses and compiles the jq expression and returns a filtering writer. | ||
| // Delegates to compileJQ for compilation so options are maintained in one place. | ||
| func newJQWriter(dest io.Writer, filter string) (*jqWriter, error) { | ||
| code, err := compileJQ(filter) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &jqWriter{dest: dest, code: code}, nil | ||
| } | ||
|
|
||
| // newJQWriterWithCode creates a jqWriter using a pre-compiled *gojq.Code. | ||
| // Used when the expression has already been validated and compiled (e.g. in PersistentPreRunE). | ||
| func newJQWriterWithCode(dest io.Writer, code *gojq.Code) *jqWriter { | ||
| return &jqWriter{dest: dest, code: code} | ||
| } | ||
|
|
||
| // compileJQ parses and compiles a jq expression, returning the compiled code. | ||
| func compileJQ(filter string) (*gojq.Code, error) { | ||
| query, err := gojq.Parse(filter) | ||
| if err != nil { | ||
| return nil, errors.ErrJQValidation(err) | ||
| } | ||
| code, err := gojq.Compile(query, gojq.WithEnvironLoader(os.Environ)) | ||
| if err != nil { | ||
| return nil, errors.ErrJQValidation(err) | ||
| } | ||
| return code, nil | ||
| } | ||
|
|
||
| // Write intercepts JSON output, applies the jq filter, and writes filtered results. | ||
| // String results print as plain text; everything else prints as compact single-line JSON. | ||
| // Error envelopes (ok: false) pass through unfiltered so error messages are never hidden. | ||
| func (w *jqWriter) Write(p []byte) (int, error) { | ||
| var input any | ||
| if err := json.Unmarshal(p, &input); err != nil { | ||
| // Not JSON — pass through unchanged. | ||
| return w.dest.Write(p) | ||
| } | ||
robzolkos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Pass through error envelopes unfiltered so jq doesn't hide error messages. | ||
| if m, ok := input.(map[string]any); ok { | ||
| if okVal, exists := m["ok"]; exists { | ||
| if okBool, isBool := okVal.(bool); isBool && !okBool { | ||
| return w.dest.Write(p) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| iter := w.code.Run(input) | ||
| for { | ||
| v, ok := iter.Next() | ||
| if !ok { | ||
| break | ||
| } | ||
| if err, isErr := v.(error); isErr { | ||
| return 0, errors.ErrJQRuntime(err) | ||
| } | ||
| if s, isStr := v.(string); isStr { | ||
| if _, err := fmt.Fprintln(w.dest, s); err != nil { | ||
| return 0, err | ||
| } | ||
| } else { | ||
| raw, err := json.Marshal(v) | ||
| if err != nil { | ||
| return 0, errors.ErrJQRuntime(fmt.Errorf("result not serializable: %w", err)) | ||
| } | ||
| if _, err := fmt.Fprintln(w.dest, string(raw)); err != nil { | ||
| return 0, err | ||
| } | ||
| } | ||
| } | ||
| return len(p), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.