Skip to content

Commit 25bd59b

Browse files
authored
Merge pull request #181 from metal-stack/upgrade-tablewriter
Upgrade Tablewriter to v1
2 parents 255408b + 01cc9ec commit 25bd59b

File tree

5 files changed

+148
-97
lines changed

5 files changed

+148
-97
lines changed

go.mod

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/metal-stack/v v1.0.3
2727
github.com/nsqio/go-nsq v1.1.0
2828
github.com/nsqio/nsq v1.3.0
29-
github.com/olekukonko/tablewriter v0.0.5
29+
github.com/olekukonko/tablewriter v1.0.8
3030
github.com/spf13/afero v1.14.0
3131
github.com/spf13/cobra v1.9.1
3232
github.com/spf13/viper v1.20.0
@@ -128,8 +128,8 @@ require (
128128
github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed // indirect
129129
github.com/magiconair/properties v1.8.7 // indirect
130130
github.com/mailru/easyjson v0.9.0 // indirect
131-
github.com/mattn/go-colorable v0.1.13 // indirect
132-
github.com/mattn/go-runewidth v0.0.15 // indirect
131+
github.com/mattn/go-colorable v0.1.14 // indirect
132+
github.com/mattn/go-runewidth v0.0.16 // indirect
133133
github.com/mdlayher/genetlink v1.3.2 // indirect
134134
github.com/mdlayher/netlink v1.7.2 // indirect
135135
github.com/mdlayher/sdnotify v1.0.0 // indirect
@@ -146,6 +146,8 @@ require (
146146
github.com/morikuni/aec v1.0.0 // indirect
147147
github.com/nsqio/go-diskqueue v1.1.0 // indirect
148148
github.com/oklog/ulid v1.3.1 // indirect
149+
github.com/olekukonko/errors v1.1.0 // indirect
150+
github.com/olekukonko/ll v0.0.9 // indirect
149151
github.com/opencontainers/go-digest v1.0.0 // indirect
150152
github.com/opencontainers/image-spec v1.1.0 // indirect
151153
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
@@ -154,7 +156,7 @@ require (
154156
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
155157
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
156158
github.com/prometheus-community/pro-bing v0.4.0 // indirect
157-
github.com/rivo/uniseg v0.4.4 // indirect
159+
github.com/rivo/uniseg v0.4.7 // indirect
158160
github.com/safchain/ethtool v0.3.0 // indirect
159161
github.com/sagikazarmark/locafero v0.7.0 // indirect
160162
github.com/segmentio/asm v1.2.0 // indirect
@@ -196,7 +198,7 @@ require (
196198
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
197199
golang.org/x/mod v0.24.0 // indirect
198200
golang.org/x/net v0.38.0 // indirect
199-
golang.org/x/sys v0.31.0 // indirect
201+
golang.org/x/sys v0.34.0 // indirect
200202
golang.org/x/term v0.30.0
201203
golang.org/x/text v0.23.0 // indirect
202204
golang.org/x/time v0.11.0 // indirect

go.sum

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,12 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN
291291
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
292292
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
293293
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
294-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
295-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
296-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
294+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
295+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
297296
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
298297
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
299-
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
300-
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
301-
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
298+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
299+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
302300
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
303301
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
304302
github.com/mdlayher/genetlink v1.3.2 h1:KdrNKe+CTu+IbZnm/GVUMXSqBBLqcGpRDa0xkQy56gw=
@@ -346,8 +344,12 @@ github.com/nsqio/nsq v1.3.0 h1:v7NtyO844ieTIOCQEqQ7IUSSi1ImhgrTTto1rgIYGEU=
346344
github.com/nsqio/nsq v1.3.0/go.mod h1:RxNr6UC0kSkNF44LnJrlN3U3CQnQGTXk+QKfSZLzqvc=
347345
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
348346
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
349-
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
350-
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
347+
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
348+
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
349+
github.com/olekukonko/ll v0.0.9 h1:Y+1YqDfVkqMWuEQMclsF9HUR5+a82+dxJuL1HHSRpxI=
350+
github.com/olekukonko/ll v0.0.9/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
351+
github.com/olekukonko/tablewriter v1.0.8 h1:f6wJzHg4QUtJdvrVPKco4QTrAylgaU0+b9br/lJxEiQ=
352+
github.com/olekukonko/tablewriter v1.0.8/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs=
351353
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
352354
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
353355
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
@@ -370,8 +372,8 @@ github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b/go.mod h1:Om
370372
github.com/prometheus-community/pro-bing v0.4.0 h1:YMbv+i08gQz97OZZBwLyvmmQEEzyfyrrjEaAchdy3R4=
371373
github.com/prometheus-community/pro-bing v0.4.0/go.mod h1:b7wRYZtCcPmt4Sz319BykUU241rWLe1VFXyiyWK/dH4=
372374
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
373-
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
374-
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
375+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
376+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
375377
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
376378
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
377379
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -532,16 +534,15 @@ golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7w
532534
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
533535
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
534536
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
535-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
536537
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
537538
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
538539
golang.org/x/sys v0.4.1-0.20230131160137-e7d7f63158de/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
539540
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
540541
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
541542
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
542543
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
543-
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
544-
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
544+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
545+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
545546
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
546547
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
547548
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

pkg/genericcli/fromfile_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func TestApplyFromFile(t *testing.T) {
5151
wantOutput: `
5252
| ID | NAME |
5353
|----|------|
54-
| 1 | one |
54+
| 1 | one |
5555
`,
5656
wantBulkOutput: `
5757
| ID | NAME |
5858
|----|------|
59-
| 1 | one |
59+
| 1 | one |
6060
`,
6161
},
6262
{
@@ -93,16 +93,16 @@ name: two
9393
wantOutput: `
9494
| ID | NAME |
9595
|----|------|
96-
| 1 | one |
96+
| 1 | one |
9797
| ID | NAME |
9898
|----|------|
99-
| 2 | two |
99+
| 2 | two |
100100
`,
101101
wantBulkOutput: `
102102
| ID | NAME |
103103
|----|------|
104-
| 1 | one |
105-
| 2 | two |
104+
| 1 | one |
105+
| 2 | two |
106106
`,
107107
},
108108
{
@@ -140,16 +140,16 @@ name: two
140140
wantOutput: `
141141
| ID | NAME |
142142
|----|------|
143-
| 1 | one |
143+
| 1 | one |
144144
| ID | NAME |
145145
|----|------|
146-
| 2 | two |
146+
| 2 | two |
147147
`,
148148
wantBulkOutput: `
149149
| ID | NAME |
150150
|----|------|
151-
| 1 | one |
152-
| 2 | two |
151+
| 1 | one |
152+
| 2 | two |
153153
`,
154154
},
155155
{
@@ -185,12 +185,12 @@ name: two
185185
error creating entity: creation error for id 1
186186
| ID | NAME |
187187
|----|------|
188-
| 2 | two |
188+
| 2 | two |
189189
`,
190190
wantBulkOutput: `
191191
| ID | NAME |
192192
|----|------|
193-
| 2 | two |
193+
| 2 | two |
194194
`,
195195
wantErr: fmt.Errorf("error creating entity: creation error for id 1"),
196196
},

pkg/genericcli/printers/table.go

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"io"
66
"os"
77

8-
"github.com/metal-stack/metal-lib/pkg/pointer"
98
"github.com/olekukonko/tablewriter"
9+
"github.com/olekukonko/tablewriter/renderer"
10+
"github.com/olekukonko/tablewriter/tw"
1011
)
1112

1213
// TablePrinter prints data into a table
@@ -27,19 +28,16 @@ type TablePrinterConfig struct {
2728
NoHeaders bool
2829
// Out defines the output writer for the printer, will default to os.stdout
2930
Out io.Writer
30-
// CustomPadding defines the table padding, defaults to three whitespaces
31-
CustomPadding *string
3231
// DisableDefaultErrorPrinter disables the default error printer when the given print data is of type error.
3332
DisableDefaultErrorPrinter bool
33+
// Autowrap Text by default, set true to disable
34+
DisableAutoWrap bool
3435
}
3536

3637
func NewTablePrinter(config *TablePrinterConfig) *TablePrinter {
3738
if config.Out == nil {
3839
config.Out = os.Stdout
3940
}
40-
if config.CustomPadding == nil {
41-
config.CustomPadding = pointer.Pointer(" ")
42-
}
4341

4442
return &TablePrinter{
4543
c: config,
@@ -51,9 +49,16 @@ func (p *TablePrinter) WithOut(out io.Writer) *TablePrinter {
5149
return p
5250
}
5351

54-
// MutateTable can be used to alter the table element. Try not to do it all the time but rather propose an API change in this project.
55-
func (p *TablePrinter) MutateTable(mutateFn func(table *tablewriter.Table)) {
56-
mutateFn(p.table)
52+
func (p *TablePrinter) DisableAutoWrap(disable bool) {
53+
p.c.DisableAutoWrap = disable
54+
}
55+
56+
func wrapOption(config *TablePrinterConfig) int {
57+
if config.DisableAutoWrap {
58+
return tw.WrapNone
59+
}
60+
61+
return tw.WrapNormal
5762
}
5863

5964
func (p *TablePrinter) Print(data any) error {
@@ -72,11 +77,16 @@ func (p *TablePrinter) Print(data any) error {
7277
}
7378

7479
if !p.c.NoHeaders {
75-
p.table.SetHeader(header)
80+
p.table.Header(header)
7681
}
77-
p.table.AppendBulk(rows)
7882

79-
p.table.Render()
83+
if err := p.table.Bulk(rows); err != nil {
84+
return err
85+
}
86+
87+
if err := p.table.Render(); err != nil {
88+
return err
89+
}
8090

8191
return nil
8292
}
@@ -85,26 +95,99 @@ func (p *TablePrinter) initTable() error {
8595
if p.c.ToHeaderAndRows == nil {
8696
return fmt.Errorf("missing to header and rows function in printer configuration")
8797
}
88-
if p.c.CustomPadding == nil {
89-
return fmt.Errorf("padding must be set")
90-
}
91-
92-
p.table = tablewriter.NewWriter(p.c.Out)
9398

9499
if p.c.Markdown {
95-
p.table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
96-
p.table.SetCenterSeparator("|")
100+
101+
symbols := tw.NewSymbolCustom("Markdown").
102+
WithRow("-").
103+
WithColumn("|").
104+
WithCenter("|").
105+
WithMidLeft("|").
106+
WithMidRight("|")
107+
108+
p.table = tablewriter.NewTable(p.c.Out,
109+
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
110+
Borders: tw.Border{Left: tw.On, Top: tw.Off, Right: tw.On, Bottom: tw.Off},
111+
Symbols: symbols,
112+
Settings: tw.Settings{
113+
Lines: tw.Lines{
114+
ShowHeaderLine: tw.On,
115+
ShowFooterLine: tw.On,
116+
},
117+
Separators: tw.Separators{
118+
ShowHeader: tw.On,
119+
ShowFooter: tw.On,
120+
},
121+
},
122+
})),
123+
tablewriter.WithConfig(tablewriter.Config{
124+
Header: tw.CellConfig{
125+
Alignment: tw.CellAlignment{
126+
Global: tw.AlignLeft,
127+
},
128+
Formatting: tw.CellFormatting{
129+
AutoWrap: wrapOption(p.c),
130+
},
131+
},
132+
Row: tw.CellConfig{
133+
Alignment: tw.CellAlignment{
134+
Global: tw.AlignLeft,
135+
},
136+
Formatting: tw.CellFormatting{
137+
AutoWrap: wrapOption(p.c),
138+
},
139+
},
140+
}),
141+
)
97142
} else {
98-
p.table.SetHeaderLine(false)
99-
p.table.SetAlignment(tablewriter.ALIGN_LEFT)
100-
p.table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
101-
p.table.SetBorder(false)
102-
p.table.SetCenterSeparator("")
103-
p.table.SetColumnSeparator("")
104-
p.table.SetRowSeparator("")
105-
p.table.SetRowLine(false)
106-
p.table.SetTablePadding(*p.c.CustomPadding)
107-
p.table.SetNoWhiteSpace(true) // no whitespace in front of every line
143+
symbols := tw.NewSymbolCustom("Default").
144+
WithColumn("")
145+
146+
padding := tw.CellPadding{
147+
Global: tw.Padding{
148+
Right: " ",
149+
},
150+
}
151+
152+
p.table = tablewriter.NewTable(p.c.Out,
153+
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
154+
Borders: tw.BorderNone,
155+
Symbols: symbols,
156+
Settings: tw.Settings{
157+
Lines: tw.Lines{
158+
ShowHeaderLine: tw.Off,
159+
ShowFooterLine: tw.Off,
160+
},
161+
Separators: tw.Separators{
162+
BetweenRows: tw.Off,
163+
BetweenColumns: tw.Off,
164+
ShowHeader: tw.Off,
165+
ShowFooter: tw.Off,
166+
},
167+
},
168+
})),
169+
tablewriter.WithConfig(tablewriter.Config{
170+
Header: tw.CellConfig{
171+
Alignment: tw.CellAlignment{
172+
Global: tw.AlignLeft,
173+
},
174+
Padding: padding,
175+
Formatting: tw.CellFormatting{
176+
AutoWrap: wrapOption(p.c),
177+
},
178+
},
179+
Row: tw.CellConfig{
180+
Alignment: tw.CellAlignment{
181+
Global: tw.AlignLeft,
182+
},
183+
Padding: padding,
184+
Formatting: tw.CellFormatting{
185+
AutoWrap: wrapOption(p.c),
186+
},
187+
},
188+
Behavior: tw.Behavior{TrimSpace: tw.On},
189+
}),
190+
)
108191
}
109192

110193
return nil

0 commit comments

Comments
 (0)