From 3547a4b45b013300accce5be1d975d4982f4aa22 Mon Sep 17 00:00:00 2001 From: Boaz Shuster Date: Mon, 9 Oct 2017 11:21:00 +0300 Subject: [PATCH] Refactor plugin store to reduce nested if's in Get This patch removes the nested if's in the Get function and makes the code more readable. Signed-off-by: Boaz Shuster Upstream-commit: 36ebf9489c3c6c8422d8ff9bb6e2cb65a9a66698 Component: engine --- components/engine/plugin/store.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/components/engine/plugin/store.go b/components/engine/plugin/store.go index adc0e26503..8349f34f1c 100644 --- a/components/engine/plugin/store.go +++ b/components/engine/plugin/store.go @@ -111,14 +111,9 @@ func (ps *Store) Remove(p *v2.Plugin) { // Get returns an enabled plugin matching the given name and capability. func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlugin, error) { - var ( - p *v2.Plugin - err error - ) - // Lookup using new model. if ps != nil { - p, err = ps.GetV2Plugin(name) + p, err := ps.GetV2Plugin(name) if err == nil { p.AddRefCount(mode) if p.IsEnabled() { @@ -133,19 +128,18 @@ func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlug } } - // Lookup using legacy model. - if allowV1PluginsFallback { - p, err := plugins.Get(name, capability) - if err != nil { - if errors.Cause(err) == plugins.ErrNotFound { - return nil, errNotFound(name) - } - return nil, errors.Wrap(systemError{err}, "legacy plugin") - } - return p, nil + if !allowV1PluginsFallback { + return nil, errNotFound(name) } - return nil, err + p, err := plugins.Get(name, capability) + if err == nil { + return p, nil + } + if errors.Cause(err) == plugins.ErrNotFound { + return nil, errNotFound(name) + } + return nil, errors.Wrap(systemError{err}, "legacy plugin") } // GetAllManagedPluginsByCap returns a list of managed plugins matching the given capability.