unified output (#14) (#40)

This commit is contained in:
root360-AndreasUlm
2019-09-15 08:38:18 +00:00
committed by Gitea
parent 1d233402fd
commit 7c024bcd69
24 changed files with 2866 additions and 12 deletions

View File

@ -35,6 +35,11 @@ var CmdIssues = cli.Command{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
}
@ -91,8 +96,17 @@ func runIssuesList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Index",
"Name",
"Updated",
"Title",
}
var values [][]string
if len(issues) == 0 {
fmt.Println("No issues left")
Output(output, headers, values)
return nil
}
@ -101,8 +115,17 @@ func runIssuesList(ctx *cli.Context) error {
if len(name) == 0 {
name = issue.Poster.UserName
}
fmt.Printf("#%d\t%s\t%s\t%s\n", issue.Index, name, issue.Updated.Format("2006-01-02 15:04:05"), issue.Title)
values = append(
values,
[]string{
strconv.FormatInt(issue.Index, 10),
name,
issue.Updated.Format("2006-01-02 15:04:05"),
issue.Title,
},
)
}
Output(output, headers, values)
return nil
}

View File

@ -4,12 +4,21 @@
package cmd
import "fmt"
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
var (
showLog bool
)
const outputUsage = "Specify output format - table (default), csv, simple, tsv or yaml."
// Println println content according the flag
func Println(a ...interface{}) {
if showLog {
@ -33,3 +42,72 @@ func Error(a ...interface{}) {
func Errorf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
// 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)
}
}
}
}
// Output provides general function to convert given information
// into several outputs
func Output(output string, headers []string, values [][]string) {
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:
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")
}
}

View File

@ -5,14 +5,16 @@
package cmd
import (
"fmt"
"log"
"strconv"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
)
var output string
// CmdPulls represents to login a gitea server.
var CmdPulls = cli.Command{
Name: "pulls",
@ -28,6 +30,11 @@ var CmdPulls = cli.Command{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
}
@ -43,8 +50,17 @@ func runPulls(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Index",
"Name",
"Updated",
"Title",
}
var values [][]string
if len(prs) == 0 {
fmt.Println("No pull requests left")
Output(output, headers, values)
return nil
}
@ -56,8 +72,17 @@ func runPulls(ctx *cli.Context) error {
if len(name) == 0 {
name = pr.Poster.UserName
}
fmt.Printf("#%d\t%s\t%s\t%s\n", pr.Index, name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title)
values = append(
values,
[]string{
strconv.FormatInt(pr.Index, 10),
name,
pr.Updated.Format("2006-01-02 15:04:05"),
pr.Title,
},
)
}
Output(output, headers, values)
return nil
}

View File

@ -5,7 +5,6 @@
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
@ -33,6 +32,11 @@ var CmdReleases = cli.Command{
Name: "repo, r",
Usage: "Indicate one repository, optional when inside a gitea repository",
},
cli.StringFlag{
Name: "output, o",
Usage: outputUsage,
Destination: &output,
},
},
}
@ -44,17 +48,32 @@ func runReleases(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
"Tag-Name",
"Title",
"Published At",
"Tar URL",
}
var values [][]string
if len(releases) == 0 {
fmt.Println("No Releases")
Output(output, headers, values)
return nil
}
for _, release := range releases {
fmt.Printf("#%s\t%s\t%s\t%s\n", release.TagName,
release.Title,
release.PublishedAt.Format("2006-01-02 15:04:05"),
release.TarURL)
values = append(
values,
[]string{
release.TagName,
release.Title,
release.PublishedAt.Format("2006-01-02 15:04:05"),
release.TarURL,
},
)
}
Output(output, headers, values)
return nil
}