github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/streams/in.go (about) 1 package streams 2 3 import ( 4 "errors" 5 "io" 6 "os" 7 "runtime" 8 9 "github.com/docker/docker/pkg/term" 10 ) 11 12 // In is an input stream used by the DockerCli to read user input 13 type In struct { 14 commonStream 15 in io.ReadCloser 16 } 17 18 func (i *In) Read(p []byte) (int, error) { 19 return i.in.Read(p) 20 } 21 22 // Close implements the Closer interface 23 func (i *In) Close() error { 24 return i.in.Close() 25 } 26 27 // SetRawTerminal sets raw mode on the input terminal 28 func (i *In) SetRawTerminal() (err error) { 29 if os.Getenv("NORAW") != "" || !i.commonStream.isTerminal { 30 return nil 31 } 32 i.commonStream.state, err = term.SetRawTerminal(i.commonStream.fd) 33 return err 34 } 35 36 // CheckTty checks if we are trying to attach to a container tty 37 // from a non-tty client input stream, and if so, returns an error. 38 func (i *In) CheckTty(attachStdin, ttyMode bool) error { 39 // In order to attach to a container tty, input stream for the client must 40 // be a tty itself: redirecting or piping the client standard input is 41 // incompatible with `docker run -t`, `docker exec -t` or `docker attach`. 42 if ttyMode && attachStdin && !i.isTerminal { 43 eText := "the input device is not a TTY" 44 if runtime.GOOS == "windows" { 45 return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'") 46 } 47 return errors.New(eText) 48 } 49 return nil 50 } 51 52 // NewIn returns a new In object from a ReadCloser 53 func NewIn(in io.ReadCloser) *In { 54 fd, isTerminal := term.GetFdInfo(in) 55 return &In{commonStream: commonStream{fd: fd, isTerminal: isTerminal}, in: in} 56 }