mirror of https://github.com/docker/cli.git
Merge pull request #341 from thaJeztah/small-prune-refactor
Small refactor in system prune
This commit is contained in:
commit
3209cb8da6
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue