Merge pull request #302 from go-jira/simplify-template-tables

add template functions to handle table output, fixes #176, replaces #296
This commit is contained in:
Mike Pountney
2019-12-02 18:19:10 -08:00
committed by GitHub
7 changed files with 123 additions and 71 deletions
+4 -4
View File
@@ -66,11 +66,11 @@ EOF
RUNS $jira ls --project BASIC --template table RUNS $jira ls --project BASIC --template table
DIFF <<EOF DIFF <<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| $(printf %-14s $issue) | summary | Bug | Medium | To Do | a minute | gojira | gojira | | $issue | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
EOF EOF
############################################################################### ###############################################################################
+4 -4
View File
@@ -72,9 +72,9 @@ EOF
RUNS $jira mine RUNS $jira mine
DIFF <<EOF DIFF <<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| $(printf %-14s $issue) | summary | Bug | Medium | To Do | a minute | gojira | gojira | | $issue | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
EOF EOF
+15 -15
View File
@@ -46,10 +46,10 @@ EOF
RUNS $jira epic list $epic RUNS $jira epic list $epic
DIFF<<EOF DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
EOF EOF
############################################################################### ###############################################################################
@@ -69,12 +69,12 @@ EOF
RUNS $jira epic list $epic RUNS $jira epic list $epic
DIFF<<EOF DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| $(printf %-14s $issue1) | summary | Bug | Medium | To Do | a minute | gojira | gojira | | $issue1 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
| $(printf %-14s $issue2) | summary | Bug | Medium | To Do | a minute | gojira | gojira | | $issue2 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
EOF EOF
############################################################################### ###############################################################################
@@ -92,11 +92,11 @@ EOF
RUNS $jira epic list $epic RUNS $jira epic list $epic
DIFF<<EOF DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
| $(printf %-14s $issue2) | summary | Bug | Medium | To Do | a minute | gojira | gojira | | $issue2 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +------------+---------+------+----------+--------+----------+----------+----------+
EOF EOF
############################################################################### ###############################################################################
@@ -114,8 +114,8 @@ EOF
RUNS $jira epic list $epic RUNS $jira epic list $epic
DIFF<<EOF DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee | | Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+ +-------+---------+------+----------+--------+-----+----------+----------+
EOF EOF
+18 -18
View File
@@ -59,13 +59,13 @@ EOF
############################################################################### ###############################################################################
RUNS $jira attach list $issue RUNS $jira attach list $issue
DIFF <<EOF DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
| id | filename | bytes | user | created | | id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
| $(printf %10s $attach1) | README.md | 1239 | gojira | a minute | | $attach1 | README.md | 1239 | gojira | a minute |
| $(printf %10s $attach2) | garbage.bin | 1048576 | gojira | a minute | | $attach2 | garbage.bin | 1048576 | gojira | a minute |
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute | | $attach3 | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
EOF EOF
############################################################################### ###############################################################################
@@ -146,12 +146,12 @@ EOF
RUNS $jira attach list $issue RUNS $jira attach list $issue
DIFF <<EOF DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
| id | filename | bytes | user | created | | id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
| $(printf %10s $attach2) | garbage.bin | 1048576 | gojira | a minute | | $attach2 | garbage.bin | 1048576 | gojira | a minute |
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute | | $attach3 | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+ +-------+-------------+---------+--------+----------+
EOF EOF
@@ -165,11 +165,11 @@ EOF
RUNS $jira attach list $issue RUNS $jira attach list $issue
DIFF <<EOF DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+ +-------+------------+---------+--------+----------+
| id | filename | bytes | user | created | | id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+ +-------+------------+---------+--------+----------+
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute | | $attach3 | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+ +-------+------------+---------+--------+----------+
EOF EOF
############################################################################### ###############################################################################
@@ -182,8 +182,8 @@ EOF
RUNS $jira attach list $issue RUNS $jira attach list $issue
DIFF <<EOF DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+ +----+----------+-------+------+---------+
| id | filename | bytes | user | created | | id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+ +----+----------+-------+------+---------+
+------------+------------------------------+------------+--------------+--------------+ +----+----------+-------+------+---------+
EOF EOF
+1
View File
@@ -27,6 +27,7 @@ require (
github.com/mattn/go-colorable v0.0.9 // indirect github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect github.com/mattn/go-isatty v0.0.3 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/olekukonko/tablewriter v0.0.3
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15 github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15
github.com/pkg/errors v0.8.0 github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
+4
View File
@@ -49,8 +49,12 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k=
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/olekukonko/tablewriter v0.0.3 h1:i0LBnzgiChAWHJYTQAZJDOgf8MNxAVYZJ2m63SIDimI=
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15 h1:mrI+6Ae64Wjt+uahGe5we/sPS1sXjvfT3YjtawAVgps= github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15 h1:mrI+6Ae64Wjt+uahGe5we/sPS1sXjvfT3YjtawAVgps=
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
+64 -17
View File
@@ -20,6 +20,7 @@ import (
"github.com/coryb/figtree" "github.com/coryb/figtree"
shellquote "github.com/kballard/go-shellquote" shellquote "github.com/kballard/go-shellquote"
"github.com/mgutz/ansi" "github.com/mgutz/ansi"
"github.com/olekukonko/tablewriter"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@@ -253,13 +254,40 @@ func RunTemplate(templateName string, data interface{}, out io.Writer) error {
return err return err
} }
tmpl, err := TemplateProcessor().Parse(templateContent) table := tablewriter.NewWriter(out)
table.SetAutoFormatHeaders(false)
headers := []string{}
cells := [][]string{}
tmpl, err := TemplateProcessor().Funcs(map[string]interface{}{
"headers": func(titles ...string) string {
headers = append(headers, titles...)
return ""
},
"row": func() string {
cells = append(cells, []string{})
return ""
},
"cell": func(value interface{}) (string, error) {
if len(cells) == 0 {
return "", fmt.Errorf(`"cell" template function called before "row" template function`)
}
cells[len(cells)-1] = append(cells[len(cells)-1], fmt.Sprintf("%v", value))
return "", nil
},
}).Parse(templateContent)
if err != nil { if err != nil {
return err return err
} }
if err := tmpl.Execute(out, rawData); err != nil { if err := tmpl.Execute(out, rawData); err != nil {
return err return err
} }
if len(headers) > 0 || len(cells) > 0 {
table.SetHeader(headers)
table.AppendBulk(cells)
table.Render()
}
return nil return nil
} }
@@ -296,23 +324,42 @@ const defaultDebugTemplate = "{{ . | toJson}}\n"
const defaultListTemplate = "{{ range .issues }}{{ .key | append \":\" | printf \"%-12s\"}} {{ .fields.summary }}\n{{ end }}" const defaultListTemplate = "{{ range .issues }}{{ .key | append \":\" | printf \"%-12s\"}} {{ .fields.summary }}\n{{ end }}"
const defaultTableTemplate = `{{/* table template */ -}} const defaultTableTemplate = `{{/* table template */ -}}
{{$w := sub termWidth 107 -}} {{- headers "Issue" "Summary" "Type" "Priority" "Status" "Age" "Reporter" "Assignee" -}}
+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ {{- range .issues -}}
| {{ "Issue" | printf "%-14s" }} | {{ "Summary" | printf (printf "%%-%ds" (sub $w 2)) }} | {{ "Type" | printf "%-12s"}} | {{ "Priority" | printf "%-12s" }} | {{ "Status" | printf "%-12s" }} | {{ "Age" | printf "%-10s" }} | {{ "Reporter" | printf "%-12s" }} | {{ "Assignee" | printf "%-12s" }} | {{- row -}}
+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ {{- cell .key -}}
{{ range .issues -}} {{- cell .fields.summary -}}
| {{ .key | printf "%-14s"}} | {{ .fields.summary | abbrev (sub $w 2) | printf (printf "%%-%ds" (sub $w 2)) }} | {{.fields.issuetype.name | printf "%-12s" }} | {{if .fields.priority}}{{.fields.priority.name | printf "%-12s" }}{{else}}<unassigned>{{end}} | {{.fields.status.name | printf "%-12s" }} | {{.fields.created | age | printf "%-10s" }} | {{if .fields.reporter}}{{ .fields.reporter.name | printf "%-12s"}}{{else}}<unassigned>{{end}} | {{if .fields.assignee }}{{.fields.assignee.name | printf "%-12s" }}{{else}}<unassigned>{{end}} | {{- cell .fields.issuetype.name -}}
{{ end -}} {{- if .fields.priority -}}
+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ {{- cell .fields.priority.name -}}
{{- else -}}
{{- cell "<none>" -}}
{{- end -}}
{{- cell .fields.status.name -}}
{{- cell (.fields.created | age) -}}
{{- if .fields.reporter -}}
{{- cell .fields.reporter.name -}}
{{- else -}}
{{- cell "<unknown>" -}}
{{- end -}}
{{- if .fields.assignee -}}
{{- cell .fields.assignee.name -}}
{{- else -}}
{{- cell "<unassigned>" -}}
{{- end -}}
{{- end -}}
` `
const defaultAttachListTemplate = `{{/* table template */ -}}
+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ const defaultAttachListTemplate = `{{/* attach list template */ -}}
| {{printf "%-10s" "id"}} | {{printf "%-28s" "filename"}} | {{printf "%-10s" "bytes"}} | {{printf "%-12s" "user"}} | {{printf "%-12s" "created"}} | {{- headers "id" "filename" "bytes" "user" "created" -}}
+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ {{- range . -}}
{{range . -}} {{- row -}}
| {{.id | printf "%10d" }} | {{.filename | printf "%-28s"}} | {{.size | printf "%10d"}} | {{.author.name | printf "%-12s"}} | {{.created | age | printf "%-12s"}} | {{- cell .id -}}
{{end -}} {{- cell .filename -}}
+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ {{- cell .size -}}
{{- cell .author.name -}}
{{- cell (.created | age) -}}
{{- end -}}
` `
const defaultViewTemplate = `{{/* view template */ -}} const defaultViewTemplate = `{{/* view template */ -}}