Skip to content

Commit fd20326

Browse files
authored
Merge pull request #28 from huhouhua/update-dependencies-table-writer
Upgrade dependencies tablewriter to v1.0.8
2 parents 1299d4a + 22ea62c commit fd20326

File tree

8 files changed

+68
-62
lines changed

8 files changed

+68
-62
lines changed

cmd/resources/branch/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,5 @@ func (o *CreateOptions) Run(args []string) error {
117117
if err != nil {
118118
return err
119119
}
120-
cmdutil.PrintBranchOut(o.Out, o.ioStreams.Out, branch)
121-
return nil
120+
return cmdutil.PrintBranchOut(o.Out, o.ioStreams.Out, branch)
122121
}

cmd/resources/branch/get.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,5 @@ func (o *ListOptions) Run(args []string) error {
132132
}
133133
o.branch.Page++
134134
}
135-
cmdutil.PrintBranchOut(o.Out, o.ioStreams.Out, branches...)
136-
return nil
135+
return cmdutil.PrintBranchOut(o.Out, o.ioStreams.Out, branches...)
137136
}

cmd/resources/file/get.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,5 @@ func (o *ListOptions) Run(args []string) error {
168168
}
169169
o.file.ListOptions.Page++
170170
}
171-
cmdutil.PrintFilesOut(o.Out, o.ioStreams.Out, list...)
172-
return nil
171+
return cmdutil.PrintFilesOut(o.Out, o.ioStreams.Out, list...)
173172
}

cmd/resources/group/get.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ func (o *ListOptions) Run(args []string) error {
144144
if err != nil {
145145
return err
146146
}
147-
cmdutil.PrintGroupsOut(o.Out, o.ioStreams.Out, group)
148-
return nil
147+
return cmdutil.PrintGroupsOut(o.Out, o.ioStreams.Out, group)
149148
}
150149
var groups []*gitlab.Group
151150
var err error
@@ -168,6 +167,5 @@ func (o *ListOptions) Run(args []string) error {
168167
if err != nil {
169168
return nil
170169
}
171-
cmdutil.PrintGroupsOut(o.Out, o.ioStreams.Out, groups...)
172-
return nil
170+
return cmdutil.PrintGroupsOut(o.Out, o.ioStreams.Out, groups...)
173171
}

cmd/resources/project/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,5 @@ func (o *CreateOptions) Run(args []string) error {
234234
if err != nil {
235235
return err
236236
}
237-
cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
238-
return nil
237+
return cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
239238
}

cmd/resources/project/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,5 @@ func (o *EditOptions) Run(args []string) error {
157157
if err != nil {
158158
return err
159159
}
160-
cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
161-
return nil
160+
return cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
162161
}

cmd/resources/project/get.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ func (o *ListOptions) Run(args []string) error {
168168
if err != nil {
169169
return err
170170
}
171-
cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
172-
return nil
171+
return cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, project)
173172
}
174173
var projects []*gitlab.Project
175174
var err error
@@ -197,6 +196,5 @@ func (o *ListOptions) Run(args []string) error {
197196
if err != nil {
198197
return nil
199198
}
200-
cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, projects...)
201-
return nil
199+
return cmdutil.PrintProjectsOut(o.Out, o.ioStreams.Out, projects...)
202200
}

cmd/util/print.go

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"strconv"
2222
"strings"
2323

24+
"github.com/olekukonko/tablewriter/tw"
25+
2426
"github.com/olekukonko/tablewriter"
2527
gitlab "gitlab.com/gitlab-org/api/client-go"
2628
"gopkg.in/yaml.v3"
@@ -31,16 +33,16 @@ var (
3133
"Use the (-h) flag to see the command usage."
3234
)
3335

34-
func PrintProjectsOut(format string, w io.Writer, projects ...*gitlab.Project) {
36+
func PrintProjectsOut(format string, w io.Writer, projects ...*gitlab.Project) error {
3537
switch format {
3638
case JSON:
37-
printJSON(w, projects)
39+
return printJSON(w, projects)
3840
case YAML:
39-
printYAML(w, projects)
41+
return printYAML(w, projects)
4042
default:
4143
if len(projects) == 0 {
42-
fmt.Fprintln(w, noResultMsg)
43-
return
44+
_, err := fmt.Fprintln(w, noResultMsg)
45+
return err
4446
}
4547
header := []string{"ID", "PATH", "URL", "ISSUES COUNT", "TAGS"}
4648
var rows [][]string
@@ -53,20 +55,20 @@ func PrintProjectsOut(format string, w io.Writer, projects ...*gitlab.Project) {
5355
strings.Join(v.Topics, ","),
5456
})
5557
}
56-
printTable(header, w, rows)
58+
return printTable(header, w, rows)
5759
}
5860
}
5961

60-
func PrintGroupsOut(format string, w io.Writer, groups ...*gitlab.Group) {
62+
func PrintGroupsOut(format string, w io.Writer, groups ...*gitlab.Group) error {
6163
switch format {
6264
case JSON:
63-
printJSON(w, groups)
65+
return printJSON(w, groups)
6466
case YAML:
65-
printYAML(w, groups)
67+
return printYAML(w, groups)
6668
default:
6769
if len(groups) == 0 {
68-
fmt.Fprintln(w, noResultMsg)
69-
return
70+
_, err := fmt.Fprintln(w, noResultMsg)
71+
return err
7072
}
7173
header := []string{"ID", "PATH", "URL", "PARENT ID"}
7274
var rows [][]string
@@ -78,19 +80,19 @@ func PrintGroupsOut(format string, w io.Writer, groups ...*gitlab.Group) {
7880
strconv.Itoa(v.ParentID),
7981
})
8082
}
81-
printTable(header, w, rows)
83+
return printTable(header, w, rows)
8284
}
8385
}
84-
func PrintBranchOut(format string, w io.Writer, branches ...*gitlab.Branch) {
86+
func PrintBranchOut(format string, w io.Writer, branches ...*gitlab.Branch) error {
8587
switch format {
8688
case YAML:
87-
printYAML(w, branches)
89+
return printYAML(w, branches)
8890
case JSON:
89-
printJSON(w, branches)
91+
return printJSON(w, branches)
9092
default:
9193
if len(branches) == 0 {
92-
fmt.Fprintln(w, noResultMsg)
93-
return
94+
_, err := fmt.Fprintln(w, noResultMsg)
95+
return err
9496
}
9597
header := []string{"NAME", "PROTECTED", "DEVELOPERS CAN PUSH", "DEVELOPERS CAN MERGE"}
9698
var rows [][]string
@@ -102,20 +104,20 @@ func PrintBranchOut(format string, w io.Writer, branches ...*gitlab.Branch) {
102104
strconv.FormatBool(v.DevelopersCanMerge),
103105
})
104106
}
105-
printTable(header, w, rows)
107+
return printTable(header, w, rows)
106108
}
107109
}
108110

109-
func PrintFilesOut(format string, w io.Writer, trees ...*gitlab.TreeNode) {
111+
func PrintFilesOut(format string, w io.Writer, trees ...*gitlab.TreeNode) error {
110112
switch format {
111113
case JSON:
112-
printJSON(w, trees)
114+
return printJSON(w, trees)
113115
case YAML:
114-
printYAML(w, trees)
116+
return printYAML(w, trees)
115117
default:
116118
if len(trees) == 0 {
117-
fmt.Println(noResultMsg)
118-
return
119+
_, err := fmt.Fprintln(w, noResultMsg)
120+
return err
119121
}
120122
header := []string{"PATH", "TYPE"}
121123
var rows [][]string
@@ -125,43 +127,56 @@ func PrintFilesOut(format string, w io.Writer, trees ...*gitlab.TreeNode) {
125127
v.Type,
126128
})
127129
}
128-
printTable(header, w, rows)
130+
return printTable(header, w, rows)
129131
}
130132
}
131133

132-
func printJSON(w io.Writer, v interface{}) {
134+
func printJSON(w io.Writer, v interface{}) error {
133135
b, err := json.MarshalIndent(v, "", " ")
134136
if err != nil {
135137
Error(w, fmt.Sprintf("failed printing to json: %v", err))
136138
}
137-
fmt.Fprintln(w, string(b))
139+
_, err = fmt.Fprintln(w, string(b))
140+
return err
138141
}
139142

140-
func printYAML(w io.Writer, v interface{}) {
143+
func printYAML(w io.Writer, v interface{}) error {
141144
b, err := yaml.Marshal(v)
142145
if err != nil {
143146
Error(w, fmt.Sprintf("failed printing to yaml: %v", err))
144147
}
145-
fmt.Fprintln(w, string(b))
148+
_, err = fmt.Fprintln(w, string(b))
149+
return err
146150
}
147151

148-
func printTable(header []string, w io.Writer, rows [][]string) {
152+
func printTable(header []string, w io.Writer, rows [][]string) error {
149153
if len(header) > 5 {
150154
panic("maximum allowed length of a table header is only 5.")
151155
}
152-
table := tablewriter.NewWriter(w)
153-
table.SetHeader(header)
154-
table.SetAutoWrapText(false)
155-
table.SetAutoFormatHeaders(true)
156-
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
157-
table.SetAlignment(tablewriter.ALIGN_LEFT)
158-
table.SetCenterSeparator("")
159-
table.SetColumnSeparator("")
160-
table.SetRowSeparator("")
161-
table.SetHeaderLine(false)
162-
table.SetBorder(false)
163-
table.SetNoWhiteSpace(true)
164-
table.SetTablePadding("\t") // pad with tabs
165-
table.AppendBulk(rows)
166-
table.Render()
156+
table := tablewriter.NewTable(w,
157+
tablewriter.WithTrimSpace(tw.Off),
158+
tablewriter.WithAlignment(tw.Alignment{tw.AlignLeft, tw.AlignLeft, tw.AlignLeft}),
159+
tablewriter.WithRendition(tw.Rendition{
160+
Borders: tw.BorderNone,
161+
Settings: tw.Settings{
162+
Separators: tw.Separators{
163+
ShowHeader: tw.Off,
164+
ShowFooter: tw.Off,
165+
BetweenRows: tw.Off,
166+
BetweenColumns: tw.Off,
167+
},
168+
Lines: tw.Lines{
169+
ShowHeaderLine: tw.Off,
170+
},
171+
},
172+
}),
173+
)
174+
table.Header(header)
175+
if err := table.Bulk(rows); err != nil {
176+
return err
177+
}
178+
if err := table.Render(); err != nil {
179+
return err
180+
}
181+
return nil
167182
}

0 commit comments

Comments
 (0)