diff --git a/main.go b/main.go index c8c3763..c50a672 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,10 @@ package main import ( "fmt" - "github.com/josephbmanley/OpenSkins-Common/datastore" "github.com/josephbmanley/OpenSkins/pluginmanager" log "github.com/sirupsen/logrus" "os" - pas "plugin" + "plugin" ) const plugindirectory = "./plugins" @@ -18,33 +17,24 @@ func main() { log.Warningln(fmt.Sprintf("Failed to read plugins directory: %v", err.Error())) } + loadedPlugins := []*plugin.Plugin{} + for _, file := range pluginFiles { log.Infoln(fmt.Sprintf("Loading plugin: %v", file)) - plugin, err := pas.Open(file) + plugin, err := plugin.Open(file) if err != nil { log.Fatalln(fmt.Sprintf("Failed to load plugin '%v': %v", file, err.Error())) os.Exit(1) } - symSkinstore, err := plugin.Lookup("SkinstoreModule") - if err != nil { - log.Fatalln(fmt.Sprintf("Failed to load plugin '%v': %v", file, err.Error())) - os.Exit(1) - } - - var skinstore datastore.Skinstore - skinstore, ok := symSkinstore.(datastore.Skinstore) - if !ok { - log.Fatalln(fmt.Sprintf("Invalid type for Skinstore in plugin '%v'", file)) - os.Exit(1) - } - - err = skinstore.Initialize() - if err != nil { - log.Fatalln(fmt.Sprintf("Failed to intialize Skinstore in plugin '%v'", file)) - os.Exit(1) - } + loadedPlugins = append(loadedPlugins, plugin) } + err = pluginmanager.LoadSkinstores(loadedPlugins) + if err != nil { + log.Fatalln(fmt.Sprintf("Failed to load skinstores '%v'", err.Error())) + os.Exit(1) + } + } diff --git a/pluginmanager/skinstore.go b/pluginmanager/skinstore.go new file mode 100644 index 0000000..1461d2a --- /dev/null +++ b/pluginmanager/skinstore.go @@ -0,0 +1,39 @@ +package pluginmanager + +import ( + "errors" + "fmt" + "github.com/josephbmanley/OpenSkins-Common/datastore" + log "github.com/sirupsen/logrus" + "plugin" +) + +// LoadedSkinstores is a list of all loaded skinstores +var LoadedSkinstores []datastore.Skinstore = []datastore.Skinstore{} + +// LoadSkinstores searches plugins for a skinstore object +// and adds it to the 'skinstores` array +func LoadSkinstores(plugins []*plugin.Plugin) error { + + for _, plugin := range plugins { + + symSkinstore, err := plugin.Lookup("SkinstoreModule") + if err != nil { + log.Fatalln(fmt.Sprintf("Failed to load plugin: %v", err.Error())) + } + + var skinstore datastore.Skinstore + skinstore, ok := symSkinstore.(datastore.Skinstore) + if !ok { + return errors.New("Invalid type for Skinstore in plugin") + } + + err = skinstore.Initialize() + if err != nil { + return err + } + + LoadedSkinstores = append(LoadedSkinstores, skinstore) + } + return nil +}