Skip to content

Commit 526dfff

Browse files
committed
cli/streams: simplify CheckTty
This function is very specific to attaching to containers, and probably helps clarity to inline it where used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 48721c2 commit 526dfff

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

cli/streams/in.go

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

87
"github.com/moby/term"
98
)
@@ -55,20 +54,15 @@ func (i *In) RestoreTerminal() {
5554
i.cs.restoreTerminal()
5655
}
5756

58-
// CheckTty checks if we are trying to attach to a container TTY
59-
// from a non-TTY client input stream, and if so, returns an error.
57+
// CheckTty reports an error when stdin is requested for a TTY-enabled
58+
// container, but the client stdin is not itself a terminal (for example,
59+
// when input is piped or redirected).
6060
func (i *In) CheckTty(attachStdin, ttyMode bool) error {
61-
// In order to attach to a container tty, input stream for the client must
62-
// be a tty itself: redirecting or piping the client standard input is
63-
// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
64-
if ttyMode && attachStdin && !i.cs.isTerminal() {
65-
const eText = "the input device is not a TTY"
66-
if runtime.GOOS == "windows" {
67-
return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
68-
}
69-
return errors.New(eText)
61+
// TODO(thaJeztah): consider inlining this code and deprecating the method.
62+
if !ttyMode || !attachStdin || i.cs.isTerminal() {
63+
return nil
7064
}
71-
return nil
65+
return errors.New("cannot attach stdin to a TTY-enabled container because stdin is not a terminal")
7266
}
7367

7468
// SetIsTerminal overrides whether a terminal is connected. It is used to

cli/streams/stream.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.25
3+
14
package streams
25

36
import (

0 commit comments

Comments
 (0)