From 73163ee78b2944fff9dd83bc69e7f7f211dd6280 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 17 Jul 2017 12:12:05 +0200 Subject: [PATCH] Small refactor in system prune Adapt "image prune" and "build cache prune" to have the same signature as the other prune commands, so that they can all be added to the "pruneFuncs" array. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/prune.go | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/cli/command/system/prune.go b/cli/command/system/prune.go index 9afe0a317c..d542391224 100644 --- a/cli/command/system/prune.go +++ b/cli/command/system/prune.go @@ -77,6 +77,15 @@ func runImagePrune(dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint return image.RunPrune(dockerCli, all, filter) } +// runBuildCachePrune executes a prune command for build cache +func runBuildCachePrune(dockerCli command.Cli, _ opts.FilterOpt) (uint64, string, error) { + report, err := dockerCli.Client().BuildCachePrune(context.Background()) + if err != nil { + return 0, "", err + } + return report.SpaceReclaimed, "", nil +} + func runPrune(dockerCli command.Cli, options pruneOptions) error { if versions.LessThan(dockerCli.Client().ClientVersion(), "1.31") { options.pruneBuildCache = false @@ -84,8 +93,9 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error { if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) { return nil } - - var spaceReclaimed uint64 + imagePrune := func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) { + return runImagePrune(dockerCli, options.all, options.filter) + } pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){ runContainerPrune, runNetworkPrune, @@ -93,7 +103,12 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error { if options.pruneVolumes { pruneFuncs = append(pruneFuncs, runVolumePrune) } + pruneFuncs = append(pruneFuncs, imagePrune) + if options.pruneBuildCache { + pruneFuncs = append(pruneFuncs, runBuildCachePrune) + } + var spaceReclaimed uint64 for _, pruneFn := range pruneFuncs { spc, output, err := pruneFn(dockerCli, options.filter) if err != nil { @@ -105,23 +120,6 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error { } } - spc, output, err := runImagePrune(dockerCli, options.all, options.filter) - if err != nil { - return err - } - if spc > 0 { - spaceReclaimed += spc - fmt.Fprintln(dockerCli.Out(), output) - } - - if options.pruneBuildCache { - report, err := dockerCli.Client().BuildCachePrune(context.Background()) - if err != nil { - return err - } - spaceReclaimed += report.SpaceReclaimed - } - fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed))) return nil