-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin_command.go
101 lines (89 loc) · 2.02 KB
/
plugin_command.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
package main
import (
"bufio"
"fmt"
"github.com/CrimsonAS/smokey/lib"
"io"
"log"
"net"
"os/exec"
"strings"
"sync"
)
// Experimental. Runs an out-of-process plugin.
type PcCmd struct {
}
const pcCmdDebug = false
const subprocDebug = false
func canIgnoreError(err error) bool {
if strings.Contains(err.Error(), "file already closed") {
return true
}
return false
}
func (this PcCmd) Call(inChan, outChan *lib.Channel, arguments []string) {
ln, err := net.Listen("tcp", "localhost:0")
defer ln.Close()
if err != nil {
panic(fmt.Sprintf("Plugin command can't communicate: %s", err))
}
pluginArgs := []string{ln.Addr().String()}
if len(arguments) > 1 {
pluginArgs = append(pluginArgs, arguments[1:]...)
}
pluginArgs = append(pluginArgs, pluginArgs...)
cmd := exec.Command("plugintest/plugintest", pluginArgs...)
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(fmt.Sprintf("Couldn't get stdout pipe: %s", err))
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(fmt.Sprintf("Couldn't get stderr pipe: %s", err))
}
consumer := func(from io.Reader) {
breader := bufio.NewReader(from)
for {
oneByte, err := breader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
} else if !canIgnoreError(err) {
panic(fmt.Sprintf("Stdout/stderr read failure: %s", err))
}
}
if subprocDebug && len(oneByte) > 0 {
log.Printf("STDERR: %s", oneByte)
}
}
}
go consumer(stdout)
go consumer(stderr)
if err := cmd.Start(); err != nil {
panic(fmt.Sprintf("error starting %s", err))
}
conn, err := ln.Accept()
if err != nil {
panic(fmt.Sprintf("Can't accept plugin connection: %s", err))
}
var wg sync.WaitGroup
wg.Add(2)
// Send from inChan to plugin
// Read from plugin to outChan
go func() {
lib.WriteFromChannel(conn, inChan)
wg.Done()
}()
go func() {
lib.ReadToChannel(conn, outChan)
wg.Done()
}()
wg.Wait()
conn.Close()
if err := cmd.Wait(); err != nil {
panic(fmt.Sprintf("error waiting %s", err))
}
if pcCmdDebug {
log.Printf("All done")
}
}