From 99594330b63a1b5fed6e1b9bdfcb1f37cc63a8db Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 25 Dec 2020 22:16:14 -0500 Subject: [PATCH] Load and intialize skinstore plugins --- .gitignore | 16 +++++++++++++ go.mod | 8 +++++++ go.sum | 9 ++++++++ main.go | 50 ++++++++++++++++++++++++++++++++++++++++ pluginmanager/helpers.go | 30 ++++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pluginmanager/helpers.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d475c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +plugins/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..80ffe5c --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/josephbmanley/OpenSkins + +go 1.13 + +require ( + github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a + github.com/sirupsen/logrus v1.7.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..07a27c4 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a h1:bW/ahOovqRP2r1Ynp4WXxrOCiHq1vqye8RFyYLIZ6hg= +github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c8c3763 --- /dev/null +++ b/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "github.com/josephbmanley/OpenSkins-Common/datastore" + "github.com/josephbmanley/OpenSkins/pluginmanager" + log "github.com/sirupsen/logrus" + "os" + pas "plugin" +) + +const plugindirectory = "./plugins" + +func main() { + + pluginFiles, err := pluginmanager.GetPlugins(plugindirectory) + if err != nil { + log.Warningln(fmt.Sprintf("Failed to read plugins directory: %v", err.Error())) + } + + for _, file := range pluginFiles { + log.Infoln(fmt.Sprintf("Loading plugin: %v", file)) + plugin, err := pas.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) + } + + } + +} diff --git a/pluginmanager/helpers.go b/pluginmanager/helpers.go new file mode 100644 index 0000000..91aeecc --- /dev/null +++ b/pluginmanager/helpers.go @@ -0,0 +1,30 @@ +package pluginmanager + +import ( + "os" + "path/filepath" +) + +// GetPlugins gets plugin files in directory +func GetPlugins(pluginDir string) ([]string, error) { + pattern := "*.so" + var matches []string + err := filepath.Walk(pluginDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil { + return err + } else if matched { + matches = append(matches, path) + } + return nil + }) + if err != nil { + return nil, err + } + return matches, nil +}