mirror of
https://github.com/mudler/luet.git
synced 2025-09-01 15:18:28 +00:00
update vendor
This commit is contained in:
72
vendor/github.com/mudler/go-pluggable/README.md
generated
vendored
72
vendor/github.com/mudler/go-pluggable/README.md
generated
vendored
@@ -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)
|
||||
}
|
||||
|
||||
```
|
86
vendor/github.com/mudler/go-pluggable/factory.go
generated
vendored
Normal file
86
vendor/github.com/mudler/go-pluggable/factory.go
generated
vendored
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright © 2021 Ettore Di Giacinto <mudler@mocaccino.org>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package pluggable
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type FactoryPlugin struct {
|
||||
EventType EventType
|
||||
PluginHandler PluginHandler
|
||||
}
|
||||
|
||||
func NewPluginFactory(p ...FactoryPlugin) PluginFactory {
|
||||
f := make(PluginFactory)
|
||||
for _, pp := range p {
|
||||
f.Add(pp.EventType, pp.PluginHandler)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// PluginHandler represent a generic plugin which
|
||||
// talks go-pluggable API
|
||||
// It receives an event, and is always expected to give a response
|
||||
type PluginHandler func(*Event) EventResponse
|
||||
|
||||
// PluginFactory is a collection of handlers for a given event type.
|
||||
// a plugin has to respond to multiple events and it always needs to return an
|
||||
// Event response as result
|
||||
type PluginFactory map[EventType]PluginHandler
|
||||
|
||||
// Run runs the PluginHandler given a event type and a payload
|
||||
//
|
||||
// The result is written to the writer provided
|
||||
// as argument.
|
||||
func (p PluginFactory) Run(name EventType, payload string, w io.Writer) error {
|
||||
ev := &Event{}
|
||||
|
||||
if err := json.Unmarshal([]byte(payload), ev); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ev.File != "" {
|
||||
c, err := ioutil.ReadFile(ev.File)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ev.Data = string(c)
|
||||
}
|
||||
|
||||
resp := EventResponse{}
|
||||
for e, r := range p {
|
||||
if name == e {
|
||||
resp = r(ev)
|
||||
}
|
||||
}
|
||||
|
||||
dat, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(dat)
|
||||
return err
|
||||
}
|
||||
|
||||
// Add associates an handler to an event type
|
||||
func (p PluginFactory) Add(ev EventType, ph PluginHandler) {
|
||||
p[ev] = ph
|
||||
}
|
13
vendor/github.com/mudler/go-pluggable/manager.go
generated
vendored
13
vendor/github.com/mudler/go-pluggable/manager.go
generated
vendored
@@ -97,6 +97,15 @@ func relativeToCwd(p string) (string, error) {
|
||||
return filepath.Join(cwd, p), nil
|
||||
}
|
||||
|
||||
func (m *Manager) insertPlugin(p Plugin) {
|
||||
for _, i := range m.Plugins {
|
||||
if i.Executable == p.Executable {
|
||||
return
|
||||
}
|
||||
}
|
||||
m.Plugins = append(m.Plugins, p)
|
||||
}
|
||||
|
||||
// Autoload automatically loads plugins binaries prefixed by 'prefix' in the current path
|
||||
// optionally takes a list of paths to look also into
|
||||
func (m *Manager) Autoload(prefix string, extensionpath ...string) *Manager {
|
||||
@@ -123,7 +132,7 @@ func (m *Manager) Autoload(prefix string, extensionpath ...string) *Manager {
|
||||
}
|
||||
for _, ma := range matches {
|
||||
short := strings.TrimPrefix(filepath.Base(ma), projPrefix)
|
||||
m.Plugins = append(m.Plugins, Plugin{Name: short, Executable: ma})
|
||||
m.insertPlugin(Plugin{Name: short, Executable: ma})
|
||||
}
|
||||
}
|
||||
return m
|
||||
@@ -140,7 +149,7 @@ func (m *Manager) Load(extname ...string) *Manager {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
m.Plugins = append(m.Plugins, Plugin{Name: n, Executable: path})
|
||||
m.insertPlugin(Plugin{Name: n, Executable: path})
|
||||
}
|
||||
}
|
||||
return m
|
||||
|
Reference in New Issue
Block a user