File: 0001-Replace-olekukonko-tablewriter-with-text-tabwriter.patch

package info (click to toggle)
golang-github-checkpoint-restore-checkpointctl 1.3.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 628 kB
  • sloc: ansic: 208; makefile: 172; sh: 40
file content (484 lines) | stat: -rw-r--r-- 17,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
From: Adrian Reber <areber@redhat.com>
Date: Sat, 2 Aug 2025 13:08:20 +0000
Subject: 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>
Signed-off-by: Radostin Stoyanov <rstoyano@redhat.com>
---
 README.md               | 36 +++++++++++++++++-----------------
 cmd/list.go             | 14 +++++++-------
 cmd/memparse.go         | 50 +++++++++++++++++++++++++-----------------------
 internal/container.go   | 15 ++++++++-------
 internal/utils.go       | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/checkpointctl.bats | 30 +++++++++++++++++------------
 6 files changed, 127 insertions(+), 69 deletions(-)

diff --git a/README.md b/README.md
index 9c4320f..99b2a98 100644
--- a/README.md
+++ b/README.md
@@ -30,24 +30,25 @@ To display an overview of a checkpoint archive you can just use
 ```console
 $ checkpointctl show /tmp/dump.tar
 
-+-----------------+------------------------------------------+--------------+---------+----------------------+--------+------------+-------------------+
-|    CONTAINER    |                  IMAGE                   |      ID      | RUNTIME |       CREATED        | ENGINE | CHKPT SIZE | ROOT FS DIFF SIZE |
-+-----------------+------------------------------------------+--------------+---------+----------------------+--------+------------+-------------------+
-| magical_murdock | quay.io/adrianreber/wildfly-hello:latest | f11d11844af0 | crun    | 2023-02-28T09:43:52Z | Podman | 338.2 MiB  | 177.0 KiB         |
-+-----------------+------------------------------------------+--------------+---------+----------------------+--------+------------+-------------------+
+Displaying container checkpoint data from /root/dump.tar
+
+CONTAINER  IMAGE                             ID            RUNTIME  CREATED               ENGINE  CHKPT SIZE ROOT FS DIFF SIZE
+---------  -----                             --            -------  -------               ------  ---------- -----------------
+looper     docker.io/library/busybox:latest  8b5c2ca15082  crun     2021-09-28T10:03:56Z  Podman  130.8 KiB  204 B
 ```
 
 For a checkpoint archive created by Kubernetes with *CRI-O* the output would
 look like this:
 
 ```console
-$ checkpointctl show /var/lib/kubelet/checkpoints/checkpoint-counters_default-counter-2023-02-13T16\:20\:09Z.tar
+$ checkpointctl show /var/lib/kubelet/checkpoints/checkpoint-counters_default-counter-2025-05-22T14\:31\:35Z.tar
+
+Displaying container checkpoint data from /var/lib/kubelet/checkpoints/checkpoint-counters_default-counter-2025-05-22T14:31:35Z.tar
+
+CONTAINER  IMAGE                               ID            RUNTIME  CREATED                         ENGINE  IP         CHKPT SIZE  ROOT FS DIFF SIZE
+---------  -----                               --            -------  -------                         ------  --         ----------  -----------------
+counter    quay.io/adrianreber/counter:latest  29ed106ef467  runc     2025-05-22T14:31:24.818422898Z  CRI-O   10.0.0.70  9.2 MiB     2.0 KiB
 
-+-----------+------------------------------------+--------------+---------+--------------------------------+--------+------------+------------+
-| CONTAINER |               IMAGE                |      ID      | RUNTIME |            CREATED             | ENGINE |     IP     | CHKPT SIZE |
-+-----------+------------------------------------+--------------+---------+--------------------------------+--------+------------+------------+
-| counter   | quay.io/adrianreber/counter:latest | 7eb9680287f1 | runc    | 2023-02-13T16:12:25.843774934Z | CRI-O  | 10.88.0.24 | 8.5 MiB    |
-+-----------+------------------------------------+--------------+---------+--------------------------------+--------+------------+------------+
 ```
 
 ### `inspect` sub-command
@@ -88,7 +89,7 @@ $ checkpointctl memparse /tmp/jira.tar.gz  --pid=1 | less
 
 Displaying memory pages content for Process ID 1 from checkpoint: /tmp/jira.tar.gz
 
-Address           Hexadecimal                                       ASCII
+ADDRESS           HEXADECIMAL                                       ASCII
 -------------------------------------------------------------------------------------
 00005633bb080000  f3 0f 1e fa 48 83 ec 08 48 8b 05 d1 4f 00 00 48  |....H...H...O..H|
 00005633bb080010  85 c0 74 02 ff d0 48 83 c4 08 c3 00 00 00 00 00  |..t...H.........|
@@ -145,13 +146,10 @@ $ sudo checkpointctl memparse /tmp/jira.tar.gz
 
 Displaying processes memory sizes from /tmp/jira.tar.gz
 
-+-----+--------------+-------------+
-| PID | PROCESS NAME | MEMORY SIZE |
-+-----+--------------+-------------+
-|   1 | tini         | 100.0 KiB   |
-+-----+--------------+-------------+
-|   2 | java         | 553.5 MiB   |
-+-----+--------------+-------------+
+PID  PROCESS NAME  MEMORY SIZE  SHARED MEMORY SIZE
+---  ------------  -----------  ------------------
+1    tini          100.0 KiB    0 B
+2    java          553.5 MiB    0 B
 ```
 
 In this example, given the large size of the java process, it is better to write its output to a file.
diff --git a/cmd/list.go b/cmd/list.go
index a562a54..f3f280c 100644
--- a/cmd/list.go
+++ b/cmd/list.go
@@ -12,7 +12,6 @@ import (
 	"time"
 
 	"github.com/checkpoint-restore/checkpointctl/internal"
-	"github.com/olekukonko/tablewriter"
 	"github.com/spf13/cobra"
 )
 
@@ -38,7 +37,7 @@ func list(cmd *cobra.Command, args []string) error {
 	}()
 	showTable := false
 
-	table := tablewriter.NewWriter(os.Stdout)
+	w := internal.GetNewTabWriter(os.Stdout)
 	header := []string{
 		"Namespace",
 		"Pod",
@@ -48,9 +47,7 @@ func list(cmd *cobra.Command, args []string) error {
 		"Checkpoint Name",
 	}
 
-	table.SetHeader(header)
-	table.SetAutoMergeCells(false)
-	table.SetRowLine(true)
+	var rows [][]string
 
 	for _, checkpointPath := range allPaths {
 		files, err := filepath.Glob(filepath.Join(checkpointPath, "checkpoint-*"))
@@ -81,7 +78,7 @@ func list(cmd *cobra.Command, args []string) error {
 				filepath.Base(file),
 			}
 
-			table.Append(row)
+			rows = append(rows, row)
 		}
 	}
 
@@ -90,6 +87,9 @@ func list(cmd *cobra.Command, args []string) error {
 		return nil
 	}
 
-	table.Render()
+	internal.WriteTableHeader(w, header)
+	internal.WriteTableRows(w, rows)
+
+	w.Flush()
 	return nil
 }
diff --git a/cmd/memparse.go b/cmd/memparse.go
index 8f69ec1..51c85fb 100644
--- a/cmd/memparse.go
+++ b/cmd/memparse.go
@@ -14,7 +14,6 @@ import (
 	"github.com/checkpoint-restore/checkpointctl/internal"
 	metadata "github.com/checkpoint-restore/checkpointctl/lib"
 	"github.com/checkpoint-restore/go-criu/v7/crit"
-	"github.com/olekukonko/tablewriter"
 	"github.com/spf13/cobra"
 )
 
@@ -116,21 +115,16 @@ func memparse(cmd *cobra.Command, args []string) error {
 
 // Display processes memory sizes within the given container checkpoints.
 func showProcessMemorySizeTables(tasks []internal.Task) error {
-	// Initialize the table
-	table := tablewriter.NewWriter(os.Stdout)
 	header := []string{
 		"PID",
 		"Process name",
 		"Memory size",
 		"Shared memory size",
 	}
-	table.SetHeader(header)
-	table.SetAutoMergeCells(false)
-	table.SetRowLine(true)
 
 	// Function to recursively traverse the process tree and populate the table rows
-	var traverseTree func(*crit.PsTree, string) error
-	traverseTree = func(root *crit.PsTree, checkpointOutputDir string) error {
+	var traverseTree func(*crit.PsTree, string, *[][]string) error
+	traverseTree = func(root *crit.PsTree, checkpointOutputDir string, rows *[][]string) error {
 		memReader, err := crit.NewMemoryReader(
 			filepath.Join(checkpointOutputDir, metadata.CheckpointDirectory),
 			root.PID, pageSize,
@@ -152,15 +146,16 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
 			return err
 		}
 
-		table.Append([]string{
+		row := []string{
 			fmt.Sprintf("%d", root.PID),
 			root.Comm,
 			metadata.ByteToString(memSize),
 			metadata.ByteToString(shmemSize),
-		})
+		}
+		*rows = append(*rows, row)
 
 		for _, child := range root.Children {
-			if err := traverseTree(child, checkpointOutputDir); err != nil {
+			if err := traverseTree(child, checkpointOutputDir, rows); err != nil {
 				return err
 			}
 		}
@@ -168,8 +163,8 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
 	}
 
 	for _, task := range tasks {
-		// Clear the table before processing each checkpoint task
-		table.ClearRows()
+		w := internal.GetNewTabWriter(os.Stdout)
+		var rows [][]string
 
 		c := crit.New(nil, nil, filepath.Join(task.OutputDir, "checkpoint"), false, false)
 		psTree, err := c.ExplorePs()
@@ -178,12 +173,16 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
 		}
 
 		// Populate the table rows
-		if err := traverseTree(psTree, task.OutputDir); err != nil {
+		if err := traverseTree(psTree, task.OutputDir, &rows); err != nil {
 			return err
 		}
 
 		fmt.Printf("\nDisplaying processes memory sizes from %s\n\n", task.CheckpointFilePath)
-		table.Render()
+
+		internal.WriteTableHeader(w, header)
+		internal.WriteTableRows(w, rows)
+
+		w.Flush()
 	}
 
 	return nil
@@ -348,21 +347,24 @@ func printMemorySearchResultForPID(task internal.Task) error {
 		return nil
 	}
 
-	table := tablewriter.NewWriter(os.Stdout)
-	table.SetHeader([]string{"Address", "Match", "Instance"})
-	table.SetAutoMergeCells(false)
-	table.SetRowLine(true)
+	w := internal.GetNewTabWriter(os.Stdout)
+	header := []string{"Address", "Match", "Instance"}
 
+	internal.WriteTableHeader(w, header)
+
+	// Build rows
+	var rows [][]string
 	for i, result := range results {
-		table.Append([]string{
-			fmt.Sprintf(
-				"%016x", result.Vaddr),
+		row := []string{
+			fmt.Sprintf("%016x", result.Vaddr),
 			result.Match,
 			fmt.Sprintf("%d", i+1),
-		})
+		}
+		rows = append(rows, row)
 	}
 
-	table.Render()
+	internal.WriteTableRows(w, rows)
 
+	w.Flush()
 	return nil
 }
diff --git a/internal/container.go b/internal/container.go
index 791785d..ace2e56 100644
--- a/internal/container.go
+++ b/internal/container.go
@@ -18,7 +18,6 @@ import (
 	metadata "github.com/checkpoint-restore/checkpointctl/lib"
 	"github.com/checkpoint-restore/go-criu/v7/crit"
 	"github.com/containers/storage/pkg/archive"
-	"github.com/olekukonko/tablewriter"
 	spec "github.com/opencontainers/runtime-spec/specs-go"
 )
 
@@ -107,7 +106,8 @@ func getCheckpointInfo(task Task) (*checkpointInfo, error) {
 }
 
 func ShowContainerCheckpoints(tasks []Task) error {
-	table := tablewriter.NewWriter(os.Stdout)
+	w := GetNewTabWriter(os.Stdout)
+
 	header := []string{
 		"Container",
 		"Image",
@@ -121,6 +121,8 @@ func ShowContainerCheckpoints(tasks []Task) error {
 		header = append(header, "IP", "MAC", "CHKPT Size", "Root Fs Diff Size")
 	}
 
+	var rows [][]string
+
 	for _, task := range tasks {
 		info, err := getCheckpointInfo(task)
 		if err != nil {
@@ -167,14 +169,13 @@ func ShowContainerCheckpoints(tasks []Task) error {
 			row = append(row, metadata.ByteToString(info.archiveSizes.rootFsDiffTarSize))
 		}
 
-		table.Append(row)
+		rows = append(rows, row)
 	}
 
-	table.SetHeader(header)
-	table.SetAutoMergeCells(false)
-	table.SetRowLine(true)
-	table.Render()
+	WriteTableHeader(w, header)
+	WriteTableRows(w, rows)
 
+	w.Flush()
 	return nil
 }
 
diff --git a/internal/utils.go b/internal/utils.go
index fc70d9b..1aeb779 100644
--- a/internal/utils.go
+++ b/internal/utils.go
@@ -2,8 +2,10 @@ package internal
 
 import (
 	"fmt"
+	"io"
 	"os"
 	"strings"
+	"text/tabwriter"
 	"time"
 
 	metadata "github.com/checkpoint-restore/checkpointctl/lib"
@@ -82,3 +84,52 @@ func CleanupTasks(tasks []Task) {
 		}
 	}
 }
+
+// Constant values are based on kubectl's tabwriter settings:
+// https://github.com/kubernetes/cli-runtime/blob/master/pkg/printers/tabwriter.go
+const (
+	tabwriterMinWidth = 6
+	tabwriterWidth    = 4
+	tabwriterPadding  = 3
+	tabwriterPadChar  = ' '
+	tabwriterFlags    = 0
+)
+
+// GetNewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text.
+func GetNewTabWriter(output io.Writer) *tabwriter.Writer {
+	return tabwriter.NewWriter(output, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)
+}
+
+// WriteTableHeader writes the header row and separator line for a table
+func WriteTableHeader(w *tabwriter.Writer, header []string) {
+	// Print header
+	for i, h := range header {
+		if i > 0 {
+			fmt.Fprint(w, "\t")
+		}
+		fmt.Fprint(w, strings.ToUpper(h))
+	}
+	fmt.Fprintln(w)
+
+	// Print separator line
+	for i := range header {
+		if i > 0 {
+			fmt.Fprint(w, "\t")
+		}
+		fmt.Fprint(w, strings.Repeat("-", len(header[i])))
+	}
+	fmt.Fprintln(w)
+}
+
+// WriteTableRows writes the data rows for a table
+func WriteTableRows(w *tabwriter.Writer, rows [][]string) {
+	for _, row := range rows {
+		for i, cell := range row {
+			if i > 0 {
+				fmt.Fprint(w, "\t")
+			}
+			fmt.Fprint(w, cell)
+		}
+		fmt.Fprintln(w)
+	}
+}
diff --git a/test/checkpointctl.bats b/test/checkpointctl.bats
index 9f3fe15..ca43641 100644
--- a/test/checkpointctl.bats
+++ b/test/checkpointctl.bats
@@ -92,7 +92,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl show "$TEST_TMP_DIR2"/test.tar
 	[ "$status" -eq 0 ]
-	[[ ${lines[4]} == *"Podman"* ]]
+	[[ ${lines[3]} == *"Podman"* ]]
 }
 
 @test "Run checkpointctl show with tar file from containerd with valid config.dump and valid spec.dump and checkpoint directory" {
@@ -102,7 +102,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl show "$TEST_TMP_DIR2"/test.tar
 	[ "$status" -eq 0 ]
-	[[ ${lines[4]} == *"containerd"* ]]
+	[[ ${lines[3]} == *"containerd"* ]]
 }
 
 @test "Run checkpointctl show with tar file with valid config.dump and valid spec.dump (CRI-O) and no checkpoint directory" {
@@ -121,7 +121,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl show "$TEST_TMP_DIR2"/test.tar
 	[ "$status" -eq 0 ]
-	[[ ${lines[4]} == *"CRI-O"* ]]
+	[[ ${lines[3]} == *"CRI-O"* ]]
 }
 
 @test "Run checkpointctl show with tar file compressed" {
@@ -131,7 +131,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar czf "$TEST_TMP_DIR2"/test.tar.gz . )
 	checkpointctl show "$TEST_TMP_DIR2"/test.tar.gz
 	[ "$status" -eq 0 ]
-	[[ ${lines[4]} == *"Podman"* ]]
+	[[ ${lines[3]} == *"Podman"* ]]
 }
 
 @test "Run checkpointctl show with tar file corrupted" {
@@ -165,7 +165,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl show "$TEST_TMP_DIR2"/test.tar
 	[ "$status" -eq 0 ]
-	[[ ${lines[2]} == *"ROOT FS DIFF SIZE"* ]]
+	[[ ${lines[1]} == *"ROOT FS DIFF SIZE"* ]]
 }
 
 @test "Run checkpointctl show with multiple tar files" {
@@ -175,8 +175,8 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test1.tar .  && tar cf "$TEST_TMP_DIR2"/test2.tar . )
 	checkpointctl show "$TEST_TMP_DIR2"/*.tar
 	[ "$status" -eq 0 ]
+	[[ ${lines[2]} == *"Podman"* ]]
 	[[ ${lines[3]} == *"Podman"* ]]
-	[[ ${lines[5]} == *"Podman"* ]]
 }
 
 @test "Run checkpointctl inspect with invalid format" {
@@ -546,7 +546,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl memparse "$TEST_TMP_DIR2"/test.tar
 	[ "$status" -eq 0 ]
-	[[ ${lines[4]} == *"piggie"* ]]
+	[[ ${lines[3]} == *"piggie"* ]]
 }
 
 @test "Run checkpointctl memparse with tar file and missing pstree.img" {
@@ -644,7 +644,7 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
 	checkpointctl memparse --search-regex='HOME=([^?]+)' "$TEST_TMP_DIR2"/test.tar --pid=1
 	[ "$status" -eq 0 ]
-	[[ ${lines[3]} == *"HOME"* ]]
+	[[ ${lines[2]} == *"HOME"* ]]
 }
 
 @test "Run checkpointctl memparse with tar file and invalid PID" {
@@ -763,8 +763,14 @@ function teardown() {
 	( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/checkpoint-valid-config-modified.tar . )
 	checkpointctl list "$TEST_TMP_DIR2"
 	[ "$status" -eq 0 ]
-	[[ "${lines[4]}" == *"| default   | modified-pod-name | container-name | CRI-O  |"* ]]
-	[[ "${lines[4]}" == *"| checkpoint-valid-config-modified.tar |"* ]]
-	[[ "${lines[6]}" == *"| default   | pod-name          | container-name | CRI-O  |"* ]]
-	[[ "${lines[6]}" == *"| checkpoint-valid-config.tar          |"* ]]
+	[[ "${lines[3]}" == *"default"* ]]
+	[[ "${lines[3]}" == *"modified-pod-name"* ]]
+	[[ "${lines[3]}" == *"container-name"* ]]
+	[[ "${lines[3]}" == *"CRI-O"* ]]
+	[[ "${lines[3]}" == *"checkpoint-valid-config-modified.tar"* ]]
+	[[ "${lines[4]}" == *"default"* ]]
+	[[ "${lines[4]}" == *"pod-name"* ]]
+	[[ "${lines[4]}" == *"container-name"* ]]
+	[[ "${lines[4]}" == *"CRI-O"* ]]
+	[[ "${lines[4]}" == *"checkpoint-valid-config.tar"* ]]
 }