system prune: delegate confirmation message and validation

This adds a "dry-run" / "pre-check" option for prune-functions,
which delegates constructing the confirmation message (what is
about to be pruned) and validation of the given options to the
prune-functions.

This helps separating concerns, and doesn't enforce knowledge
about what's supported by each content-type onto the system
prune command.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-03 22:04:07 +02:00
parent 02d578b637
commit a888c4091c
7 changed files with 125 additions and 39 deletions

View File

@ -100,6 +100,11 @@ func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.Fi
// pruneFn calls the Network Prune API for use in "docker system prune"
// and returns the amount of space reclaimed and a detailed output string.
func pruneFn(ctx context.Context, dockerCLI command.Cli, options pruner.PruneOptions) (uint64, string, error) {
if !options.Confirmed {
// Dry-run: perform validation and produce confirmation before pruning.
confirmMsg := "all networks not used by at least one container"
return 0, confirmMsg, cancelledErr{errors.New("network prune has been cancelled")}
}
output, err := runPrune(ctx, dockerCLI, pruneOptions{
force: true,
filter: options.Filter,