implement eh-steve/goloader
This commit is contained in:
parent
6ac65abfa3
commit
3e2a03204b
5 changed files with 59 additions and 88 deletions
|
@ -1,74 +1,51 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"git.aliberksandikci.com.tr/asandikci/go-loader-test/pkg/utils"
|
"github.com/eh-steve/goloader/jit"
|
||||||
)
|
)
|
||||||
|
|
||||||
// General Functions/Methods that each plugin has
|
|
||||||
type PlugGeneral interface {
|
|
||||||
Info() map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// plugins/tmptest
|
|
||||||
type TmpTest interface {
|
|
||||||
TmpTest()
|
|
||||||
}
|
|
||||||
|
|
||||||
// FILLME creating new plugin interface, template
|
|
||||||
// type NewPluginInterface interface {
|
|
||||||
// PluginMethod() returnType
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Loads Plugins and runs them concurrently.
|
|
||||||
// When you create a new plugin create a new interface and call this plugin in this function
|
|
||||||
func PluginManager(params ...string) {
|
func PluginManager(params ...string) {
|
||||||
chanPlug := make(chan interface{})
|
conf := jit.BuildConfig{
|
||||||
|
KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies
|
||||||
if len(params) > 0 && params[0] == "tmptest" {
|
ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command
|
||||||
go LoadPlugin("tmptest", chanPlug)
|
BuildEnv: []string{"GOOS=windows"}, // Env vars to set for go build toolchain
|
||||||
res, ok := <-chanPlug
|
TmpDir: "/tmp/inner", // To control where temporary files are copied
|
||||||
var tmptest TmpTest = res.(TmpTest)
|
DebugLog: true, //
|
||||||
checkPlugin(res, ok)
|
|
||||||
tmptest.TmpTest()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logs plugin outputs.
|
loadable, err := jit.BuildGoPackage(conf, "plugins/tmptest/")
|
||||||
func logPlugin(title string, mp map[string]interface{}, toJson bool) {
|
if err != nil {
|
||||||
log.Printf("\n----- %v -----\n", title)
|
panic(err)
|
||||||
if toJson {
|
|
||||||
data, err := json.MarshalIndent(&mp, "", " ")
|
|
||||||
utils.Check(err)
|
|
||||||
log.Println(string(data))
|
|
||||||
} else {
|
|
||||||
for i, v := range mp {
|
|
||||||
log.Printf("%v: %v\n", i, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks plugin status
|
module, err := loadable.Load()
|
||||||
func checkPlugin(plugVal interface{}, status bool) {
|
// module.SymbolsByPkg is a map[string]map[string]interface{} of all packages and their exported functions and global vars
|
||||||
if status {
|
symbols := module.SymbolsByPkg[loadable.ImportPath]
|
||||||
if plugVal == nil {
|
if err != nil {
|
||||||
log.Fatal("Plugin loaded but there is no value!")
|
panic(err)
|
||||||
} else {
|
|
||||||
plugInfo := plugVal.(PlugGeneral).Info()
|
|
||||||
log.Printf("Plugin \"%v\" loaded and ready to use, version \"%v\" ", plugInfo["name"], plugInfo["version"])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if plugVal == nil {
|
|
||||||
log.Fatal("Plugin closed and there is no value! ")
|
|
||||||
} else {
|
|
||||||
log.Fatal("Plugin closed or there is an error")
|
|
||||||
}
|
}
|
||||||
|
// defer func() {
|
||||||
|
// err = module.Unload()
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
// }()
|
||||||
|
switch f := symbols["MyFunc"].(type) {
|
||||||
|
case func([]byte) (interface{}, error):
|
||||||
|
result, err := f([]byte(`{"k":"v"}`))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println(result)
|
||||||
|
default:
|
||||||
|
fmt.Println(f)
|
||||||
|
panic("Function signature was not what was expected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// // TODO response to Lider
|
err = module.Unload()
|
||||||
// // func createResponse() {
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
// // }
|
}
|
||||||
|
}
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -2,4 +2,9 @@ module git.aliberksandikci.com.tr/asandikci/go-loader-test
|
||||||
|
|
||||||
go 1.21.0
|
go 1.21.0
|
||||||
|
|
||||||
require golang.org/x/text v0.12.0
|
require (
|
||||||
|
github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550
|
||||||
|
golang.org/x/text v0.12.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,2 +1,6 @@
|
||||||
|
github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 h1:0mZ2Y8x4dhPfm9eOlbzBx31YSLaECdPvKEBrL5Hc0YE=
|
||||||
|
github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:k7xs3CUwCvOU9aw851I6AEb6ZzZJ3nos5dZ6A/2ewM0=
|
||||||
|
github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550 h1:clY7T47fFZdFcILcu7uksS7FStRaKMssCYGW0l/AoMo=
|
||||||
|
github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550/go.mod h1:p18VKcPYO8oWGYcQt/K5EGIGqak0ZT5HwVirGpUGZBg=
|
||||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
||||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package main
|
package mypackage
|
||||||
|
|
||||||
func (p plug) Info() map[string]interface{} {
|
func Info() map[string]interface{} {
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"name": "tmptest",
|
"name": "tmptest",
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
|
|
|
@ -1,24 +1,9 @@
|
||||||
package main
|
package mypackage
|
||||||
|
|
||||||
import (
|
import "encoding/json"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type plug string
|
func MyFunc(input []byte) (interface{}, error) {
|
||||||
|
var output interface{}
|
||||||
// exported plugin Symbol
|
err := json.Unmarshal(input, &output)
|
||||||
var TmptestConnect plug
|
return output, err
|
||||||
|
|
||||||
func (p plug) TmpTest() {
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println("----- Entered Test Area -----")
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println("----- End of Test Area -----")
|
|
||||||
fmt.Println()
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue