2020-12-09 23:04:36 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 03:40:02 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-09-30 07:11:33 +02:00
|
|
|
package print
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2019-09-15 10:38:18 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-27 17:39:47 +02:00
|
|
|
"io"
|
2019-09-15 10:38:18 +02:00
|
|
|
"os"
|
2022-09-27 17:39:47 +02:00
|
|
|
"regexp"
|
2020-12-09 23:04:36 +01:00
|
|
|
"sort"
|
2019-09-15 10:38:18 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-12-09 23:04:36 +01:00
|
|
|
// table provides infrastructure to easily print (sorted) lists in different formats
|
|
|
|
type table struct {
|
|
|
|
headers []string
|
|
|
|
values [][]string
|
|
|
|
sortDesc bool // used internally by sortable interface
|
|
|
|
sortColumn uint // ↑
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:41:07 +01:00
|
|
|
// printable can be implemented for structs to put fields dynamically into a table
|
|
|
|
type printable interface {
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 22:36:33 +02:00
|
|
|
FormatField(field string, machineReadable bool) string
|
2020-12-21 16:41:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// high level api to print a table of items with dynamic fields
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 22:36:33 +02:00
|
|
|
func tableFromItems(fields []string, values []printable, machineReadable bool) table {
|
2020-12-21 16:41:07 +01:00
|
|
|
t := table{headers: fields}
|
|
|
|
for _, v := range values {
|
|
|
|
row := make([]string, len(fields))
|
|
|
|
for i, f := range fields {
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 22:36:33 +02:00
|
|
|
row[i] = v.FormatField(f, machineReadable)
|
2020-12-21 16:41:07 +01:00
|
|
|
}
|
|
|
|
t.addRowSlice(row)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-12-09 23:04:36 +01:00
|
|
|
func tableWithHeader(header ...string) table {
|
|
|
|
return table{headers: header}
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's the callers responsibility to ensure row length is equal to header length!
|
|
|
|
func (t *table) addRow(row ...string) {
|
|
|
|
t.addRowSlice(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's the callers responsibility to ensure row length is equal to header length!
|
|
|
|
func (t *table) addRowSlice(row []string) {
|
|
|
|
t.values = append(t.values, row)
|
|
|
|
}
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-12-09 23:04:36 +01:00
|
|
|
func (t *table) sort(column uint, desc bool) {
|
|
|
|
t.sortColumn = column
|
|
|
|
t.sortDesc = desc
|
|
|
|
sort.Stable(t) // stable to allow multiple calls to sort
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortable interface
|
|
|
|
func (t table) Len() int { return len(t.values) }
|
|
|
|
func (t table) Swap(i, j int) { t.values[i], t.values[j] = t.values[j], t.values[i] }
|
|
|
|
func (t table) Less(i, j int) bool {
|
|
|
|
if t.sortDesc {
|
|
|
|
i, j = j, i
|
|
|
|
}
|
|
|
|
return t.values[i][t.sortColumn] < t.values[j][t.sortColumn]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) print(output string) {
|
2022-09-27 17:39:47 +02:00
|
|
|
t.fprint(os.Stdout, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) fprint(f io.Writer, output string) {
|
2020-12-21 16:41:07 +01:00
|
|
|
switch output {
|
|
|
|
case "", "table":
|
2022-09-27 17:39:47 +02:00
|
|
|
outputTable(f, t.headers, t.values)
|
2020-12-21 16:41:07 +01:00
|
|
|
case "csv":
|
2022-09-27 17:39:47 +02:00
|
|
|
outputDsv(f, t.headers, t.values, ",")
|
2020-12-21 16:41:07 +01:00
|
|
|
case "simple":
|
2022-09-27 17:39:47 +02:00
|
|
|
outputSimple(f, t.headers, t.values)
|
2020-12-21 16:41:07 +01:00
|
|
|
case "tsv":
|
2022-09-27 17:39:47 +02:00
|
|
|
outputDsv(f, t.headers, t.values, "\t")
|
2020-12-21 16:41:07 +01:00
|
|
|
case "yml", "yaml":
|
2022-09-27 17:39:47 +02:00
|
|
|
outputYaml(f, t.headers, t.values)
|
|
|
|
case "json":
|
|
|
|
outputJSON(f, t.headers, t.values)
|
2020-12-09 23:04:36 +01:00
|
|
|
default:
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintf(f, `"unknown output type '%s', available types are:
|
|
|
|
- csv: comma-separated values
|
|
|
|
- simple: space-separated values
|
|
|
|
- table: auto-aligned table format (default)
|
|
|
|
- tsv: tab-separated values
|
|
|
|
- yaml: YAML format
|
|
|
|
- json: JSON format
|
|
|
|
`, output)
|
|
|
|
os.Exit(1)
|
2020-12-09 23:04:36 +01:00
|
|
|
}
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
2019-09-15 10:38:18 +02:00
|
|
|
|
2022-09-27 17:39:47 +02:00
|
|
|
// outputTable prints structured data as table
|
|
|
|
func outputTable(f io.Writer, headers []string, values [][]string) {
|
|
|
|
table := tablewriter.NewWriter(f)
|
2019-09-15 10:38:18 +02:00
|
|
|
if len(headers) > 0 {
|
|
|
|
table.SetHeader(headers)
|
|
|
|
}
|
|
|
|
for _, value := range values {
|
|
|
|
table.Append(value)
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:39:47 +02:00
|
|
|
// outputSimple prints structured data as space delimited value
|
|
|
|
func outputSimple(f io.Writer, headers []string, values [][]string) {
|
2019-09-15 10:38:18 +02:00
|
|
|
for _, value := range values {
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprint(f, strings.Join(value, " "))
|
|
|
|
fmt.Fprintf(f, "\n")
|
2019-09-15 10:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:39:47 +02:00
|
|
|
// outputDsv prints structured data as delimiter separated value format
|
|
|
|
func outputDsv(f io.Writer, headers []string, values [][]string, delimiterOpt ...string) {
|
2019-09-15 10:38:18 +02:00
|
|
|
delimiter := ","
|
|
|
|
if len(delimiterOpt) > 0 {
|
|
|
|
delimiter = delimiterOpt[0]
|
|
|
|
}
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintln(f, "\""+strings.Join(headers, "\""+delimiter+"\"")+"\"")
|
2019-09-15 10:38:18 +02:00
|
|
|
for _, value := range values {
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintf(f, "\"")
|
|
|
|
fmt.Fprint(f, strings.Join(value, "\""+delimiter+"\""))
|
|
|
|
fmt.Fprintf(f, "\"")
|
|
|
|
fmt.Fprintf(f, "\n")
|
2019-09-15 10:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:39:47 +02:00
|
|
|
// outputYaml prints structured data as yaml
|
|
|
|
func outputYaml(f io.Writer, headers []string, values [][]string) {
|
2019-09-15 10:38:18 +02:00
|
|
|
for _, value := range values {
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintln(f, "-")
|
2019-09-15 10:38:18 +02:00
|
|
|
for j, val := range value {
|
|
|
|
intVal, _ := strconv.Atoi(val)
|
|
|
|
if strconv.Itoa(intVal) == val {
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintf(f, " %s: %s\n", headers[j], val)
|
2019-09-15 10:38:18 +02:00
|
|
|
} else {
|
2022-09-27 17:39:47 +02:00
|
|
|
fmt.Fprintf(f, " %s: '%s'\n", headers[j], val)
|
2019-09-15 10:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 16:41:07 +01:00
|
|
|
|
2022-09-27 17:39:47 +02:00
|
|
|
var (
|
|
|
|
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
|
|
|
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
|
|
|
)
|
|
|
|
|
|
|
|
func toSnakeCase(str string) string {
|
|
|
|
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
|
|
|
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
|
|
|
return strings.ToLower(snake)
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputJSON prints structured data as json
|
|
|
|
func outputJSON(f io.Writer, headers []string, values [][]string) {
|
|
|
|
fmt.Fprintln(f, "[")
|
|
|
|
itemCount := len(values)
|
|
|
|
headersCount := len(headers)
|
|
|
|
const space = " "
|
|
|
|
for i, value := range values {
|
|
|
|
fmt.Fprintf(f, "%s{\n", space)
|
|
|
|
for j, val := range value {
|
|
|
|
intVal, _ := strconv.Atoi(val)
|
|
|
|
if strconv.Itoa(intVal) == val {
|
|
|
|
fmt.Fprintf(f, "%s%s\"%s\": %s", space, space, toSnakeCase(headers[j]), val)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(f, "%s%s\"%s\": \"%s\"", space, space, toSnakeCase(headers[j]), val)
|
|
|
|
}
|
|
|
|
if j != headersCount-1 {
|
|
|
|
fmt.Fprintln(f, ",")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != itemCount-1 {
|
|
|
|
fmt.Fprintf(f, "%s},\n", space)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(f, "%s}\n", space)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintln(f, "]")
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:41:07 +01:00
|
|
|
func isMachineReadable(outputFormat string) bool {
|
|
|
|
switch outputFormat {
|
2022-09-27 17:39:47 +02:00
|
|
|
case "yml", "yaml", "csv", "tsv", "json":
|
2020-12-21 16:41:07 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|