Don't change terminal settings if not run in a terminal
Don't try to put a filedescriptor which is not a terminal into raw mode.
@@ -12,14 +12,17 @@ import (
// AttachConsole attaches stdin and stdout to the console
// in -> stdinWriter | stdinReader -> console
// out <- stdoutReader | stdoutWriter <- console
-func AttachConsole(stdinReader, stdoutReader *io.PipeReader, stdinWriter, stdoutWriter *io.PipeWriter, message string, resChan <-chan error) error {
+func AttachConsole(stdinReader, stdoutReader *io.PipeReader, stdinWriter, stdoutWriter *io.PipeWriter, message string, resChan <-chan error) (err error) {
stopChan := make(chan struct{}, 1)
writeStop := make(chan error)
readStop := make(chan error)
+ if terminal.IsTerminal(int(os.Stdin.Fd())) {
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return fmt.Errorf("Make raw terminal failed: %s", err)
}
+ defer terminal.Restore(int(os.Stdin.Fd()), state)
+ }
fmt.Fprint(os.Stderr, message)
in := os.Stdin
@@ -70,6 +73,5 @@ func AttachConsole(stdinReader, stdoutReader *io.PipeReader, stdinWriter, stdout
case err = <-resChan:
}
- terminal.Restore(int(os.Stdin.Fd()), state)
return err
}
