Skip to content

Commit 34805dd

Browse files
committed
cli/streams: (In|Out).SetRawTerminal: dry
Move the code to the commonStream type, which is where the actual state field is kept. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 7639343 commit 34805dd

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

cli/streams/in.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package streams
33
import (
44
"errors"
55
"io"
6-
"os"
76
"runtime"
87

98
"github.com/moby/term"
@@ -29,12 +28,8 @@ func (i *In) Close() error {
2928
// SetRawTerminal sets raw mode on the input terminal. It is a no-op if In
3029
// is not a TTY, or if the "NORAW" environment variable is set to a non-empty
3130
// value.
32-
func (i *In) SetRawTerminal() (err error) {
33-
if !i.isTerminal || os.Getenv("NORAW") != "" {
34-
return nil
35-
}
36-
i.state, err = term.SetRawTerminal(i.fd)
37-
return err
31+
func (i *In) SetRawTerminal() error {
32+
return i.setRawTerminal(term.SetRawTerminal)
3833
}
3934

4035
// CheckTty checks if we are trying to attach to a container TTY

cli/streams/out.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package streams
22

33
import (
44
"io"
5-
"os"
65

76
"github.com/moby/term"
87
"github.com/sirupsen/logrus"
@@ -26,12 +25,8 @@ func (o *Out) Write(p []byte) (int, error) {
2625
// On UNIX, this does nothing. On Windows, it disables LF -> CRLF/ translation.
2726
// It is a no-op if Out is not a TTY, or if the "NORAW" environment variable is
2827
// set to a non-empty value.
29-
func (o *Out) SetRawTerminal() (err error) {
30-
if !o.isTerminal || os.Getenv("NORAW") != "" {
31-
return nil
32-
}
33-
o.state, err = term.SetRawTerminalOutput(o.fd)
34-
return err
28+
func (o *Out) SetRawTerminal() error {
29+
return o.setRawTerminal(term.SetRawTerminalOutput)
3530
}
3631

3732
// GetTtySize returns the height and width in characters of the TTY, or

cli/streams/stream.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package streams
22

33
import (
4+
"os"
5+
46
"github.com/moby/term"
57
)
68

@@ -20,13 +22,25 @@ func (s *commonStream) IsTerminal() bool {
2022
return s.isTerminal
2123
}
2224

23-
// RestoreTerminal restores normal mode to the terminal.
25+
// RestoreTerminal restores the terminal state if SetRawTerminal succeeded earlier.
2426
func (s *commonStream) RestoreTerminal() {
2527
if s.state != nil {
2628
_ = term.RestoreTerminal(s.fd, s.state)
2729
}
2830
}
2931

32+
func (s *commonStream) setRawTerminal(setter func(uintptr) (*term.State, error)) error {
33+
if !s.isTerminal || os.Getenv("NORAW") != "" {
34+
return nil
35+
}
36+
state, err := setter(s.fd)
37+
if err != nil {
38+
return err
39+
}
40+
s.state = state
41+
return nil
42+
}
43+
3044
// SetIsTerminal overrides whether a terminal is connected. It is used to
3145
// override this property in unit-tests, and should not be depended on for
3246
// other purposes.

0 commit comments

Comments
 (0)