A gh Alias For json Data
The GitHub command-line tool gh
has a number of commands that can return json data. For example, this command returns the two fields of data for each repo:
gh repo list --json field1,field2
But there is no way to fetch all fields without listing them explicitly.
For commands that take the --json
option, using it without any fields, or with an incorrect field, throws an error with a message containing the list of valid fields. We can use that to construct the list of all fields in an alias in the config.yml file for gh
:
json: |
!gh --json "$(gh "$0" "$@" --json 2>&1 | sed -E '/^ +/!d;s/^ +//;H;$!d;g;s/^\n//;s/\n/,/g')" "$0" "$@"
With this alias in place we can use gh json repo list
to fetch all fields for each repo. While it is not thoroughly tested, I believe this alias works for any command taking the --json
option.