merge output

Signed-off-by: Andreas Ulm <andreas.ulm@root360.de>
This commit is contained in:
Andreas Ulm 2019-05-08 21:01:07 +02:00
parent 912803706a
commit 03b5266aab
6 changed files with 161 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: "Specify output format. (table)",
Destination: &output,
},
},
}
@ -91,8 +96,17 @@ func runIssuesList(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
string("Index"),
string("Name"),
string("Updated"),
string("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,7 +4,14 @@
package cmd
import "fmt"
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
var (
showLog bool
@ -33,3 +40,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: "Specify output format. (table)",
Destination: &output,
},
},
}
@ -43,8 +50,17 @@ func runPulls(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
string("Index"),
string("Name"),
string("Updated"),
string("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: "Specify output format. (table)",
Destination: &output,
},
},
}
@ -44,17 +48,32 @@ func runReleases(ctx *cli.Context) error {
log.Fatal(err)
}
headers := []string{
string("Tag-Name"),
string("Title"),
string("Published At"),
string("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
}

2
go.mod
View File

@ -5,6 +5,8 @@ go 1.12
require (
code.gitea.io/sdk v0.0.0-20190424055801-13a7bf625b83
github.com/go-gitea/yaml v0.0.0-20170812160011-eb3733d160e7
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/urfave/cli v1.20.0
gopkg.in/src-d/go-git.v4 v4.11.0
gopkg.in/yaml.v2 v2.2.2 // indirect

4
go.sum
View File

@ -26,8 +26,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=