File tree Expand file tree Collapse file tree 3 files changed +37
-7
lines changed Expand file tree Collapse file tree 3 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ func NewPtyWriter(w io.Writer) io.Writer {
13
13
}
14
14
}
15
15
16
+ var _ io.Writer = ptyWriter {}
17
+
16
18
type ptyWriter struct {
17
19
w io.Writer
18
20
}
@@ -29,3 +31,27 @@ func (w ptyWriter) Write(p []byte) (int, error) {
29
31
}
30
32
return n , err
31
33
}
34
+
35
+ // NewPtyReadWriter return an io.ReadWriter that delegates the read to the
36
+ // given io.ReadWriter, and the writes to a ptyWriter.
37
+ func NewPtyReadWriter (rw io.ReadWriter ) io.ReadWriter {
38
+ return readWriterDelegate {
39
+ w : NewPtyWriter (rw ),
40
+ r : rw ,
41
+ }
42
+ }
43
+
44
+ var _ io.ReadWriter = readWriterDelegate {}
45
+
46
+ type readWriterDelegate struct {
47
+ w io.Writer
48
+ r io.Reader
49
+ }
50
+
51
+ func (rw readWriterDelegate ) Read (p []byte ) (n int , err error ) {
52
+ return rw .r .Read (p )
53
+ }
54
+
55
+ func (rw readWriterDelegate ) Write (p []byte ) (n int , err error ) {
56
+ return rw .w .Write (p )
57
+ }
Original file line number Diff line number Diff line change @@ -83,9 +83,11 @@ type Session interface {
83
83
// During the time that no channel is registered, breaks are ignored.
84
84
Break (c chan <- bool )
85
85
86
- // SafeStderr returns the Stderr io.Writer that handles replacing \n with
87
- // \r\n when there's an active Pty.
88
- SafeStderr () io.Writer
86
+ // Stderr returns an io.ReadWriter that writes to this channel
87
+ // with the extended data type set to stderr. Stderr may
88
+ // safely be read and written from a different goroutine than
89
+ // Read and Write respectively.
90
+ Stderr () io.ReadWriter
89
91
}
90
92
91
93
// maxSigBufSize is how many signals will be buffered
@@ -131,11 +133,11 @@ type session struct {
131
133
breakCh chan <- bool
132
134
}
133
135
134
- func (sess * session ) SafeStderr () io.Writer {
136
+ func (sess * session ) Stderr () io.ReadWriter {
135
137
if sess .pty != nil {
136
- return NewPtyWriter (sess .Stderr ())
138
+ return NewPtyReadWriter (sess . Channel .Stderr ())
137
139
}
138
- return sess .Stderr ()
140
+ return sess .Channel . Stderr ()
139
141
}
140
142
141
143
func (sess * session ) Write (p []byte ) (int , error ) {
Original file line number Diff line number Diff line change 6
6
"io"
7
7
"net"
8
8
"testing"
9
+ "time"
9
10
10
11
gossh "golang.org/x/crypto/ssh"
11
12
)
@@ -236,7 +237,8 @@ func TestPtyWriter(t *testing.T) {
236
237
session , _ , cleanup := newTestSession (t , & Server {
237
238
Handler : func (s Session ) {
238
239
_ , _ = fmt .Fprintln (s , "foo\n bar" )
239
- _ , _ = fmt .Fprintln (s .SafeStderr (), "many\n errors" )
240
+ time .Sleep (10 * time .Millisecond )
241
+ _ , _ = fmt .Fprintln (s .Stderr (), "many\n errors" )
240
242
_ = s .Exit (0 )
241
243
},
242
244
}, nil )
You can’t perform that action at this time.
0 commit comments