-
Notifications
You must be signed in to change notification settings - Fork 13
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
Open
robzolkos
wants to merge
11
commits into
master
Choose a base branch
from
rz/built-in-jq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db78e5a
Add built-in --jq flag via gojq
robzolkos 3b3f1da
Address PR review feedback
robzolkos 79bb9cb
Pass error envelopes through jqWriter unfiltered
robzolkos 3599a63
Add jq error types, early validation, env access, and compact output
robzolkos e80fc16
Reject --jq for version command, output plain text
robzolkos 224096d
Address PR review feedback
robzolkos 9c8346b
Address PR review feedback
robzolkos 4b02ec3
Compile jq expression once, reuse in writer
robzolkos f3d5649
Deduplicate jq compilation logic
robzolkos d31c29b
Fix jq error bypass to use resolved format and outWriter
robzolkos 08a2c07
Fall back to JSON format when resolveFormat fails in jq error bypass
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
| 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.