2018-09-03 02:43:00 -04:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
package print
|
2018-09-03 02:43:00 -04:00
|
|
|
|
2019-09-15 04:38:18 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2018-09-03 02:43:00 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
showLog bool
|
|
|
|
)
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
// errorf printf content as an error information
|
|
|
|
func errorf(format string, a ...interface{}) {
|
2018-09-03 02:43:00 -04:00
|
|
|
fmt.Printf(format, a...)
|
|
|
|
}
|
2019-09-15 04:38:18 -04:00
|
|
|
|
|
|
|
// outputtable prints structured data as table
|
|
|
|
func outputtable(headers []string, values [][]string) {
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
if len(headers) > 0 {
|
|
|
|
table.SetHeader(headers)
|
|
|
|
}
|
|
|
|
for _, value := range values {
|
|
|
|
table.Append(value)
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputsimple prints structured data as space delimited value
|
|
|
|
func outputsimple(headers []string, values [][]string) {
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Printf(strings.Join(value, " "))
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputdsv prints structured data as delimiter separated value format
|
|
|
|
func outputdsv(headers []string, values [][]string, delimiterOpt ...string) {
|
|
|
|
delimiter := ","
|
|
|
|
if len(delimiterOpt) > 0 {
|
|
|
|
delimiter = delimiterOpt[0]
|
|
|
|
}
|
|
|
|
fmt.Println("\"" + strings.Join(headers, "\""+delimiter+"\"") + "\"")
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Printf("\"")
|
|
|
|
fmt.Printf(strings.Join(value, "\""+delimiter+"\""))
|
|
|
|
fmt.Printf("\"")
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputyaml prints structured data as yaml
|
|
|
|
func outputyaml(headers []string, values [][]string) {
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Println("-")
|
|
|
|
for j, val := range value {
|
|
|
|
intVal, _ := strconv.Atoi(val)
|
|
|
|
if strconv.Itoa(intVal) == val {
|
|
|
|
fmt.Printf(" %s: %s\n", headers[j], val)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" %s: '%s'\n", headers[j], val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:28:54 -05:00
|
|
|
// outputList provides general function to convert given list of items
|
2020-09-30 01:11:33 -04:00
|
|
|
// into several outputs (table, csv, simple, tsv, yaml)
|
2020-12-08 05:28:54 -05:00
|
|
|
func outputList(output string, headers []string, values [][]string) {
|
2019-09-15 04:38:18 -04:00
|
|
|
switch {
|
|
|
|
case output == "" || output == "table":
|
|
|
|
outputtable(headers, values)
|
|
|
|
case output == "csv":
|
|
|
|
outputdsv(headers, values, ",")
|
|
|
|
case output == "simple":
|
|
|
|
outputsimple(headers, values)
|
|
|
|
case output == "tsv":
|
|
|
|
outputdsv(headers, values, "\t")
|
|
|
|
case output == "yaml":
|
|
|
|
outputyaml(headers, values)
|
|
|
|
default:
|
2020-09-30 01:11:33 -04:00
|
|
|
errorf("unknown output type '" + output + "', available types are:\n- csv: comma-separated values\n- simple: space-separated values\n- table: auto-aligned table format (default)\n- tsv: tab-separated values\n- yaml: YAML format\n")
|
2019-09-15 04:38:18 -04:00
|
|
|
}
|
|
|
|
}
|