/
stdio_conn.go
83 lines (68 loc) · 1.58 KB
/
stdio_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
package sshpass_proxy
import (
"fmt"
"net"
"os"
"time"
)
type stdioConn struct{}
func wrapStdioError(inErr, outErr error) error {
if inErr != nil && outErr != nil {
return fmt.Errorf("stdin error: %v, stdout error: %v", inErr, outErr)
}
if inErr != nil {
return inErr
}
if outErr != nil {
return outErr
}
return nil
}
// Close implements net.Conn
func (stdioConn) Close() error {
inErr := os.Stdin.Close()
outErr := os.Stdout.Close()
return wrapStdioError(inErr, outErr)
}
type stdioAddr struct{}
// Network implements net.Addr
func (stdioAddr) Network() string {
return "stdio"
}
// String implements net.Addr
func (stdioAddr) String() string {
return "stdio"
}
// LocalAddr implements net.Conn
func (stdioConn) LocalAddr() net.Addr {
return stdioAddr{}
}
// Read implements net.Conn
func (stdioConn) Read(b []byte) (n int, err error) {
return os.Stdin.Read(b)
}
// RemoteAddr implements net.Conn
func (stdioConn) RemoteAddr() net.Addr {
return stdioAddr{}
}
// SetDeadline implements net.Conn
func (stdioConn) SetDeadline(t time.Time) error {
inErr := os.Stdin.SetDeadline(t)
outErr := os.Stdout.SetDeadline(t)
return wrapStdioError(inErr, outErr)
}
// SetReadDeadline implements net.Conn
func (stdioConn) SetReadDeadline(t time.Time) error {
return os.Stdin.SetReadDeadline(t)
}
// SetWriteDeadline implements net.Conn
func (stdioConn) SetWriteDeadline(t time.Time) error {
return os.Stdout.SetWriteDeadline(t)
}
// Write implements net.Conn
func (stdioConn) Write(b []byte) (n int, err error) {
return os.Stdout.Write(b)
}
func StdioConn() net.Conn {
return stdioConn{}
}