Skip to content
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

Merged
merged 8 commits into from
Mar 4, 2021
Merged

Conversation

mislav
Copy link
Contributor

@mislav mislav commented Feb 22, 2021

The --filter flag takes a filter expression in jq syntax to select specific fields from the JSON data received from the server.

Example:

$ gh api -X GET search/repositories -f q='cli language:go' -f per_page=10 --filter '.items[].full_name'
cli/cli
urfave/cli
docker/cli
cloudfoundry/cli
exercism/cli
smallstep/cli
mitchellh/cli
tektoncd/cli
rancher/cli
prasmussen/gdrive

This is equivalent to doing gh api ... | jq -r '.items[].full_name', except that it's completely contained within gh and doesn't require the jq tool to be separately installed and available in the user's environment.

TODO:

  • output non-scalars as JSON to allow users to incrementally build up a query
  • tests

@mislav mislav requested a review from a team February 22, 2021 17:00
@mislav mislav added this to Backlog 🗒 in The GitHub CLI via automation Feb 22, 2021
@mislav mislav moved this from Backlog 🗒 to Needs review 🤔 in The GitHub CLI Feb 22, 2021
Copy link
Contributor

@vilmibm vilmibm left a 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

@mislav mislav requested a review from a team March 3, 2021 18:25
@mislav
Copy link
Contributor Author

mislav commented Mar 3, 2021

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 --filter is a mouthful to type every time. Should this flag have a shorthand, and if so, what could it be? I'm thinking of -j to further drive the point that the argument should be given in jq syntax.

Copy link
Contributor

@vilmibm vilmibm left a 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.

@@ -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")
Copy link
Contributor

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.

Copy link
Contributor

@samcoe samcoe left a 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.

@mislav mislav requested a review from vilmibm March 4, 2021 16:35
@mislav
Copy link
Contributor Author

mislav commented Mar 4, 2021

Renamed the flag -q, --jq to serve as a better mnemonic for what it is!

The GitHub CLI automation moved this from Needs review 🤔 to Needs to be merged 🎉 Mar 4, 2021
Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

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

thanks~

Base automatically changed from api-template to trunk March 4, 2021 16:46
@mislav mislav merged commit dbf1145 into trunk Mar 4, 2021
The GitHub CLI automation moved this from Needs to be merged 🎉 to Pending Release 🥚 Mar 4, 2021
@mislav mislav deleted the api-jq branch March 4, 2021 16:47
@github-actions github-actions bot moved this from Pending Release 🥚 to Done 💤 in The GitHub CLI Mar 4, 2021
@billygriffin billygriffin changed the title Add --filter to api command to filter data using jq syntax Add --jq to api command to filter data using jq syntax Apr 9, 2021
Copy link

@soffieswan043 soffieswan043 left a comment

Choose a reason for hiding this comment

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

(#3012)

@soffieswan043

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
The GitHub CLI
  
Done 💤
Development

Successfully merging this pull request may close these issues.

None yet

4 participants