diff --git a/go.mod b/go.mod index b4535dd3..5da10f61 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,7 @@ require ( github.com/moby/moby v20.10.9+incompatible github.com/moby/sys/mount v0.2.0 // indirect github.com/mudler/cobra-extensions v0.0.0-20200612154940-31a47105fe3d - github.com/mudler/go-pluggable v0.0.0-20210513155700-54c6443073af + github.com/mudler/go-pluggable v0.0.0-20211022125509-94dbf124830d github.com/mudler/topsort v0.0.0-20201103161459-db5c7901c290 github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.16.0 @@ -54,7 +54,6 @@ require ( github.com/pkg/errors v0.9.1 github.com/pterm/pterm v0.12.32-0.20211002183613-ada9ef6790c3 github.com/rancher-sandbox/gofilecache v0.0.0-20210330135715-becdeff5df15 - github.com/sirupsen/logrus v1.8.1 github.com/spf13/cast v1.4.1 // indirect github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 diff --git a/go.sum b/go.sum index eac707f3..9f457080 100644 --- a/go.sum +++ b/go.sum @@ -811,6 +811,8 @@ github.com/mudler/cobra-extensions v0.0.0-20200612154940-31a47105fe3d h1:fKh+rvw github.com/mudler/cobra-extensions v0.0.0-20200612154940-31a47105fe3d/go.mod h1:puRUWSwyecW2V355tKncwPVPRAjQBduPsFjG0mrV/Nw= github.com/mudler/go-pluggable v0.0.0-20210513155700-54c6443073af h1:jixIxEgLSqu24eMiyzfCI+roa5IaOUhF546ePSFyHeY= github.com/mudler/go-pluggable v0.0.0-20210513155700-54c6443073af/go.mod h1:WmKcT8ONmhDQIqQ+HxU+tkGWjzBEyY/KFO8LTGCu4AI= +github.com/mudler/go-pluggable v0.0.0-20211022125509-94dbf124830d h1:NKvvf/q1dWDde+yg5cMiU5EuYZ2jNuKs/9hb8xod8A0= +github.com/mudler/go-pluggable v0.0.0-20211022125509-94dbf124830d/go.mod h1:WmKcT8ONmhDQIqQ+HxU+tkGWjzBEyY/KFO8LTGCu4AI= github.com/mudler/topsort v0.0.0-20201103161459-db5c7901c290 h1:426hFyXMpXeqIeGJn2cGAW9ogvM2Jf+Jv23gtVPvBLM= github.com/mudler/topsort v0.0.0-20201103161459-db5c7901c290/go.mod h1:uP5BBgFxq2wNWo7n1vnY5SSbgL0WDshVJrOO12tZ/lA= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= diff --git a/vendor/github.com/mudler/go-pluggable/README.md b/vendor/github.com/mudler/go-pluggable/README.md index 205f79ed..f75217d8 100644 --- a/vendor/github.com/mudler/go-pluggable/README.md +++ b/vendor/github.com/mudler/go-pluggable/README.md @@ -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) +} + +``` \ No newline at end of file diff --git a/vendor/github.com/mudler/go-pluggable/factory.go b/vendor/github.com/mudler/go-pluggable/factory.go new file mode 100644 index 00000000..767020d6 --- /dev/null +++ b/vendor/github.com/mudler/go-pluggable/factory.go @@ -0,0 +1,86 @@ +// Copyright © 2021 Ettore Di Giacinto +// +// 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 . + +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 +} diff --git a/vendor/github.com/mudler/go-pluggable/manager.go b/vendor/github.com/mudler/go-pluggable/manager.go index d2dac6f5..1c42e3ec 100644 --- a/vendor/github.com/mudler/go-pluggable/manager.go +++ b/vendor/github.com/mudler/go-pluggable/manager.go @@ -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 diff --git a/vendor/modules.txt b/vendor/modules.txt index dd84faa0..457c4f32 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -339,7 +339,7 @@ github.com/morikuni/aec # github.com/mudler/cobra-extensions v0.0.0-20200612154940-31a47105fe3d ## explicit github.com/mudler/cobra-extensions -# github.com/mudler/go-pluggable v0.0.0-20210513155700-54c6443073af +# github.com/mudler/go-pluggable v0.0.0-20211022125509-94dbf124830d ## explicit github.com/mudler/go-pluggable # github.com/mudler/topsort v0.0.0-20201103161459-db5c7901c290 @@ -434,7 +434,6 @@ github.com/rogpeppe/go-internal/internal/syscall/windows/sysdll github.com/rogpeppe/go-internal/lockedfile github.com/rogpeppe/go-internal/lockedfile/internal/filelock # github.com/sirupsen/logrus v1.8.1 -## explicit github.com/sirupsen/logrus # github.com/spf13/afero v1.6.0 github.com/spf13/afero