Skip to content
This repository was archived by the owner on May 24, 2019. It is now read-only.

Commit 0cd7ba0

Browse files
implemented unified Output (#14)
Signed-off-by: Andreas Ulm <[email protected]>
1 parent 9128037 commit 0cd7ba0

24 files changed

+2863
-12
lines changed

cmd/issues.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ var CmdIssues = cli.Command{
3535
Name: "repo, r",
3636
Usage: "Indicate one repository, optional when inside a gitea repository",
3737
},
38+
cli.StringFlag{
39+
Name: "output, o",
40+
Usage: "Specify output format. (table)",
41+
Destination: &output,
42+
},
3843
},
3944
}
4045

@@ -91,8 +96,17 @@ func runIssuesList(ctx *cli.Context) error {
9196
log.Fatal(err)
9297
}
9398

99+
headers := []string{
100+
string("Index"),
101+
string("Name"),
102+
string("Updated"),
103+
string("Title"),
104+
}
105+
106+
var values [][]string
107+
94108
if len(issues) == 0 {
95-
fmt.Println("No issues left")
109+
Output(output, headers, values)
96110
return nil
97111
}
98112

@@ -101,8 +115,17 @@ func runIssuesList(ctx *cli.Context) error {
101115
if len(name) == 0 {
102116
name = issue.Poster.UserName
103117
}
104-
fmt.Printf("#%d\t%s\t%s\t%s\n", issue.Index, name, issue.Updated.Format("2006-01-02 15:04:05"), issue.Title)
118+
values = append(
119+
values,
120+
[]string{
121+
strconv.FormatInt(issue.Index, 10),
122+
name,
123+
issue.Updated.Format("2006-01-02 15:04:05"),
124+
issue.Title,
125+
},
126+
)
105127
}
128+
Output(output, headers, values)
106129

107130
return nil
108131
}

cmd/log.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
package cmd
66

7-
import "fmt"
7+
import (
8+
"fmt"
9+
"os"
10+
"strconv"
11+
"strings"
12+
13+
"github.com/olekukonko/tablewriter"
14+
)
815

916
var (
1017
showLog bool
@@ -33,3 +40,71 @@ func Error(a ...interface{}) {
3340
func Errorf(format string, a ...interface{}) {
3441
fmt.Printf(format, a...)
3542
}
43+
44+
// OutputTable prints structured data as table
45+
func OutputTable(headers []string, values [][]string) {
46+
table := tablewriter.NewWriter(os.Stdout)
47+
if len(headers) > 0 {
48+
table.SetHeader(headers)
49+
}
50+
for _, value := range values {
51+
table.Append(value)
52+
}
53+
table.Render()
54+
}
55+
56+
// OutputSimple prints structured data as space delimited value
57+
func OutputSimple(headers []string, values [][]string) {
58+
for _, value := range values {
59+
fmt.Printf(strings.Join(value, " "))
60+
fmt.Printf("\n")
61+
}
62+
}
63+
64+
// OutputDsv prints structured data as delimiter separated value format
65+
func OutputDsv(headers []string, values [][]string, delimiterOpt ...string) {
66+
delimiter := ","
67+
if len(delimiterOpt) > 0 {
68+
delimiter = delimiterOpt[0]
69+
}
70+
for _, value := range values {
71+
fmt.Printf("\"")
72+
fmt.Printf(strings.Join(value, "\""+delimiter+"\""))
73+
fmt.Printf("\"")
74+
fmt.Printf("\n")
75+
}
76+
}
77+
78+
// OutputYaml prints structured data as yaml
79+
func OutputYaml(headers []string, values [][]string) {
80+
for _, value := range values {
81+
fmt.Println("-")
82+
for j, val := range value {
83+
intVal, _ := strconv.Atoi(val)
84+
if strconv.Itoa(intVal) == val {
85+
fmt.Printf(" %s: %s\n", headers[j], val)
86+
} else {
87+
fmt.Printf(" %s: '%s'\n", headers[j], val)
88+
}
89+
}
90+
}
91+
}
92+
93+
// Output provides general function to convert given information
94+
// into several outputs
95+
func Output(output string, headers []string, values [][]string) {
96+
switch {
97+
case output == "" || output == "table":
98+
OutputTable(headers, values)
99+
case output == "csv":
100+
OutputDsv(headers, values, ",")
101+
case output == "simple":
102+
OutputSimple(headers, values)
103+
case output == "tsv":
104+
OutputDsv(headers, values, "\t")
105+
case output == "yaml":
106+
OutputYaml(headers, values)
107+
default:
108+
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")
109+
}
110+
}

cmd/pulls.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
package cmd
66

77
import (
8-
"fmt"
98
"log"
9+
"strconv"
1010

1111
"code.gitea.io/sdk/gitea"
1212

1313
"github.com/urfave/cli"
1414
)
1515

16+
var output string
17+
1618
// CmdPulls represents to login a gitea server.
1719
var CmdPulls = cli.Command{
1820
Name: "pulls",
@@ -28,6 +30,11 @@ var CmdPulls = cli.Command{
2830
Name: "repo, r",
2931
Usage: "Indicate one repository, optional when inside a gitea repository",
3032
},
33+
cli.StringFlag{
34+
Name: "output, o",
35+
Usage: "Specify output format. (table)",
36+
Destination: &output,
37+
},
3138
},
3239
}
3340

@@ -43,8 +50,17 @@ func runPulls(ctx *cli.Context) error {
4350
log.Fatal(err)
4451
}
4552

53+
headers := []string{
54+
string("Index"),
55+
string("Name"),
56+
string("Updated"),
57+
string("Title"),
58+
}
59+
60+
var values [][]string
61+
4662
if len(prs) == 0 {
47-
fmt.Println("No pull requests left")
63+
Output(output, headers, values)
4864
return nil
4965
}
5066

@@ -56,8 +72,17 @@ func runPulls(ctx *cli.Context) error {
5672
if len(name) == 0 {
5773
name = pr.Poster.UserName
5874
}
59-
fmt.Printf("#%d\t%s\t%s\t%s\n", pr.Index, name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title)
75+
values = append(
76+
values,
77+
[]string{
78+
strconv.FormatInt(pr.Index, 10),
79+
name,
80+
pr.Updated.Format("2006-01-02 15:04:05"),
81+
pr.Title,
82+
},
83+
)
6084
}
85+
Output(output, headers, values)
6186

6287
return nil
6388
}

cmd/releases.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cmd
66

77
import (
8-
"fmt"
98
"log"
109
"os"
1110
"path/filepath"
@@ -33,6 +32,11 @@ var CmdReleases = cli.Command{
3332
Name: "repo, r",
3433
Usage: "Indicate one repository, optional when inside a gitea repository",
3534
},
35+
cli.StringFlag{
36+
Name: "output, o",
37+
Usage: "Specify output format. (table)",
38+
Destination: &output,
39+
},
3640
},
3741
}
3842

@@ -44,17 +48,32 @@ func runReleases(ctx *cli.Context) error {
4448
log.Fatal(err)
4549
}
4650

51+
headers := []string{
52+
string("Tag-Name"),
53+
string("Title"),
54+
string("Published At"),
55+
string("Tar URL"),
56+
}
57+
58+
var values [][]string
59+
4760
if len(releases) == 0 {
48-
fmt.Println("No Releases")
61+
Output(output, headers, values)
4962
return nil
5063
}
5164

5265
for _, release := range releases {
53-
fmt.Printf("#%s\t%s\t%s\t%s\n", release.TagName,
54-
release.Title,
55-
release.PublishedAt.Format("2006-01-02 15:04:05"),
56-
release.TarURL)
66+
values = append(
67+
values,
68+
[]string{
69+
release.TagName,
70+
release.Title,
71+
release.PublishedAt.Format("2006-01-02 15:04:05"),
72+
release.TarURL,
73+
},
74+
)
5775
}
76+
Output(output, headers, values)
5877

5978
return nil
6079
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.12
55
require (
66
code.gitea.io/sdk v0.0.0-20190424055801-13a7bf625b83
77
github.com/go-gitea/yaml v0.0.0-20170812160011-eb3733d160e7
8+
github.com/mattn/go-runewidth v0.0.4 // indirect
9+
github.com/olekukonko/tablewriter v0.0.1
810
github.com/urfave/cli v1.20.0
911
gopkg.in/src-d/go-git.v4 v4.11.0
1012
gopkg.in/yaml.v2 v2.2.2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
2626
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2727
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2828
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
29+
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
30+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
2931
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
3032
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
33+
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
34+
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
3135
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
3236
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
3337
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=

vendor/github.com/mattn/go-runewidth/.travis.yml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/mattn/go-runewidth/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/mattn/go-runewidth/README.mkd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)