Move interfaces to maubot package and other stuff to app/

This commit is contained in:
Tulir Asokan 2018-06-14 11:11:24 +03:00
parent ef8fffaff8
commit 3a27831112
14 changed files with 329 additions and 198 deletions

View file

@ -16,66 +16,16 @@
package maubot
import (
"fmt"
"plugin"
"maubot.xyz/database"
"maubot.xyz/interfaces"
)
type PluginWrapper struct {
interfaces.Plugin
Creator *interfaces.PluginCreator
DB *database.Plugin
type Plugin interface {
Start()
Stop()
}
func LoadPlugin(path string) (*interfaces.PluginCreator, error) {
rawPlugin, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open: %v", err)
}
type PluginCreatorFunc func(client MatrixClient) Plugin
pluginCreatorSymbol, err := rawPlugin.Lookup("Plugin")
if err == nil {
pluginCreator, ok := pluginCreatorSymbol.(*interfaces.PluginCreator)
if ok {
pluginCreator.Path = path
return pluginCreator, nil
}
}
pluginCreatorFuncSymbol, err := rawPlugin.Lookup("Create")
if err != nil {
return nil, fmt.Errorf("symbol \"Create\" not found: %v", err)
}
pluginCreatorFunc, ok := pluginCreatorFuncSymbol.(interfaces.PluginCreatorFunc)
if !ok {
return nil, fmt.Errorf("symbol \"Create\" does not implement maubot.PluginCreator")
}
nameSymbol, err := rawPlugin.Lookup("Name")
if err != nil {
return nil, fmt.Errorf("symbol \"Name\" not found: %v", err)
}
name, ok := nameSymbol.(string)
if !ok {
return nil, fmt.Errorf("symbol \"Name\" is not a string")
}
versionSymbol, err := rawPlugin.Lookup("Version")
if err != nil {
return nil, fmt.Errorf("symbol \"Version\" not found: %v", err)
}
version, ok := versionSymbol.(string)
if !ok {
return nil, fmt.Errorf("symbol \"Version\" is not a string")
}
return &interfaces.PluginCreator{
Create: pluginCreatorFunc,
Name: name,
Version: version,
Path: path,
}, nil
type PluginCreator struct {
Create PluginCreatorFunc
Name string
Version string
Path string
}