diff options
author | Matthew Heon <matthew.heon@pm.me> | 2019-08-27 15:25:54 -0400 |
---|---|---|
committer | Matthew Heon <matthew.heon@pm.me> | 2019-09-09 12:06:10 -0400 |
commit | 046178e55f72ed9db7cf5898d3be91b0112ab94f (patch) | |
tree | ad844579a48f043d1959233ca881307801f79a45 /libpod/in_memory_state.go | |
parent | 16a70490852fdaf3ea5aeea6b2be19dd70fbf1c7 (diff) | |
download | podman-046178e55f72ed9db7cf5898d3be91b0112ab94f.tar.gz podman-046178e55f72ed9db7cf5898d3be91b0112ab94f.tar.bz2 podman-046178e55f72ed9db7cf5898d3be91b0112ab94f.zip |
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 <matthew.heon@pm.me>
Diffstat (limited to 'libpod/in_memory_state.go')
-rw-r--r-- | libpod/in_memory_state.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go index 280ae5f5c..a008fcb39 100644 --- a/libpod/in_memory_state.go +++ b/libpod/in_memory_state.go @@ -459,6 +459,41 @@ func (s *InMemoryState) Volume(name string) (*Volume, error) { return vol, nil } +// LookupVolume finds a volume from an unambiguous partial ID. +func (s *InMemoryState) LookupVolume(name string) (*Volume, error) { + if name == "" { + return nil, define.ErrEmptyID + } + + vol, ok := s.volumes[name] + if ok { + return vol, nil + } + + // Alright, we've failed to find by full name. Now comes the expensive + // part. + // Loop through all volumes and look for matches. + var ( + foundMatch bool + candidate *Volume + ) + for volName, vol := range s.volumes { + if strings.HasPrefix(volName, name) { + if foundMatch { + return nil, errors.Wrapf(define.ErrVolumeExists, "more than one result for volume name %q", name) + } + candidate = vol + foundMatch = true + } + } + + if !foundMatch { + return nil, errors.Wrapf(define.ErrNoSuchVolume, "no volume with name %q found", name) + } + + return candidate, nil +} + // HasVolume checks if a volume with the given name is present in the state func (s *InMemoryState) HasVolume(name string) (bool, error) { if name == "" { |