@@ -60,7 +60,6 @@ func (s *StartCommand) addFlags() {
6060func (s * StartCommand ) runStart (args []string ) error {
6161 ctx := context .Background ()
6262 apiClient := s .cli .Client ()
63-
6463 // attach to io.
6564 if s .attach || s .stdin {
6665 var wait chan struct {}
@@ -69,17 +68,28 @@ func (s *StartCommand) runStart(args []string) error {
6968 return fmt .Errorf ("cannot start and attach multiple containers at once" )
7069 }
7170
72- in , out , err := setRawMode (s .stdin , false )
71+ container := args [0 ]
72+ c , err := apiClient .ContainerGet (ctx , container )
7373 if err != nil {
74- return fmt . Errorf ( "failed to set raw mode" )
74+ return err
7575 }
76- defer func () {
77- if err := restoreMode (in , out ); err != nil {
78- fmt .Fprintf (os .Stderr , "failed to restore term mode" )
76+
77+ if err := checkTty (s .attach , c .Config .Tty , os .Stdout .Fd ()); err != nil {
78+ return err
79+ }
80+
81+ if c .Config .Tty {
82+ in , out , err := setRawMode (s .stdin , false )
83+ if err != nil {
84+ return fmt .Errorf ("failed to set raw mode" )
7985 }
80- }()
86+ defer func () {
87+ if err := restoreMode (in , out ); err != nil {
88+ fmt .Fprintf (os .Stderr , "failed to restore term mode" )
89+ }
90+ }()
91+ }
8192
82- container := args [0 ]
8393 conn , br , err := apiClient .ContainerAttach (ctx , container , s .stdin )
8494 if err != nil {
8595 return fmt .Errorf ("failed to attach container: %v" , err )
@@ -175,6 +185,15 @@ func restoreMode(in, out *terminal.State) error {
175185 return nil
176186}
177187
188+ // CheckTty checks if we are trying to attach to a container tty
189+ // from a non-tty client input stream, and if so, returns an error.
190+ func checkTty (attachStdin , ttyMode bool , fd uintptr ) error {
191+ if ttyMode && attachStdin && ! terminal .IsTerminal (int (fd )) {
192+ return errors .New ("the input device is not a TTY" )
193+ }
194+ return nil
195+ }
196+
178197// startExample shows examples in start command, and is used in auto-generated cli docs.
179198func startExample () string {
180199 return `$ pouch ps -a
0 commit comments