-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyplugin.go
161 lines (147 loc) · 3.01 KB
/
easyplugin.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package easyplugin
import (
"bufio"
"io"
"net/rpc/jsonrpc"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type Plugin struct {
Name string
cmd *exec.Cmd
stderr io.ReadCloser
stdout io.ReadCloser
stdin io.WriteCloser
}
type PluginSystem struct {
Name string
plugins []*Plugin
f func(string)
input chan string
}
func New(name string) (*PluginSystem, error) {
home := os.Getenv("HOME")
p := ""
if home == "" && runtime.GOOS == "windows" {
p = filepath.Join(os.Getenv("APPDATA"), name, "plugins")
} else {
p = filepath.Join(home, ".config", name, "plugins")
}
err := os.MkdirAll(p, 755)
if err != nil {
return nil, err
}
dir, err := os.Open(p)
if err != nil {
return nil, err
}
defer dir.Close()
fis, err := dir.Readdir(-1)
if err != nil {
return nil, err
}
ps := &PluginSystem{
input: make(chan string),
}
runtime.SetFinalizer(ps, func(ps *PluginSystem) {
ps.Unload()
})
for _, fi := range fis {
if runtime.GOOS == "windows" && !strings.HasSuffix(fi.Name(), ".exe") {
continue
} else if fi.Mode().Perm()&0700 == 0 {
continue
}
cmd := exec.Command(filepath.Join(p, fi.Name()), "-d")
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
name := filepath.Base(fi.Name())
if ext := filepath.Ext(name); ext != "" {
name = name[:len(name)-len(ext)]
}
plugin := &Plugin{
Name: name,
cmd: cmd,
stdin: stdin,
stdout: stdout,
stderr: stderr,
}
ps.plugins = append(ps.plugins, plugin)
if strings.HasPrefix(plugin.Name, "client-") {
go func() {
s := bufio.NewScanner(plugin.stdout)
for s.Scan() {
res := s.Text()
ps.input <- res
}
}()
}
}
return ps, nil
}
func (ps *PluginSystem) Stop() {
close(ps.input)
}
func (ps *PluginSystem) ListenAndServe() {
for data := range ps.input {
ps.f(data)
}
}
func (ps *PluginSystem) Handle(f func(data string)) {
ps.f = f
}
func (ps *PluginSystem) Unload() {
for _, p := range ps.plugins {
p.cmd.Process.Kill()
}
}
type ReadWriteCloser struct {
io.Reader
io.Writer
}
func (c *ReadWriteCloser) Close() error {
return nil
}
func (ps *PluginSystem) Call(method string, args interface{}) error {
for _, plugin := range ps.plugins {
if !strings.HasPrefix(plugin.Name, "server-") {
continue
}
rwc := &ReadWriteCloser{plugin.stdout, plugin.stdin}
client := jsonrpc.NewClient(rwc)
var res interface{}
client.Call(method, args, &res)
client.Close()
}
return nil
}
func (ps *PluginSystem) CallFor(name string, method string, res interface{}, args interface{}) error {
for _, plugin := range ps.plugins {
if plugin.Name != name {
continue
}
rwc := &ReadWriteCloser{plugin.stdout, plugin.stdin}
client := jsonrpc.NewClient(rwc)
err := client.Call(method, args, &res)
client.Close()
return err
}
return os.ErrNotExist
}