From 046178e55f72ed9db7cf5898d3be91b0112ab94f Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Tue, 27 Aug 2019 15:25:54 -0400 Subject: Add function for looking up volumes by partial name This isn't included in Docker, but seems handy enough. Use the new API for 'volume rm' and 'volume inspect'. Fixes #3891 Signed-off-by: Matthew Heon --- cmd/podman/shared/volumes_shared.go | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cmd/podman/shared/volumes_shared.go (limited to 'cmd/podman/shared') diff --git a/cmd/podman/shared/volumes_shared.go b/cmd/podman/shared/volumes_shared.go new file mode 100644 index 000000000..912615cad --- /dev/null +++ b/cmd/podman/shared/volumes_shared.go @@ -0,0 +1,47 @@ +package shared + +import ( + "context" + + "github.com/containers/libpod/libpod" +) + +// Remove given set of volumes +func SharedRemoveVolumes(ctx context.Context, runtime *libpod.Runtime, vols []string, all, force bool) ([]string, map[string]error, error) { + var ( + toRemove []*libpod.Volume + success []string + failed map[string]error + ) + + failed = make(map[string]error) + + if all { + vols, err := runtime.Volumes() + if err != nil { + return nil, nil, err + } + toRemove = vols + } else { + for _, v := range vols { + vol, err := runtime.LookupVolume(v) + if err != nil { + failed[v] = err + continue + } + toRemove = append(toRemove, vol) + } + } + + // We could parallelize this, but I haven't heard anyone complain about + // performance here yet, so hold off. + for _, vol := range toRemove { + if err := runtime.RemoveVolume(ctx, vol, force); err != nil { + failed[vol.Name()] = err + continue + } + success = append(success, vol.Name()) + } + + return success, failed, nil +} -- cgit v1.2.3-54-g00ecf