-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add --jq
to api command to filter data using jq syntax
#3012
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gojq
? this...is awesome. 👍 , will re-review once tests are in
Now with documentation and tests, this is ready for full review. Final question: I can see this flag being used a lot, so I'm wondering whether |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this 👍
some minor requests:
- a golden path test that ensures the flag can be passed to the command and that output is formatted as expected. I know it might sound silly, but a Flags invocation could be lost in a refactor or similar so I think such simple tests have merit.
- going ahead with the
-j
shortcut. I'm actually wondering if the longform should just be--jq
but I like that--filter
expresses intent more clearly. If-f
wasn't already taken I'd say just do that but-j
is a suitable backup.
pkg/cmd/api/api.go
Outdated
@@ -197,6 +205,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command | |||
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The `file` to use as body for the HTTP request") | |||
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body") | |||
cmd.Flags().StringVarP(&opts.Template, "template", "t", "", "Format the response using a Go template") | |||
cmd.Flags().StringVar(&opts.FilterOutput, "filter", "", "Filter fields from the response using jq syntax") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since filter and template are mutually exclusive I think we should probably raise an error message when both are provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Between adding this and the templating functionality I am starting to think we should make more detailed documentation on the website for the api
command. It is just such a powerful command with a ton of functionality that could use some thorough examples and structured explanation.
Renamed the flag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks~
--filter
to api command to filter data using jq syntax--jq
to api command to filter data using jq syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(#3012)
The
--filter
flag takes a filter expression in jq syntax to select specific fields from the JSON data received from the server.Example:
This is equivalent to doing
gh api ... | jq -r '.items[].full_name'
, except that it's completely contained within gh and doesn't require thejq
tool to be separately installed and available in the user's environment.TODO: