From 526dfffc26f355e2ecd0298212c99d265ef1d8de Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 1 Apr 2026 15:16:04 +0200 Subject: [PATCH] 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 --- cli/streams/in.go | 20 +++++++------------- cli/streams/stream.go | 3 +++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cli/streams/in.go b/cli/streams/in.go index b3849e8f41..f060d81ec1 100644 --- a/cli/streams/in.go +++ b/cli/streams/in.go @@ -3,7 +3,6 @@ package streams import ( "errors" "io" - "runtime" "github.com/moby/term" ) @@ -55,20 +54,15 @@ func (i *In) RestoreTerminal() { i.cs.restoreTerminal() } -// CheckTty checks if we are trying to attach to a container TTY -// from a non-TTY client input stream, and if so, returns an error. +// CheckTty reports an error when stdin is requested for a TTY-enabled +// container, but the client stdin is not itself a terminal (for example, +// when input is piped or redirected). func (i *In) CheckTty(attachStdin, ttyMode bool) error { - // In order to attach to a container tty, input stream for the client must - // be a tty itself: redirecting or piping the client standard input is - // incompatible with `docker run -t`, `docker exec -t` or `docker attach`. - if ttyMode && attachStdin && !i.cs.isTerminal() { - const eText = "the input device is not a TTY" - if runtime.GOOS == "windows" { - return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'") - } - return errors.New(eText) + // TODO(thaJeztah): consider inlining this code and deprecating the method. + if !ttyMode || !attachStdin || i.cs.isTerminal() { + return nil } - return nil + return errors.New("cannot attach stdin to a TTY-enabled container because stdin is not a terminal") } // SetIsTerminal overrides whether a terminal is connected. It is used to diff --git a/cli/streams/stream.go b/cli/streams/stream.go index 941084dde4..35758fbe97 100644 --- a/cli/streams/stream.go +++ b/cli/streams/stream.go @@ -1,3 +1,6 @@ +// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: +//go:build go1.25 + package streams import (