Add "json" as output type (#513)

Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/513
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: strk <strk@noreply.gitea.io>
This commit is contained in:
6543 2022-09-27 23:39:47 +08:00
parent 15457f1770
commit a37377d181
3 changed files with 125 additions and 27 deletions

View File

@ -33,7 +33,7 @@ var RemoteFlag = cli.StringFlag{
var OutputFlag = cli.StringFlag{ var OutputFlag = cli.StringFlag{
Name: "output", Name: "output",
Aliases: []string{"o"}, Aliases: []string{"o"},
Usage: "Output format. (csv, simple, table, tsv, yaml)", Usage: "Output format. (simple, table, csv, tsv, yaml, json)",
} }
// PaginationPageFlag provides flag for pagination options // PaginationPageFlag provides flag for pagination options

View File

@ -6,7 +6,9 @@ package print
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -72,25 +74,39 @@ func (t table) Less(i, j int) bool {
} }
func (t *table) print(output string) { func (t *table) print(output string) {
t.fprint(os.Stdout, output)
}
func (t *table) fprint(f io.Writer, output string) {
switch output { switch output {
case "", "table": case "", "table":
outputtable(t.headers, t.values) outputTable(f, t.headers, t.values)
case "csv": case "csv":
outputdsv(t.headers, t.values, ",") outputDsv(f, t.headers, t.values, ",")
case "simple": case "simple":
outputsimple(t.headers, t.values) outputSimple(f, t.headers, t.values)
case "tsv": case "tsv":
outputdsv(t.headers, t.values, "\t") outputDsv(f, t.headers, t.values, "\t")
case "yml", "yaml": case "yml", "yaml":
outputyaml(t.headers, t.values) outputYaml(f, t.headers, t.values)
case "json":
outputJSON(f, t.headers, t.values)
default: default:
fmt.Printf("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") 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)
} }
} }
// outputtable prints structured data as table // outputTable prints structured data as table
func outputtable(headers []string, values [][]string) { func outputTable(f io.Writer, headers []string, values [][]string) {
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(f)
if len(headers) > 0 { if len(headers) > 0 {
table.SetHeader(headers) table.SetHeader(headers)
} }
@ -100,47 +116,89 @@ func outputtable(headers []string, values [][]string) {
table.Render() table.Render()
} }
// outputsimple prints structured data as space delimited value // outputSimple prints structured data as space delimited value
func outputsimple(headers []string, values [][]string) { func outputSimple(f io.Writer, headers []string, values [][]string) {
for _, value := range values { for _, value := range values {
fmt.Printf(strings.Join(value, " ")) fmt.Fprint(f, strings.Join(value, " "))
fmt.Printf("\n") fmt.Fprintf(f, "\n")
} }
} }
// outputdsv prints structured data as delimiter separated value format // outputDsv prints structured data as delimiter separated value format
func outputdsv(headers []string, values [][]string, delimiterOpt ...string) { func outputDsv(f io.Writer, headers []string, values [][]string, delimiterOpt ...string) {
delimiter := "," delimiter := ","
if len(delimiterOpt) > 0 { if len(delimiterOpt) > 0 {
delimiter = delimiterOpt[0] delimiter = delimiterOpt[0]
} }
fmt.Println("\"" + strings.Join(headers, "\""+delimiter+"\"") + "\"") fmt.Fprintln(f, "\""+strings.Join(headers, "\""+delimiter+"\"")+"\"")
for _, value := range values { for _, value := range values {
fmt.Printf("\"") fmt.Fprintf(f, "\"")
fmt.Printf(strings.Join(value, "\""+delimiter+"\"")) fmt.Fprint(f, strings.Join(value, "\""+delimiter+"\""))
fmt.Printf("\"") fmt.Fprintf(f, "\"")
fmt.Printf("\n") fmt.Fprintf(f, "\n")
} }
} }
// outputyaml prints structured data as yaml // outputYaml prints structured data as yaml
func outputyaml(headers []string, values [][]string) { func outputYaml(f io.Writer, headers []string, values [][]string) {
for _, value := range values { for _, value := range values {
fmt.Println("-") fmt.Fprintln(f, "-")
for j, val := range value { for j, val := range value {
intVal, _ := strconv.Atoi(val) intVal, _ := strconv.Atoi(val)
if strconv.Itoa(intVal) == val { if strconv.Itoa(intVal) == val {
fmt.Printf(" %s: %s\n", headers[j], val) fmt.Fprintf(f, " %s: %s\n", headers[j], val)
} else { } else {
fmt.Printf(" %s: '%s'\n", headers[j], val) fmt.Fprintf(f, " %s: '%s'\n", headers[j], val)
} }
} }
} }
} }
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, "]")
}
func isMachineReadable(outputFormat string) bool { func isMachineReadable(outputFormat string) bool {
switch outputFormat { switch outputFormat {
case "yml", "yaml", "csv", "tsv": case "yml", "yaml", "csv", "tsv", "json":
return true return true
} }
return false return false

View File

@ -0,0 +1,40 @@
// Copyright 2022 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.
package print
import (
"bytes"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToSnakeCase(t *testing.T) {
assert.EqualValues(t, "some_test_var_at2d", toSnakeCase("SomeTestVarAt2d"))
}
func TestPrint(t *testing.T) {
tData := &table{
headers: []string{"A", "B"},
values: [][]string{
{"new a", "some bbbb"},
{"AAAAA", "b2"},
},
}
buf := &bytes.Buffer{}
tData.fprint(buf, "json")
result := []struct {
A string
B string
}{}
assert.NoError(t, json.NewDecoder(buf).Decode(&result))
if assert.Len(t, result, 2) {
assert.EqualValues(t, "new a", result[0].A)
}
}