Displaying nested tables using --template option in gh cli

55 views Asked by At

How do I go about displaying display tables from the gh cli output?

E.g. I want to display the labels for a certain set of PRs along with the PR information

Doing the below does not work

> gh pr list --state merged -B "my_branch" --json title,number,author,labels,commits --template \
'{{$prs := .}}{{range $pr:=$prs}}{{range .labels}}{{tablerow .name {{$pr.number}}}} {{end}}{{end}}'
template: :1: unexpected "{" in operand

I have tried various combinations but can't seem to get this working correctly. I got the below example working, but the output isn't how I expected

> gh pr list --state merged -B "my_branch" --json title,number,author,labels,commits --template \
'{{$prs := .}}{{range $pr:=$prs}}{{range .labels}} {{tablerow .name }} {{$pr.number}} {{end}}{{end}}'
  97830   95507   93914   93082   92499   91500   91051   90072   89822 LABEL1
LABEL1
LABEL1
LABEL1
LABEL1
LABEL1
LABEL1
LABEL1
LABEL1

I can't understand why the labels PR numbers are displayed all at once and not along side each label. In this case each PR has only label.

1

There are 1 answers

0
Mayank On

I think I found the problem, though I cannot understand the behavior in the 2nd example: The syntax would be:

> gh pr list --state merged -B "my_branch" --json title,number,author,labels,commits --template \
'{{$prs := .}}{{range $pr:=$prs}}{{range .labels}}{{tablerow .name $pr.number}} {{end}}{{end}}'

Basically, the {{ before $pr isn't needed since it is already part of the tablerow statement.