cli/command/formatter: assorted fixes and cleanups#6875
cli/command/formatter: assorted fixes and cleanups#6875vvoland merged 4 commits intodocker:masterfrom
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR refactors and optimizes parts of the CLI formatter layer, focusing on reducing allocations in container name formatting and simplifying template preprocessing / output buffering behavior.
Changes:
- Adds
Format.templateString()to normalize--formatinputs (table/json prefixes, whitespace trimming, escape handling) and removesContext.preFormat()/finalFormat. - Simplifies
Context.postFormat()by removing an extra intermediate buffer and adding an early return when not using the tabwriter. - Optimizes
ContainerContext.Names()to avoid intermediate slices/splits and reduce allocations; updatesNewStatsdoc/comment to better reflect fuzzy matching behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| cli/command/formatter/formatter.go | Introduces Format.templateString(), removes Context.preFormat(), simplifies post-formatting output flow. |
| cli/command/formatter/formatter_test.go | Expands Format tests to cover templateString() normalization and edge cases. |
| cli/command/formatter/disk_usage.go | Removes preFormat() calls to align with the new templateString()-based parsing. |
| cli/command/formatter/container.go | Optimizes container name formatting logic and clarifies truncation behavior in docs. |
| cli/command/container/formatter_stats.go | Updates NewStats signature/docs and adds a FIXME about fuzzy matching duplicates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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>
b0d95d1 to
b309524
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cli/command/formatter: optimize ContainerContext.Names
Optimize formatting of container name(s);
StripNamePrefixin the loop, so that we don't have toconstruct a new slice with names (in most cases only to pick
the first one).
strings.Split, as it allocates a new slice and we onlyused it to check if the container-name was a legacy-link (contained
slashes).
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;
cli/command/formatter: add Format.templateString, remove Context.preFormat
The
Context.preFormatmethod 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
finalFormatfield on the Context as intermediate,and was required to be called before executing the format.
This patch adds a
Format.templateString()method that returns theparsed 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:
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)