github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/in.go (about)

     1  package command
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os"
     7  	"runtime"
     8  
     9  	"github.com/docker/docker/pkg/term"
    10  )
    11  
    12  // InStream is an input stream used by the DockerCli to read user input
    13  type InStream struct {
    14  	CommonStream
    15  	in io.ReadCloser
    16  }
    17  
    18  func (i *InStream) Read(p []byte) (int, error) {
    19  	return i.in.Read(p)
    20  }
    21  
    22  // Close implements the Closer interface
    23  func (i *InStream) Close() error {
    24  	return i.in.Close()
    25  }
    26  
    27  // SetRawTerminal sets raw mode on the input terminal
    28  func (i *InStream) 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 *InStream) 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  // NewInStream returns a new InStream object from a ReadCloser
    53  func NewInStream(in io.ReadCloser) *InStream {
    54  	fd, isTerminal := term.GetFdInfo(in)
    55  	return &InStream{CommonStream: CommonStream{fd: fd, isTerminal: isTerminal}, in: in}
    56  }