in.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package command
  2. import (
  3. "errors"
  4. "github.com/docker/docker/pkg/term"
  5. "io"
  6. "runtime"
  7. )
  8. // InStream is an input stream used by the DockerCli to read user input
  9. type InStream struct {
  10. CommonStream
  11. in io.ReadCloser
  12. }
  13. func (i *InStream) Read(p []byte) (int, error) {
  14. return i.in.Read(p)
  15. }
  16. // Close implements the Closer interface
  17. func (i *InStream) Close() error {
  18. return i.in.Close()
  19. }
  20. // CheckTty checks if we are trying to attach to a container tty
  21. // from a non-tty client input stream, and if so, returns an error.
  22. func (i *InStream) CheckTty(attachStdin, ttyMode bool) error {
  23. // In order to attach to a container tty, input stream for the client must
  24. // be a tty itself: redirecting or piping the client standard input is
  25. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  26. if ttyMode && attachStdin && !i.isTerminal {
  27. eText := "the input device is not a TTY"
  28. if runtime.GOOS == "windows" {
  29. return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
  30. }
  31. return errors.New(eText)
  32. }
  33. return nil
  34. }
  35. // NewInStream returns a new InStream object from a ReadCloser
  36. func NewInStream(in io.ReadCloser) *InStream {
  37. fd, isTerminal := term.GetFdInfo(in)
  38. return &InStream{CommonStream: CommonStream{fd: fd, isTerminal: isTerminal}, in: in}
  39. }