Skip to content

Commit

Permalink
implemented unified Output (go-gitea#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Ulm <[email protected]>
  • Loading branch information
root360-AndreasUlm committed Apr 28, 2019
1 parent 9128037 commit 0cd7ba0
Show file tree
Hide file tree
Showing 24 changed files with 2,863 additions and 12 deletions.
27 changes: 25 additions & 2 deletions cmd/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
77 changes: 76 additions & 1 deletion cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

package cmd

import "fmt"
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/olekukonko/tablewriter"
)

var (
showLog bool
Expand Down Expand Up @@ -33,3 +40,71 @@ 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]
}
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")
}
}
31 changes: 28 additions & 3 deletions cmd/pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
},
}

Expand All @@ -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
}

Expand All @@ -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
}
31 changes: 25 additions & 6 deletions cmd/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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,
},
},
}

Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/mattn/go-runewidth/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/mattn/go-runewidth/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/mattn/go-runewidth/README.mkd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0cd7ba0

Please sign in to comment.