Skip to content

Add a template --format flag to api command#3011

Merged
mislav merged 8 commits intotrunkfrom
api-template
Mar 4, 2021
Merged

Add a template --format flag to api command#3011
mislav merged 8 commits intotrunkfrom
api-template

Conversation

@mislav
Copy link
Contributor

@mislav mislav commented Feb 22, 2021

The --format flag accepts a Go template string that the resulting data will be formatted with instead of being output as JSON.

Example:

gh api -X GET search/repositories -f q='cli language:go' -f per_page=10 \
  --format '{{range .items}}{{.full_name | color "blue+h"}} ({{.stargazers_count | color "yellow"}}){{if .description}} - {{.description}}{{end}}{{"\n"}}{{end}}'

Output:

Discuss:

  • Is it alright to expose Go templates to the user here?
  • Is the -t, --format flag naming okay? Should it be -t, --template instead?
  • For passing in more complex templates, should we additionally accept a template read from a file?
  • Are there any other formatting functions like color we should add for the initial ship?

TODO:

  • document Go template syntax
  • document template functions like color
  • add a color helper variant that only outputs ANSI color sequences when outputting to a tty

With the `--format` flag, the value of the flag is parsed as a Go
template which is then evaluated against parsed response data.
https://golang.org/pkg/text/template
@mislav mislav requested a review from a team February 22, 2021 16:54
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.

This is really cool!

Is it alright to expose Go templates to the user here?

I'm torn on this but I think it's for the best. To try and present something simpler means we'd need to maintain our own little templating language; I prefer the idea of adopting something else. It is a fair bit of cognitive overhead though and pretty verbose as far as template languages go. I'm curious what @ampinsk thinks.

Is the -t, --format flag naming okay? Should it be -t, --template instead?

If -f is already taken I'd vote for -t and --template for consistency.

For passing in more complex templates, should we additionally accept a template read from a file?

This is a reasonable improvement but I don't think needs to be in the first ship if you want to put it off.

Are there any other formatting functions like color we should add for the initial ship?

Color is a great place to start; it will be easier to guess what other functions we might want once it's in use. I suspect something around managing alignment (beyond raw tab characters) could be useful but I'm not exactly sure what that would look like yet.

@vilmibm
Copy link
Contributor

vilmibm commented Mar 1, 2021

@mislav should I be reviewing this PR in full yet?

Base automatically changed from api-cache to trunk March 2, 2021 11:47
@mislav
Copy link
Contributor Author

mislav commented Mar 2, 2021

@vilmibm Later today please! I'm working on adding documentation to this first.

@mislav mislav requested a review from a team March 2, 2021 19:08
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 love this feature and its implementation as well as the set of initial tempalate functions you went with.

Question: you mention mgutz/ansi; does this mean no color support on Windows?

as far as changes, I'd like to see a few more tests:

  • golden path for using -t with gh api
  • tests that exercise the default template functions; I see pluck and join but none of the other functions present in tests. Even if we're not actually checking color codes it's good to verify that we're exposing those functions correctly the the template engine.

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.

This looks good to me. I agree with @vilmibm about adding a couple more tests though.

@mislav
Copy link
Contributor Author

mislav commented Mar 4, 2021

Question: you mention mgutz/ansi; does this mean no color support on Windows?

All ANSI escape sequences are automatically converted to correct color codes on Windows by nature of our IOStreams object.

as far as changes, I'd like to see a few more tests:

Added more tests!

@mislav mislav requested a review from vilmibm March 4, 2021 15:39
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.

thank you!

@mislav mislav merged commit 0aebfac into trunk Mar 4, 2021
@mislav mislav deleted the api-template branch March 4, 2021 16:46
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.

3 participants