ahenk-go/cmd/ahenk-go/plugin-opener.go

38 lines
983 B
Go
Raw Normal View History

2023-08-21 16:45:11 +03:00
package main
import (
"fmt"
"os"
"plugin"
2023-08-21 16:45:11 +03:00
"git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils"
)
2023-08-21 16:45:11 +03:00
// Load Plugin placed in PluginDir, returns empty interface with channel
// Do not forget to cast in plugin manager
//
// Give Plugin Name as argument and be sure you compiled plugins with `-buildmode=plugin` to PluginDir as `pluginname.so`
func LoadPlugin(plugName string, chn chan interface{}) {
2023-08-21 16:45:11 +03:00
// TODO if error caugth try without relative path, this will be good for local testing
plug, err := plugin.Open(PluginDir + plugName + ".so")
utils.Check(err)
// TODO also allow lookup another symbol other than PlugnameConnect
2023-08-24 21:06:05 +03:00
symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect")
utils.Check(err)
var plugOut interface{}
2023-08-24 21:06:05 +03:00
plugOut, ok := symPlug.(interface{})
if !ok {
fmt.Println("unexpected type from module symbol")
os.Exit(1)
}
chn <- plugOut
}
// NEXT implement unload function
// func UnloadPlugin(plugName string) interface{} {
// }