@@ -21,6 +21,8 @@ import (
21
21
"strconv"
22
22
"strings"
23
23
24
+ "github.com/olekukonko/tablewriter/tw"
25
+
24
26
"github.com/olekukonko/tablewriter"
25
27
gitlab "gitlab.com/gitlab-org/api/client-go"
26
28
"gopkg.in/yaml.v3"
@@ -31,16 +33,16 @@ var (
31
33
"Use the (-h) flag to see the command usage."
32
34
)
33
35
34
- func PrintProjectsOut (format string , w io.Writer , projects ... * gitlab.Project ) {
36
+ func PrintProjectsOut (format string , w io.Writer , projects ... * gitlab.Project ) error {
35
37
switch format {
36
38
case JSON :
37
- printJSON (w , projects )
39
+ return printJSON (w , projects )
38
40
case YAML :
39
- printYAML (w , projects )
41
+ return printYAML (w , projects )
40
42
default :
41
43
if len (projects ) == 0 {
42
- fmt .Fprintln (w , noResultMsg )
43
- return
44
+ _ , err := fmt .Fprintln (w , noResultMsg )
45
+ return err
44
46
}
45
47
header := []string {"ID" , "PATH" , "URL" , "ISSUES COUNT" , "TAGS" }
46
48
var rows [][]string
@@ -53,20 +55,20 @@ func PrintProjectsOut(format string, w io.Writer, projects ...*gitlab.Project) {
53
55
strings .Join (v .Topics , "," ),
54
56
})
55
57
}
56
- printTable (header , w , rows )
58
+ return printTable (header , w , rows )
57
59
}
58
60
}
59
61
60
- func PrintGroupsOut (format string , w io.Writer , groups ... * gitlab.Group ) {
62
+ func PrintGroupsOut (format string , w io.Writer , groups ... * gitlab.Group ) error {
61
63
switch format {
62
64
case JSON :
63
- printJSON (w , groups )
65
+ return printJSON (w , groups )
64
66
case YAML :
65
- printYAML (w , groups )
67
+ return printYAML (w , groups )
66
68
default :
67
69
if len (groups ) == 0 {
68
- fmt .Fprintln (w , noResultMsg )
69
- return
70
+ _ , err := fmt .Fprintln (w , noResultMsg )
71
+ return err
70
72
}
71
73
header := []string {"ID" , "PATH" , "URL" , "PARENT ID" }
72
74
var rows [][]string
@@ -78,19 +80,19 @@ func PrintGroupsOut(format string, w io.Writer, groups ...*gitlab.Group) {
78
80
strconv .Itoa (v .ParentID ),
79
81
})
80
82
}
81
- printTable (header , w , rows )
83
+ return printTable (header , w , rows )
82
84
}
83
85
}
84
- func PrintBranchOut (format string , w io.Writer , branches ... * gitlab.Branch ) {
86
+ func PrintBranchOut (format string , w io.Writer , branches ... * gitlab.Branch ) error {
85
87
switch format {
86
88
case YAML :
87
- printYAML (w , branches )
89
+ return printYAML (w , branches )
88
90
case JSON :
89
- printJSON (w , branches )
91
+ return printJSON (w , branches )
90
92
default :
91
93
if len (branches ) == 0 {
92
- fmt .Fprintln (w , noResultMsg )
93
- return
94
+ _ , err := fmt .Fprintln (w , noResultMsg )
95
+ return err
94
96
}
95
97
header := []string {"NAME" , "PROTECTED" , "DEVELOPERS CAN PUSH" , "DEVELOPERS CAN MERGE" }
96
98
var rows [][]string
@@ -102,20 +104,20 @@ func PrintBranchOut(format string, w io.Writer, branches ...*gitlab.Branch) {
102
104
strconv .FormatBool (v .DevelopersCanMerge ),
103
105
})
104
106
}
105
- printTable (header , w , rows )
107
+ return printTable (header , w , rows )
106
108
}
107
109
}
108
110
109
- func PrintFilesOut (format string , w io.Writer , trees ... * gitlab.TreeNode ) {
111
+ func PrintFilesOut (format string , w io.Writer , trees ... * gitlab.TreeNode ) error {
110
112
switch format {
111
113
case JSON :
112
- printJSON (w , trees )
114
+ return printJSON (w , trees )
113
115
case YAML :
114
- printYAML (w , trees )
116
+ return printYAML (w , trees )
115
117
default :
116
118
if len (trees ) == 0 {
117
- fmt .Println ( noResultMsg )
118
- return
119
+ _ , err := fmt .Fprintln ( w , noResultMsg )
120
+ return err
119
121
}
120
122
header := []string {"PATH" , "TYPE" }
121
123
var rows [][]string
@@ -125,43 +127,56 @@ func PrintFilesOut(format string, w io.Writer, trees ...*gitlab.TreeNode) {
125
127
v .Type ,
126
128
})
127
129
}
128
- printTable (header , w , rows )
130
+ return printTable (header , w , rows )
129
131
}
130
132
}
131
133
132
- func printJSON (w io.Writer , v interface {}) {
134
+ func printJSON (w io.Writer , v interface {}) error {
133
135
b , err := json .MarshalIndent (v , "" , " " )
134
136
if err != nil {
135
137
Error (w , fmt .Sprintf ("failed printing to json: %v" , err ))
136
138
}
137
- fmt .Fprintln (w , string (b ))
139
+ _ , err = fmt .Fprintln (w , string (b ))
140
+ return err
138
141
}
139
142
140
- func printYAML (w io.Writer , v interface {}) {
143
+ func printYAML (w io.Writer , v interface {}) error {
141
144
b , err := yaml .Marshal (v )
142
145
if err != nil {
143
146
Error (w , fmt .Sprintf ("failed printing to yaml: %v" , err ))
144
147
}
145
- fmt .Fprintln (w , string (b ))
148
+ _ , err = fmt .Fprintln (w , string (b ))
149
+ return err
146
150
}
147
151
148
- func printTable (header []string , w io.Writer , rows [][]string ) {
152
+ func printTable (header []string , w io.Writer , rows [][]string ) error {
149
153
if len (header ) > 5 {
150
154
panic ("maximum allowed length of a table header is only 5." )
151
155
}
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
167
182
}
0 commit comments