Load and intialize skinstore plugins
This commit is contained in:
		
							
								
								
									
										16
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -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/
 | 
			
		||||
							
								
								
									
										8
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										9
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@ -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=
 | 
			
		||||
							
								
								
									
										50
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								pluginmanager/helpers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								pluginmanager/helpers.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user