Skip to content

Consolidate CLI formatting infrastructure#1385

Open
realrajaryan wants to merge 2 commits intoapple:mainfrom
realrajaryan:consolidate-cli-formatting
Open

Consolidate CLI formatting infrastructure#1385
realrajaryan wants to merge 2 commits intoapple:mainfrom
realrajaryan:consolidate-cli-formatting

Conversation

@realrajaryan
Copy link
Copy Markdown
Contributor

Motivation and Context

This PR consolidates duplicated list-output formatting across the CLI into shared rendering infrastructure.

Currently, each list command has its own copy of the same json/quiet/table branching. This PR pulls that into shared rendering infrastructure in ContainerCommands:

  • ListDisplayable protocol for table + quiet output
  • renderJSON, renderTable, renderList as pure functions that return strings
  • emit() as the single stdout boundary (no-ops on empty strings to avoid blank-line regressions)
  • JSONOptions so all JSON encoding goes through one path, including volume inspect's pretty + ISO 8601 case

JSON encoding remains separate from display formatting: each command still chooses its own JSON model, while ListDisplayable is used only for table and quiet output. ImageList remains the intentional exception for quiet mode so it can avoid unnecessary async work.

This change also replaces inline JSONEncoder usage with renderJSON, removes the old Codable+JSON.swift helper, and moves TableOutput and ListFormat into ContainerCommands. It also adds unit tests for the shared rendering helpers and expands integration coverage for image, network, and registry list formatting.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

@github-actions github-actions bot added the cli label Apr 3, 2026
@realrajaryan realrajaryan requested a review from jglogan April 3, 2026 21:25
import Foundation

public struct TableOutput {
struct TableOutput {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One consideration here: what public visibility is needed for commands in third-party libraries/plugins that depend on ContainerCommands and want to produce consistent collection output?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I don't mind, we can make it public for third party use cases

.forEach { print($0.id) }
return
}
private struct PrintableBuilder: ListDisplayable {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we can't define a ListDisplayable extension on the bulk of or regular types instead of defining completely separate types that conform to that protocol? It feels like there might be a bit more boilerplate as it currently stands.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants