Skip to content

refactor: code deduplication and simplification#13759

Open
ndeloof wants to merge 9 commits intodocker:mainfrom
ndeloof:cleanup/code-dedup
Open

refactor: code deduplication and simplification#13759
ndeloof wants to merge 9 commits intodocker:mainfrom
ndeloof:cleanup/code-dedup

Conversation

@ndeloof
Copy link
Copy Markdown
Contributor

@ndeloof ndeloof commented Apr 27, 2026

Pure refactoring — no functional changes. All existing tests pass.

Changes:

  • Extract withBackend() helper in cmd/compose/ to replace repetitive NewComposeService → backend.Foo boilerplate in pause, unpause, kill, start, stop, restart
  • Extract optionalTimeout() helper to deduplicate timeout-pointer construction in stop/restart
  • Collapse ~18 trivial event-helper one-liners in progress.go to newEvent() calls; keep only functions used ≥3 times or as function values
  • Remove redundant package-level wrapper functions in dependencies.go (leaves, roots, getChildren, filterChildren, filterParents) — replaced with method expressions; replace remove(slice, item) with slices.DeleteFunc
  • Extract removeResource() helper in down.go to deduplicate the remove-emit pattern shared by removeImage and removeVolume
  • Extract forEachContainerConcurrent() in containers.go to deduplicate the errgroup iteration pattern in pause, unpause, kill
  • Collapse three identical blocks in resolveSharedNamespaces (NetworkMode/Ipc/Pid) into a resolve closure
  • Inline containers.forEach (only 1 remaining call site); add mutation comment to sorted(); replace hasConfigHashLabel() with api.ConfigHashLabel directly; add labelFilter() factory for label filters; remove unused Set.Clear and Set.Union

Copilot AI review requested due to automatic review settings April 27, 2026 11:32
@ndeloof ndeloof requested a review from a team as a code owner April 27, 2026 11:32
@ndeloof ndeloof requested a review from glours April 27, 2026 11:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors Compose CLI/service code to reduce duplication by extracting shared helpers and inlining/removing trivial wrappers, while keeping behavior the same.

Changes:

  • Added shared helpers in cmd/compose/ (withBackend, optionalTimeout) and pkg/compose/ (forEachContainerConcurrent, removeResource, labelFilter) to deduplicate common patterns.
  • Simplified progress/event emission by collapsing many one-off event helper functions into direct newEvent(...) calls.
  • Removed unused utilities/wrappers (graph traversal wrapper fns; utils.Set methods/tests) and updated call sites/tests accordingly.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/utils/set_test.go Removes the Union unit test and its now-unused import.
pkg/utils/set.go Removes unused Set methods and repositions RemoveAll.
pkg/compose/restart.go Switches to newEvent(...) for restart progress events.
pkg/compose/remove.go Simplifies iteration over stopped containers; adds cyclo suppression annotation.
pkg/compose/pull.go Replaces pull progress helpers with newEvent(...) calls.
pkg/compose/ps_test.go Updates label filtering to use api.ConfigHashLabel directly.
pkg/compose/progress.go Collapses many trivial event helper wrappers into newEvent(...); keeps function-value helpers.
pkg/compose/pause.go Uses forEachContainerConcurrent to deduplicate pause/unpause concurrency pattern.
pkg/compose/monitor.go Uses api.ConfigHashLabel directly (removes wrapper indirection).
pkg/compose/logs_test.go Updates label filtering to use api.ConfigHashLabel directly.
pkg/compose/kill_test.go Updates label filtering to use api.ConfigHashLabel directly.
pkg/compose/kill.go Uses forEachContainerConcurrent and newEvent(...) for kill progress events.
pkg/compose/filters.go Introduces labelFilter(...) and removes redundant hasConfigHashLabel().
pkg/compose/down.go Extracts removeResource(...) helper and switches stop events to newEvent(...).
pkg/compose/dependencies.go Removes redundant wrapper functions via method expressions; replaces custom slice remove with slices.DeleteFunc.
pkg/compose/convergence_test.go Updates label filtering to use api.ConfigHashLabel directly.
pkg/compose/convergence.go Replaces several progress helpers with newEvent(...); refactors shared-namespace resolution via closure.
pkg/compose/containers.go Adds forEachContainerConcurrent; documents sorted() as in-place mutation; uses api.ConfigHashLabel directly.
cmd/compose/stop.go Uses withBackend(...) and optionalTimeout(...) to deduplicate backend/timeout boilerplate.
cmd/compose/start.go Uses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/restart.go Uses withBackend(...) and optionalTimeout(...) to deduplicate backend/timeout boilerplate.
cmd/compose/pause.go Uses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/kill.go Uses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/backend.go Adds new shared backend/timeout helper functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/utils/set.go
Comment on lines +54 to 58
func (s Set[T]) RemoveAll(elements ...T) {
for _, e := range elements {
s.Remove(e)
}
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

Set is an exported type, and this change removes exported methods (Clear, Union). Even if they’re unused internally, this is a compile-time breaking API change for any downstream consumers importing pkg/utils. Consider keeping these methods as thin wrappers (potentially marked deprecated) or moving Set to an internal/ package if it’s not meant to be part of the public API.

Copilot uses AI. Check for mistakes.
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.

Restored. Both Clear and Union are back as thin wrappers with // Deprecated: godoc comments, preserving API compatibility for any downstream consumers. Fixed in the latest commit.

Comment thread pkg/compose/pull.go Outdated
Comment on lines +177 to +178
resource := "Image " + service.Image
s.events.On(pullingEvent(service.Image))
s.events.On(newEvent("Image "+service.Image, api.Working, api.StatusPulling))
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

resource is computed as "Image "+service.Image, but the progress events re-create the same string literal. Using the resource variable for the newEvent calls would avoid duplication and guarantees the event IDs stay consistent if the formatting ever changes.

Copilot uses AI. Check for mistakes.
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.

Fixed — the newEvent calls now use the already-computed resource variable instead of re-constructing the string literal. Fixed in the latest commit.

ndeloof added 8 commits April 27, 2026 11:39
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
…pause/kill logic

Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
@ndeloof ndeloof force-pushed the cleanup/code-dedup branch from 63752ea to de5e11d Compare April 27, 2026 11:39
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants