update vendor

This commit is contained in:
Ettore Di Giacinto
2021-10-24 17:45:32 +02:00
parent c220eac061
commit bb40b5d1b7
6 changed files with 168 additions and 11 deletions

View File

@@ -20,9 +20,12 @@ func main() {
},
)
// We have a file 'test-foo' in temp.
// 'test-foo' will receive our event payload in json
m.Autoload("test", temp)
// Load plugins
m.Autoload("test", temp) // Scan for binary plugins with the "test" prefix. E.g. 'test-foo'
m.Plugin = append(m.Plugin, pluggable.Plugin{ Name: "foo" , Executable: "path" }) // manually add a Plugin
m.Load("my-binary", "my-binary-2"...) // Load individually, scanning $PATH
// Register to events and initialize the manager
m.Register()
// Optionally process plugin results response
@@ -41,8 +44,67 @@ func main() {
// Emit events, they are encoded and passed as JSON payloads to the plugins.
// In our case, test-foo will receive the map as JSON
m.Publish(myEv, map[string]string{"foo": "bar"})
}
```
# Plugin processed data
The interface passed to `Publish` gets marshalled in JSON in a event struct of the following form:
```go
type Event struct {
Name EventType `json:"name"`
Data string `json:"data"`
File string `json:"file"`
}
```
An example bash plugin could be, for example:
```bash
#!/bin/bash
event="$1"
payload="$2"
if [ "$event" == "something.to.hook.on" ]; then
custom_data=$(echo "$payload" | jq -r .data | jq -r .foo )
...
fi
```
Which can be called by
```golang
m.Publish(myEv, map[string]string{"foo": "bar"})
```
To note, when the payload exceeds the [threshold size](https://github.com/mudler/go-pluggable/blob/master/plugin.go#L35) the payload published with `Publish` is written into a temporary file and the file location is sent to the plugin with the Event `file` field, so for example, a plugin should expect data in a file if the publisher expects to send big chunk of data:
```bash
#!/bin/bash
data_file="$(echo $2 | jq -r .file)"
if [ -n "${data_file}" ]; then
payload="$(cat $data_file)"
...
fi
```
## Writing plugin in golang
It is present a `FactoryPlugin` which allows to create plugins in golang, consider:
```golang
import "github.com/mudler/go-pluggable"
func main() {
var myEv pluggableEventType = "event"
factory := pluggable.NewPluginFactory()
factory.Add(myEv, func(e *Event) EventResponse { return EventResponse{ ... })
factory.Run(os.Args[1], os.Args[2], os.Stdout)
}
```