Skip to content

cli/command/formatter: assorted fixes and cleanups#6875

Open
thaJeztah wants to merge 4 commits intodocker:masterfrom
thaJeztah:optimize_formatter
Open

cli/command/formatter: assorted fixes and cleanups#6875
thaJeztah wants to merge 4 commits intodocker:masterfrom
thaJeztah:optimize_formatter

Conversation

@thaJeztah
Copy link
Member

cli/command/formatter: optimize ContainerContext.Names

Optimize formatting of container name(s);

  • Inline StripNamePrefix in the loop, so that we don't have to
    construct a new slice with names (in most cases only to pick
    the first one).
  • Don't use strings.Split, as it allocates a new slice and we only
    used it to check if the container-name was a legacy-link (contained
    slashes).
  • Use a string-builder to concatenate names when not truncating instead
    of using an intermediate slice (and strings.Join).

cli/command/formatter: Context.postFormat: remove redundant buffer

A tabwriter is backed by a buffer already, because it needs to re-flow columns
based on content written to it. This buffer was added in moby@ea61dac9e6 as
part of a new feature to allow for custom delimiters; neither the patch, nor
code-review on the PR mention the extra buffer, so it likely was just overlooked.

This patch;

  • removes the redundant buffer
  • adds an early return for cases where no tabwriter is used.

cli/command/formatter: add Format.templateString, remove Context.preFormat

The Context.preFormat method normalizes the Format as given by the user,
and handles (e.g.) stripping the "table" prefix and replacing the "json"
format for the actual format ({{json .}}).

The method used a finalFormat field on the Context as intermediate,
and was required to be called before executing the format.

This patch adds a Format.templateString() method that returns the
parsed format instead of storing it on the Context. It is currently
not exported, but something we could consider in future.

cli/command/formatter: NewStats: update GoDoc and add TODO

Update the GoDoc to better align with the actual implementation. The
"idOrName" is used for fuzzy-matching the container, which can result
in multiple stats for the same container:

docker ps --format 'table {{.ID}}\t{{.Names}}'
CONTAINER ID   NAMES
b49e6c21d12e   quizzical_maxwell

docker stats --no-stream quizzical_maxwell b49e6c21d12e b49e6
CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28
b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28
b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28

We should resolve the canonical ID once, then use that as reference
to prevent duplicates. Various parts in the code compare Container
against "ID" only (not considering "name" or "ID-prefix").

- Human readable description for the release notes

- A picture of a cute animal (not mandatory but encouraged)

Optimize formatting of container name(s);

- Inline `StripNamePrefix` in the loop, so that we don't have to
  construct a new slice with names (in most cases only to pick
  the first one).
- Don't use `strings.Split`, as it allocates a new slice and we only
  used it to check if the container-name was a legacy-link (contained
  slashes).
- Use a string-builder to concatenate names when not truncating instead
  of using an intermediate slice (and `strings.Join`).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
A tabwriter is backed by a buffer already, because it needs to re-flow columns
based on content written to it. This buffer was added in [moby@ea61dac9e6] as
part of a new feature to allow for custom delimiters; neither the patch, nor
code-review on the PR mention the extra buffer, so it likely was just overlooked.

This patch;

- removes the redundant buffer
- adds an early return for cases where no tabwriter is used.

[moby@ea61dac9e6]: moby/moby@ea61dac

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
…ormat

The `Context.preFormat` method normalizes the Format as given by the user,
and handles (e.g.) stripping the "table" prefix and replacing the "json"
format for the actual format (`{{json .}}`).

The method used a `finalFormat` field on the Context as intermediate,
and was required to be called before executing the format.

This patch adds a `Format.templateString()` method that returns the
parsed format instead of storing it on the Context. It is currently
not exported, but something we could consider in future.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Update the GoDoc to better align with the actual implementation. The
"idOrName" is used for fuzzy-matching the container, which can result
in multiple stats for the same container:

    docker ps --format 'table {{.ID}}\t{{.Names}}'
    CONTAINER ID   NAMES
    b49e6c21d12e   quizzical_maxwell

    docker stats --no-stream quizzical_maxwell b49e6c21d12e b49e6
    CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
    b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28
    b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28
    b49e6c21d12e   quizzical_maxwell   0.10%     140.8MiB / 7.653GiB   1.80%     3.11MB / 13.4kB   115MB / 1.12MB   28

We should resolve the canonical ID once, then use that as reference
to prevent duplicates. Various  parts in the code compare Container
against "ID" only (not considering "name" or "ID-prefix").

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah added this to the 29.3.1 milestone Mar 20, 2026
@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Mar 20, 2026
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 81.08108% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cli/command/formatter/container.go 72.72% 2 Missing and 1 partial ⚠️
cli/command/container/formatter_stats.go 0.00% 2 Missing ⚠️
cli/command/formatter/formatter.go 91.66% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants