Skip to content

Support using both --jq and --template for gh repo list #8734

Closed as duplicate

Description

@MGaetan89

Describe the feature or problem you’d like to solve

I'd like to use the gh repo list command to list the repositories of an organisation, grouped by primary language, in Markdown, ie. something like:

## Language 1

- Repo 1
- Repo 2

## Language 2

- Repo 3
- Repo 4

To get the data I need, I can use the following command: gh repo list cli --json name,primaryLanguage, which will output something like:

[
  {
    "name": "cli",
    "primaryLanguage": {
      "name": "Go"
    }
  },
  {
    "name": "go-gh",
    "primaryLanguage": {
      "name": "Go"
    }
  }
]

Then I can use the --jq option to get the data structure I need: gh repo list cli --json name,primaryLanguage --jq '. | reduce .[] as { $primaryLanguage, $name } ({}; .[$primaryLanguage.name | tostring] += [$name])', which will output:

{
  "Go": [
    "cli",
    "go-gh",
    "safeexec",
    "browser",
    "gh-webhook",
    "shurcooL-graphql",
    "oauth"
  ],
  "Shell": [
    "gh-extension-precompile"
  ],
  "null": [
    "scoop-gh"
  ]
}

Now, to get the output I shown above (in Markdown), I can use --template, with something like: gh repo list cli --json name,primaryLanguage --jq '. | reduce .[] as { $primaryLanguage, $name } ({}; .[$primaryLanguage.name | tostring] += [$name])' --template '{{ range $language, $repositories := . }}## {{ .language }}{{ "\n\n" }}{{ range $repository := $repositories }}- {{ .name }}{{ "\n" }}{{ end }}{{ end }}'. But that will give me the same output as before (the JSON object).

The other way around, running gh repo list cli --json name,primaryLanguage --template '{{ range . }}- {{ .name }}{{ "\n" }}{{ end }}' will output:

- cli
- go-gh
- safeexec
- browser
- gh-extension-precompile
- gh-webhook
- shurcooL-graphql
- oauth
- scoop-gh

But adding --jq '.' will give me JSON:

[
  {
    "name": "cli",
    "primaryLanguage": {
      "name": "Go"
    }
  },
  {
    "name": "go-gh",
    "primaryLanguage": {
      "name": "Go"
    }
  }
]

Proposed solution

Supporting both the --jq and --template options together allows user to manipulate the data before formatting them.

Additional context

I'm using the latest version of GitHub's cli to date:

> gh --version
gh version 2.44.1 (2024-02-16)
https://github.com/cli/cli/releases/tag/v2.44.1

Activity

  1. ronnypolley commented on Feb 21, 2024

    @ronnypolley

    I would like to see the same in other subcommands too (like search).

  2. williammartin commented on Feb 26, 2024

    @williammartin
    Member

    Thanks for the detailed description of what you're wanting to do, it's very useful to have the context.

    This looks like a duplicate of #8415 and there are some workarounds offered there. If I've misunderstood feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions