From 80e8fda14f27e5b0ed9038bc096bf31f4796157a Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Tue, 17 Dec 2024 17:41:45 +0100 Subject: [PATCH] compose top: ensure CMD is right-most column Signed-off-by: Dominik Menke --- cmd/compose/top.go | 15 +++++++++++++++ cmd/compose/top_test.go | 16 ++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/compose/top.go b/cmd/compose/top.go index 14f9d0054..5a9d854ef 100644 --- a/cmd/compose/top.go +++ b/cmd/compose/top.go @@ -101,6 +101,21 @@ func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) entries = append(entries, entry) } } + + // ensure CMD is the right-most column + if pos, ok := header["CMD"]; ok { + max := pos + for h, i := range header { + if i > max { + max = i + } + if i > pos { + header[h] = i - 1 + } + } + header["CMD"] = max + } + return header, entries } diff --git a/cmd/compose/top_test.go b/cmd/compose/top_test.go index cc7470f4d..35b5caa2d 100644 --- a/cmd/compose/top_test.go +++ b/cmd/compose/top_test.go @@ -245,8 +245,8 @@ func TestRunTopCore(t *testing.T) { "STIME": 6, "TTY": 7, "TIME": 8, - "CMD": 9, - "GID": 10, + "GID": 9, + "CMD": 10, }, header) assert.EqualValues(t, []topEntries{ { @@ -311,12 +311,12 @@ func TestRunTopCore(t *testing.T) { err := topPrint(&buf, header, entries) require.NoError(t, err) assert.Equal(t, trim(` - SERVICE # UID PID PPID C STIME TTY TIME CMD GID - simple 1 root 1 1 0 12:00 ? 00:00:01 /entrypoint - - noppid 1 root 1 - 0 12:00 ? 00:00:02 /entrypoint - - extra-hdr 1 root 1 1 0 12:00 ? 00:00:03 /entrypoint 1 - multiple 1 root 1 1 0 12:00 ? 00:00:04 /entrypoint - - multiple 1 root 123 1 0 12:00 ? 00:00:42 sleep infinity - + SERVICE # UID PID PPID C STIME TTY TIME GID CMD + simple 1 root 1 1 0 12:00 ? 00:00:01 - /entrypoint + noppid 1 root 1 - 0 12:00 ? 00:00:02 - /entrypoint + extra-hdr 1 root 1 1 0 12:00 ? 00:00:03 1 /entrypoint + multiple 1 root 1 1 0 12:00 ? 00:00:04 - /entrypoint + multiple 1 root 123 1 0 12:00 ? 00:00:42 - sleep infinity `), buf.String()) })