Skip to content

Commit

Permalink
plugin: retrieve non-pointer interface before casting into plugin Int…
Browse files Browse the repository at this point in the history
…erface

Fixes #149

Signed-off-by: Romain Beuque <[email protected]>
  • Loading branch information
rbeuque74 committed May 26, 2020
1 parent b3dc0c9 commit c4c2ac0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"plugin"
"reflect"
"strings"

"github.com/ovh/configstore"
Expand Down Expand Up @@ -53,10 +54,15 @@ type InitializerPlugin interface {
// InitializersFromFolder loads initialization plugins compiled as .so files
// from a folder, runs them on a received pointer to a Service
func InitializersFromFolder(path string, service *Service) error {
return loadPlugins(path, func(fileName string, p plugin.Symbol) error {
plug, ok := p.(InitializerPlugin)
return loadPlugins(path, func(fileName string, pluginSymbol plugin.Symbol) error {
reflectvalue := reflect.ValueOf(pluginSymbol)
if reflectvalue.Kind() != reflect.Ptr {
return fmt.Errorf("failed to load Plugin from %s: received a non-pointer object", fileName)
}
pluginInterface := reflectvalue.Elem().Interface()
plug, ok := pluginInterface.(InitializerPlugin)
if !ok {
return fmt.Errorf("failed to assert type of plugin '%s': expected InitializerPlugin got %T", fileName, p)
return fmt.Errorf("failed to assert type of plugin '%s': expected InitializerPlugin got %T", fileName, pluginInterface)
}
if err := plug.Init(service); err != nil {
return fmt.Errorf("failed to run initialization plugin: %s", err)
Expand Down

0 comments on commit c4c2ac0

Please sign in to comment.