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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2017-07-17 12:12:05 +02:00
parent 79e4d635ca
commit 73163ee78b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 17 additions and 19 deletions

View File

@ -77,6 +77,15 @@ func runImagePrune(dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint
return image.RunPrune(dockerCli, all, filter) 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 { func runPrune(dockerCli command.Cli, options pruneOptions) error {
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.31") { if versions.LessThan(dockerCli.Client().ClientVersion(), "1.31") {
options.pruneBuildCache = false 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)) { if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
return nil return nil
} }
imagePrune := func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
var spaceReclaimed uint64 return runImagePrune(dockerCli, options.all, options.filter)
}
pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){ pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
runContainerPrune, runContainerPrune,
runNetworkPrune, runNetworkPrune,
@ -93,7 +103,12 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
if options.pruneVolumes { if options.pruneVolumes {
pruneFuncs = append(pruneFuncs, runVolumePrune) pruneFuncs = append(pruneFuncs, runVolumePrune)
} }
pruneFuncs = append(pruneFuncs, imagePrune)
if options.pruneBuildCache {
pruneFuncs = append(pruneFuncs, runBuildCachePrune)
}
var spaceReclaimed uint64
for _, pruneFn := range pruneFuncs { for _, pruneFn := range pruneFuncs {
spc, output, err := pruneFn(dockerCli, options.filter) spc, output, err := pruneFn(dockerCli, options.filter)
if err != nil { 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))) fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
return nil return nil