mirror of
https://github.com/mudler/luet.git
synced 2025-09-02 15:54:39 +00:00
Update go-pluggable
This commit is contained in:
7
vendor/github.com/mudler/go-pluggable/events.go
generated
vendored
7
vendor/github.com/mudler/go-pluggable/events.go
generated
vendored
@@ -29,6 +29,7 @@ type EventType string
|
||||
type Event struct {
|
||||
Name EventType `json:"name"`
|
||||
Data string `json:"data"`
|
||||
File string `json:"file"` // If Data >> 10K write content to file instead
|
||||
}
|
||||
|
||||
// EventResponse describes the event response structure
|
||||
@@ -45,6 +46,12 @@ func (e Event) JSON() (string, error) {
|
||||
return string(dat), err
|
||||
}
|
||||
|
||||
// Copy returns a copy of Event
|
||||
func (e Event) Copy() *Event {
|
||||
copy := &e
|
||||
return copy
|
||||
}
|
||||
|
||||
func (e Event) ResponseEventName(s string) EventType {
|
||||
return EventType(fmt.Sprintf("%s-%s", e.Name, s))
|
||||
}
|
||||
|
31
vendor/github.com/mudler/go-pluggable/plugin.go
generated
vendored
31
vendor/github.com/mudler/go-pluggable/plugin.go
generated
vendored
@@ -16,7 +16,9 @@
|
||||
package pluggable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
@@ -29,19 +31,42 @@ type Plugin struct {
|
||||
Executable string
|
||||
}
|
||||
|
||||
// A safe threshold to avoid unpleasant exec buffer fill for argv too big. Seems 128K is the limit on Linux.
|
||||
const maxMessageSize = 1 << 13
|
||||
|
||||
// Run runs the Event on the plugin, and returns an EventResponse
|
||||
func (p Plugin) Run(e Event) (EventResponse, error) {
|
||||
r := EventResponse{}
|
||||
k, err := e.JSON()
|
||||
|
||||
eventToprocess := &e
|
||||
|
||||
if len(e.Data) > maxMessageSize {
|
||||
copy := e.Copy()
|
||||
copy.Data = ""
|
||||
f, err := ioutil.TempFile(os.TempDir(), "pluggable")
|
||||
if err != nil {
|
||||
return r, errors.Wrap(err, "while creating temporary file")
|
||||
}
|
||||
if err := ioutil.WriteFile(f.Name(), []byte(e.Data), os.ModePerm); err != nil {
|
||||
return r, errors.Wrap(err, "while writing to temporary file")
|
||||
}
|
||||
copy.File = f.Name()
|
||||
eventToprocess = copy
|
||||
defer os.RemoveAll(f.Name())
|
||||
}
|
||||
|
||||
k, err := eventToprocess.JSON()
|
||||
if err != nil {
|
||||
return r, errors.Wrap(err, "while marshalling event")
|
||||
}
|
||||
cmd := exec.Command(p.Executable, string(e.Name), k)
|
||||
cmd.Env = os.Environ()
|
||||
var b bytes.Buffer
|
||||
cmd.Stderr = &b
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
r.Error = err.Error()
|
||||
return r, errors.Wrap(err, "while executing plugin")
|
||||
r.Error = "error while executing plugin: " + err.Error() + string(b.String())
|
||||
return r, errors.Wrap(err, "while executing plugin: "+string(b.String()))
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(out, &r); err != nil {
|
||||
|
Reference in New Issue
Block a user