From 6c10abb24776efb695253e6756109a44490480f7 Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Wed, 21 Nov 2018 01:45:55 +0000 Subject: [PATCH] prune: move image pruning before build cache pruning This is cleaner because running system prune twice in a row now results in a no-op the second time. Signed-off-by: Tibor Vass --- cli/command/image/prune.go | 21 ++++++++++++++++++++- cli/command/system/prune.go | 3 +-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cli/command/image/prune.go b/cli/command/image/prune.go index 0c79d6699c..5672358e29 100644 --- a/cli/command/image/prune.go +++ b/cli/command/image/prune.go @@ -8,7 +8,9 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/opts" + "github.com/docker/docker/api/types/filters" units "github.com/docker/go-units" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -55,8 +57,25 @@ Are you sure you want to continue?` Are you sure you want to continue?` ) +// cloneFilter is a temporary workaround that uses existing public APIs from the filters package to clone a filter. +// TODO(tiborvass): remove this once filters.Args.Clone() is added. +func cloneFilter(args filters.Args) (newArgs filters.Args, err error) { + if args.Len() == 0 { + return filters.NewArgs(), nil + } + b, err := args.MarshalJSON() + if err != nil { + return newArgs, err + } + err = newArgs.UnmarshalJSON(b) + return newArgs, err +} + func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) { - pruneFilters := options.filter.Value() + pruneFilters, err := cloneFilter(options.filter.Value()) + if err != nil { + return 0, "", errors.Wrap(err, "could not copy filter in image prune") + } pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all)) pruneFilters = command.PruneFilters(dockerCli, pruneFilters) diff --git a/cli/command/system/prune.go b/cli/command/system/prune.go index 3cb9f8b54b..3f0015545e 100644 --- a/cli/command/system/prune.go +++ b/cli/command/system/prune.go @@ -73,11 +73,10 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error { if options.pruneVolumes { pruneFuncs = append(pruneFuncs, volume.RunPrune) } + pruneFuncs = append(pruneFuncs, image.RunPrune) if options.pruneBuildCache { pruneFuncs = append(pruneFuncs, builder.CachePrune) } - // FIXME: modify image.RunPrune to not modify options.filter, otherwise this has to be last in the list. - pruneFuncs = append(pruneFuncs, image.RunPrune) var spaceReclaimed uint64 for _, pruneFn := range pruneFuncs {