diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-09-09 21:48:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-09 21:48:30 +0200 |
commit | 9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba (patch) | |
tree | b048b0e2228fa9ae67c9a73bd4fecf1739f64d75 /libpod/in_memory_state.go | |
parent | 7042a3d7a539bae79ed63bdc87f432b8ec73afd8 (diff) | |
parent | 046178e55f72ed9db7cf5898d3be91b0112ab94f (diff) | |
download | podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.tar.gz podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.tar.bz2 podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.zip |
Merge pull request #3896 from mheon/volume_lookup
Add ability to look up volumes by unambiguous partial name
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 == "" { |