forked from jopbrown/gtk-sugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_conn.go
157 lines (127 loc) · 3.04 KB
/
client_conn.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
package sugar
import (
"fmt"
"io"
"os"
"runtime"
"sync"
"github.com/pkg/errors"
)
type Conn interface {
Open() error
io.ReadWriteCloser
}
type mutexConn struct {
inner Conn
mux sync.Mutex
}
func wrapMutex(conn Conn) Conn {
return &mutexConn{inner: conn}
}
func (conn *mutexConn) Open() error {
conn.mux.Lock()
defer conn.mux.Unlock()
return conn.inner.Open()
}
func (conn *mutexConn) Write(p []byte) (n int, err error) {
conn.mux.Lock()
defer conn.mux.Unlock()
return conn.inner.Write(p)
}
func (conn *mutexConn) Read(p []byte) (n int, err error) {
conn.mux.Lock()
defer conn.mux.Unlock()
return conn.inner.Read(p)
}
func (conn *mutexConn) Close() error {
conn.mux.Lock()
defer conn.mux.Unlock()
return conn.inner.Close()
}
type ConnAgent func(*client)
type stdinConn struct {
w io.Writer
r io.Reader
}
func (conn *stdinConn) Open() error {
return nil
}
func (conn *stdinConn) Write(p []byte) (n int, err error) {
return conn.w.Write(p)
}
func (conn *stdinConn) Read(p []byte) (n int, err error) {
return conn.r.Read(p)
}
func (conn *stdinConn) Close() error {
return nil
}
func ConnStdin() ConnAgent {
return func(c *client) {
w, err := c.cmd.StdinPipe()
if err != nil {
panic(fmt.Sprintf("FATAL:%+v", errors.Wrap(err, "invalid operation to create pipe for stdin")))
}
r, err := c.cmd.StdoutPipe()
if err != nil {
panic(fmt.Sprintf("FATAL:%+v", errors.Wrap(err, "invalid operation to create pipe for stdout")))
}
c.cmd.Args = append(c.cmd.Args, "-stdin")
c.conn = &stdinConn{w: w, r: r}
}
}
type fifoConn struct {
inFileName string
outFileName string
w io.WriteCloser
r io.ReadCloser
}
func (conn *fifoConn) Open() error {
w, err := os.OpenFile(conn.outFileName, os.O_WRONLY, 0666)
if err != nil {
return errors.Wrapf(err, "fifo: unable to open as output file for %v", conn.outFileName)
}
r, err := os.OpenFile(conn.inFileName, os.O_RDONLY, 0666)
if err != nil {
return errors.Wrapf(err, "fifo: unable to open as input file for %v", conn.inFileName)
}
conn.w = w
conn.r = r
return nil
}
func (conn *fifoConn) Write(p []byte) (n int, err error) {
return conn.w.Write(p)
}
func (conn *fifoConn) Read(p []byte) (n int, err error) {
return conn.r.Read(p)
}
func (conn *fifoConn) Close() (err error) {
defer func() {
if err != nil {
err = errors.Wrap(err, "unable to close fifo")
}
}()
defer func() {
errw := conn.w.Close()
if errw != nil {
err = errw
}
}()
defer func() {
errr := conn.r.Close()
if errr != nil {
err = errr
}
}()
return
}
func ConnFifo(fileName string) ConnAgent {
return func(c *client) {
if runtime.GOOS == "windows" {
c.cmd.Args = append(c.cmd.Args, "-fifo")
c.conn = &fifoConn{inFileName: WINDOWS_PIPEIN, outFileName: WINDOWS_PIPEOUT}
} else {
c.cmd.Args = append(c.cmd.Args, "-fifo="+fileName)
c.conn = &fifoConn{inFileName: fileName, outFileName: fileName}
}
}
}