diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-02-02 14:02:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 14:02:58 -0500 |
commit | 628b0d79b523953cc6aa9b48bd91d04d0843353b (patch) | |
tree | 2d30d6751ad78c9450f34f8ffde82a1b16ca171e /cmd/podman | |
parent | d66a18cb11688060a3ef737dd05758398279f053 (diff) | |
parent | 9b5b03d1e03b20a80c0a0c92866136d4243e5f4a (diff) | |
download | podman-628b0d79b523953cc6aa9b48bd91d04d0843353b.tar.gz podman-628b0d79b523953cc6aa9b48bd91d04d0843353b.tar.bz2 podman-628b0d79b523953cc6aa9b48bd91d04d0843353b.zip |
Merge pull request #9116 from iwita/ls-volumes-before-pruning
List volumes before pruning
Diffstat (limited to 'cmd/podman')
-rw-r--r-- | cmd/podman/volumes/prune.go | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/cmd/podman/volumes/prune.go b/cmd/podman/volumes/prune.go index 0f3ba9ef6..39ad2735b 100644 --- a/cmd/podman/volumes/prune.go +++ b/cmd/podman/volumes/prune.go @@ -49,16 +49,46 @@ func init() { func prune(cmd *cobra.Command, args []string) error { var ( - pruneOptions = entities.VolumePruneOptions{} + pruneOptions = entities.VolumePruneOptions{} + listOptions = entities.VolumeListOptions{} + unusedOptions = entities.VolumeListOptions{} ) // Prompt for confirmation if --force is not set force, err := cmd.Flags().GetBool("force") if err != nil { return err } + pruneOptions.Filters, err = filters.ParseFilterArgumentsIntoFilters(filter) if !force { reader := bufio.NewReader(os.Stdin) - fmt.Println("WARNING! This will remove all volumes not used by at least one container.") + fmt.Println("WARNING! This will remove all volumes not used by at least one container. The following volumes will be removed:") + if err != nil { + return err + } + listOptions.Filter, err = filters.ParseFilterArgumentsIntoFilters(filter) + if err != nil { + return err + } + // filter all the dangling volumes + unusedOptions.Filter = make(map[string][]string, 1) + unusedOptions.Filter["dangling"] = []string{"true"} + unusedVolumes, err := registry.ContainerEngine().VolumeList(context.Background(), unusedOptions) + if err != nil { + return err + } + // filter volumes based on user input + filteredVolumes, err := registry.ContainerEngine().VolumeList(context.Background(), listOptions) + if err != nil { + return err + } + finalVolumes := getIntersection(unusedVolumes, filteredVolumes) + if len(finalVolumes) < 1 { + fmt.Println("No dangling volumes found") + return nil + } + for _, fv := range finalVolumes { + fmt.Println(fv.Name) + } fmt.Print("Are you sure you want to continue? [y/N] ") answer, err := reader.ReadString('\n') if err != nil { @@ -68,13 +98,23 @@ func prune(cmd *cobra.Command, args []string) error { return nil } } - pruneOptions.Filters, err = filters.ParseFilterArgumentsIntoFilters(filter) - if err != nil { - return err - } responses, err := registry.ContainerEngine().VolumePrune(context.Background(), pruneOptions) if err != nil { return err } return utils.PrintVolumePruneResults(responses, false) } + +func getIntersection(a, b []*entities.VolumeListReport) []*entities.VolumeListReport { + var intersection []*entities.VolumeListReport + hash := make(map[string]bool, len(a)) + for _, aa := range a { + hash[aa.Name] = true + } + for _, bb := range b { + if hash[bb.Name] { + intersection = append(intersection, bb) + } + } + return intersection +} |