forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
63 lines (59 loc) · 1.38 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package buffalo
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"github.com/gobuffalo/buffalo/plugins"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/events"
)
var loadPlugins sync.Once
// LoadPlugins will add listeners for any plugins that support "events"
func LoadPlugins() error {
var errResult error
loadPlugins.Do(func() {
// don't send plugins events during testing
if envy.Get("GO_ENV", "development") == "test" {
return
}
plugs, err := plugins.Available()
if err != nil {
errResult = err
return
}
for _, cmds := range plugs {
for _, c := range cmds {
if c.BuffaloCommand != "events" {
continue
}
err := func(c plugins.Command) error {
n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name)
fn := func(e events.Event) {
b, err := json.Marshal(e)
if err != nil {
fmt.Println("error trying to marshal event", e, err)
return
}
cmd := exec.Command(c.Binary, c.UseCommand, string(b))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err)
}
}
_, err := events.NamedListen(n, events.Filter(c.ListenFor, fn))
return err
}(c)
if err != nil {
errResult = err
return
}
}
}
})
return errResult
}