Skip to content

Commit de26713

Browse files
committed
Replace olekukonko/tablewriter with text/tabwriter
Drop external tablewriter dependency in favor of Go's built-in text/tabwriter package to reduce dependencies and simplify maintenance. Changes: - Replace tablewriter.NewWriter() with tabwriter.NewWriter() in all table display functions - Update table formatting logic to use tab-separated output with headers and separator lines - Remove olekukonko/tablewriter and related dependencies from go.mod - Update test expectations to match new table output format - Fix test line number references after table format changes All tests pass with the new implementation. Assisted-by: Claude AI for dependency replacement and test updates Signed-off-by: Adrian Reber <areber@redhat.com>
1 parent 6e6a8c6 commit de26713

46 files changed

Lines changed: 162 additions & 19225 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/list.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"log"
1010
"os"
1111
"path/filepath"
12+
"strings"
13+
"text/tabwriter"
1214
"time"
1315

1416
"github.com/checkpoint-restore/checkpointctl/internal"
15-
"github.com/olekukonko/tablewriter"
1617
"github.com/spf13/cobra"
1718
)
1819

@@ -38,7 +39,7 @@ func list(cmd *cobra.Command, args []string) error {
3839
}()
3940
showTable := false
4041

41-
table := tablewriter.NewWriter(os.Stdout)
42+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
4243
header := []string{
4344
"Namespace",
4445
"Pod",
@@ -48,9 +49,7 @@ func list(cmd *cobra.Command, args []string) error {
4849
"Checkpoint Name",
4950
}
5051

51-
table.SetHeader(header)
52-
table.SetAutoMergeCells(false)
53-
table.SetRowLine(true)
52+
var rows [][]string
5453

5554
for _, checkpointPath := range allPaths {
5655
files, err := filepath.Glob(filepath.Join(checkpointPath, "checkpoint-*"))
@@ -81,7 +80,7 @@ func list(cmd *cobra.Command, args []string) error {
8180
filepath.Base(file),
8281
}
8382

84-
table.Append(row)
83+
rows = append(rows, row)
8584
}
8685
}
8786

@@ -90,6 +89,35 @@ func list(cmd *cobra.Command, args []string) error {
9089
return nil
9190
}
9291

93-
table.Render()
92+
// Print header
93+
for i, h := range header {
94+
if i > 0 {
95+
fmt.Fprint(w, "\t")
96+
}
97+
fmt.Fprint(w, h)
98+
}
99+
fmt.Fprintln(w)
100+
101+
// Print separator line
102+
for i := range header {
103+
if i > 0 {
104+
fmt.Fprint(w, "\t")
105+
}
106+
fmt.Fprint(w, strings.Repeat("-", len(header[i])))
107+
}
108+
fmt.Fprintln(w)
109+
110+
// Print rows
111+
for _, row := range rows {
112+
for i, cell := range row {
113+
if i > 0 {
114+
fmt.Fprint(w, "\t")
115+
}
116+
fmt.Fprint(w, cell)
117+
}
118+
fmt.Fprintln(w)
119+
}
120+
121+
w.Flush()
94122
return nil
95123
}

cmd/memparse.go

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
"io"
1111
"os"
1212
"path/filepath"
13+
"strings"
14+
"text/tabwriter"
1315

1416
"github.com/checkpoint-restore/checkpointctl/internal"
1517
metadata "github.com/checkpoint-restore/checkpointctl/lib"
1618
"github.com/checkpoint-restore/go-criu/v7/crit"
17-
"github.com/olekukonko/tablewriter"
1819
"github.com/spf13/cobra"
1920
)
2021

@@ -116,21 +117,16 @@ func memparse(cmd *cobra.Command, args []string) error {
116117

117118
// Display processes memory sizes within the given container checkpoints.
118119
func showProcessMemorySizeTables(tasks []internal.Task) error {
119-
// Initialize the table
120-
table := tablewriter.NewWriter(os.Stdout)
121120
header := []string{
122121
"PID",
123122
"Process name",
124123
"Memory size",
125124
"Shared memory size",
126125
}
127-
table.SetHeader(header)
128-
table.SetAutoMergeCells(false)
129-
table.SetRowLine(true)
130126

131127
// Function to recursively traverse the process tree and populate the table rows
132-
var traverseTree func(*crit.PsTree, string) error
133-
traverseTree = func(root *crit.PsTree, checkpointOutputDir string) error {
128+
var traverseTree func(*crit.PsTree, string, *[][]string) error
129+
traverseTree = func(root *crit.PsTree, checkpointOutputDir string, rows *[][]string) error {
134130
memReader, err := crit.NewMemoryReader(
135131
filepath.Join(checkpointOutputDir, metadata.CheckpointDirectory),
136132
root.PID, pageSize,
@@ -152,24 +148,25 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
152148
return err
153149
}
154150

155-
table.Append([]string{
151+
row := []string{
156152
fmt.Sprintf("%d", root.PID),
157153
root.Comm,
158154
metadata.ByteToString(memSize),
159155
metadata.ByteToString(shmemSize),
160-
})
156+
}
157+
*rows = append(*rows, row)
161158

162159
for _, child := range root.Children {
163-
if err := traverseTree(child, checkpointOutputDir); err != nil {
160+
if err := traverseTree(child, checkpointOutputDir, rows); err != nil {
164161
return err
165162
}
166163
}
167164
return nil
168165
}
169166

170167
for _, task := range tasks {
171-
// Clear the table before processing each checkpoint task
172-
table.ClearRows()
168+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
169+
var rows [][]string
173170

174171
c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
175172
psTree, err := c.ExplorePs()
@@ -178,12 +175,42 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
178175
}
179176

180177
// Populate the table rows
181-
if err := traverseTree(psTree, task.OutputDir); err != nil {
178+
if err := traverseTree(psTree, task.OutputDir, &rows); err != nil {
182179
return err
183180
}
184181

185182
fmt.Printf("\nDisplaying processes memory sizes from %s\n\n", task.CheckpointFilePath)
186-
table.Render()
183+
184+
// Print header
185+
for i, h := range header {
186+
if i > 0 {
187+
fmt.Fprint(w, "\t")
188+
}
189+
fmt.Fprint(w, h)
190+
}
191+
fmt.Fprintln(w)
192+
193+
// Print separator line
194+
for i := range header {
195+
if i > 0 {
196+
fmt.Fprint(w, "\t")
197+
}
198+
fmt.Fprint(w, strings.Repeat("-", len(header[i])))
199+
}
200+
fmt.Fprintln(w)
201+
202+
// Print rows
203+
for _, row := range rows {
204+
for i, cell := range row {
205+
if i > 0 {
206+
fmt.Fprint(w, "\t")
207+
}
208+
fmt.Fprint(w, cell)
209+
}
210+
fmt.Fprintln(w)
211+
}
212+
213+
w.Flush()
187214
}
188215

189216
return nil
@@ -348,21 +375,43 @@ func printMemorySearchResultForPID(task internal.Task) error {
348375
return nil
349376
}
350377

351-
table := tablewriter.NewWriter(os.Stdout)
352-
table.SetHeader([]string{"Address", "Match", "Instance"})
353-
table.SetAutoMergeCells(false)
354-
table.SetRowLine(true)
378+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
379+
header := []string{"Address", "Match", "Instance"}
380+
381+
// Print header
382+
for i, h := range header {
383+
if i > 0 {
384+
fmt.Fprint(w, "\t")
385+
}
386+
fmt.Fprint(w, h)
387+
}
388+
fmt.Fprintln(w)
355389

390+
// Print separator line
391+
for i := range header {
392+
if i > 0 {
393+
fmt.Fprint(w, "\t")
394+
}
395+
fmt.Fprint(w, strings.Repeat("-", len(header[i])))
396+
}
397+
fmt.Fprintln(w)
398+
399+
// Print rows
356400
for i, result := range results {
357-
table.Append([]string{
358-
fmt.Sprintf(
359-
"%016x", result.Vaddr),
401+
row := []string{
402+
fmt.Sprintf("%016x", result.Vaddr),
360403
result.Match,
361404
fmt.Sprintf("%d", i+1),
362-
})
405+
}
406+
for j, cell := range row {
407+
if j > 0 {
408+
fmt.Fprint(w, "\t")
409+
}
410+
fmt.Fprint(w, cell)
411+
}
412+
fmt.Fprintln(w)
363413
}
364414

365-
table.Render()
366-
415+
w.Flush()
367416
return nil
368417
}

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ toolchain go1.24.2
77
require (
88
github.com/checkpoint-restore/go-criu/v7 v7.2.0
99
github.com/containers/storage v1.58.0
10-
github.com/olekukonko/tablewriter v0.0.5
1110
github.com/opencontainers/runtime-spec v1.2.1
1211
github.com/spf13/cobra v1.9.1
1312
github.com/xlab/treeprint v1.2.0
@@ -18,11 +17,9 @@ require (
1817
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1918
github.com/klauspost/compress v1.18.0 // indirect
2019
github.com/klauspost/pgzip v1.2.6 // indirect
21-
github.com/mattn/go-runewidth v0.0.16 // indirect
2220
github.com/moby/sys/capability v0.4.0 // indirect
2321
github.com/moby/sys/mountinfo v0.7.2 // indirect
2422
github.com/moby/sys/user v0.4.0 // indirect
25-
github.com/rivo/uniseg v0.4.7 // indirect
2623
github.com/sirupsen/logrus v1.9.3 // indirect
2724
github.com/spf13/pflag v1.0.6 // indirect
2825
github.com/ulikunitz/xz v0.5.12 // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@ github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zt
1616
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
1717
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
1818
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
19-
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
20-
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
21-
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2219
github.com/moby/sys/capability v0.4.0 h1:4D4mI6KlNtWMCM1Z/K0i7RV1FkX+DBDHKVJpCndZoHk=
2320
github.com/moby/sys/capability v0.4.0/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
2421
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
2522
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
2623
github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs=
2724
github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
28-
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
29-
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
3025
github.com/opencontainers/runtime-spec v1.2.1 h1:S4k4ryNgEpxW1dzyqffOmhI1BHYcjzU8lpJfSlR0xww=
3126
github.com/opencontainers/runtime-spec v1.2.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
3227
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3328
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34-
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
35-
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
36-
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3729
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
3830
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
3931
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=

internal/container.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"os"
1414
"path/filepath"
1515
"strings"
16+
"text/tabwriter"
1617
"time"
1718

1819
metadata "github.com/checkpoint-restore/checkpointctl/lib"
1920
"github.com/checkpoint-restore/go-criu/v7/crit"
2021
"github.com/containers/storage/pkg/archive"
21-
"github.com/olekukonko/tablewriter"
2222
spec "github.com/opencontainers/runtime-spec/specs-go"
2323
)
2424

@@ -107,7 +107,8 @@ func getCheckpointInfo(task Task) (*checkpointInfo, error) {
107107
}
108108

109109
func ShowContainerCheckpoints(tasks []Task) error {
110-
table := tablewriter.NewWriter(os.Stdout)
110+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
111+
111112
header := []string{
112113
"Container",
113114
"Image",
@@ -121,6 +122,8 @@ func ShowContainerCheckpoints(tasks []Task) error {
121122
header = append(header, "IP", "MAC", "CHKPT Size", "Root Fs Diff Size")
122123
}
123124

125+
var rows [][]string
126+
124127
for _, task := range tasks {
125128
info, err := getCheckpointInfo(task)
126129
if err != nil {
@@ -167,14 +170,39 @@ func ShowContainerCheckpoints(tasks []Task) error {
167170
row = append(row, metadata.ByteToString(info.archiveSizes.rootFsDiffTarSize))
168171
}
169172

170-
table.Append(row)
173+
rows = append(rows, row)
174+
}
175+
176+
// Print header
177+
for i, h := range header {
178+
if i > 0 {
179+
fmt.Fprint(w, "\t")
180+
}
181+
fmt.Fprint(w, h)
182+
}
183+
fmt.Fprintln(w)
184+
185+
// Print separator line
186+
for i := range header {
187+
if i > 0 {
188+
fmt.Fprint(w, "\t")
189+
}
190+
fmt.Fprint(w, strings.Repeat("-", len(header[i])))
171191
}
192+
fmt.Fprintln(w)
172193

173-
table.SetHeader(header)
174-
table.SetAutoMergeCells(false)
175-
table.SetRowLine(true)
176-
table.Render()
194+
// Print rows
195+
for _, row := range rows {
196+
for i, cell := range row {
197+
if i > 0 {
198+
fmt.Fprint(w, "\t")
199+
}
200+
fmt.Fprint(w, cell)
201+
}
202+
fmt.Fprintln(w)
203+
}
177204

205+
w.Flush()
178206
return nil
179207
}
180208

0 commit comments

Comments
 (0)