2019-01-28 08:30:31 -05:00
|
|
|
package streams
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
import (
|
2017-03-30 20:21:14 -04:00
|
|
|
"errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"io"
|
2017-04-25 12:25:05 -04:00
|
|
|
"os"
|
2016-09-08 13:11:39 -04:00
|
|
|
"runtime"
|
2017-04-25 12:25:05 -04:00
|
|
|
|
2020-04-16 05:23:37 -04:00
|
|
|
"github.com/moby/term"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
// In is an input stream used by the DockerCli to read user input
|
|
|
|
type In struct {
|
|
|
|
commonStream
|
2017-03-30 20:21:14 -04:00
|
|
|
in io.ReadCloser
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
func (i *In) Read(p []byte) (int, error) {
|
2016-09-08 13:11:39 -04:00
|
|
|
return i.in.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the Closer interface
|
2019-01-28 08:30:31 -05:00
|
|
|
func (i *In) Close() error {
|
2016-09-08 13:11:39 -04:00
|
|
|
return i.in.Close()
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:25:05 -04:00
|
|
|
// SetRawTerminal sets raw mode on the input terminal
|
2019-01-28 08:30:31 -05:00
|
|
|
func (i *In) SetRawTerminal() (err error) {
|
|
|
|
if os.Getenv("NORAW") != "" || !i.commonStream.isTerminal {
|
2017-04-25 12:25:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-28 08:30:31 -05:00
|
|
|
i.commonStream.state, err = term.SetRawTerminal(i.commonStream.fd)
|
2017-04-25 12:25:05 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// CheckTty checks if we are trying to attach to a container tty
|
|
|
|
// from a non-tty client input stream, and if so, returns an error.
|
2019-01-28 08:30:31 -05:00
|
|
|
func (i *In) CheckTty(attachStdin, ttyMode bool) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
// In order to attach to a container tty, input stream for the client must
|
|
|
|
// be a tty itself: redirecting or piping the client standard input is
|
|
|
|
// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
|
|
|
|
if ttyMode && attachStdin && !i.isTerminal {
|
|
|
|
eText := "the input device is not a TTY"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
|
|
|
|
}
|
|
|
|
return errors.New(eText)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
// NewIn returns a new In object from a ReadCloser
|
|
|
|
func NewIn(in io.ReadCloser) *In {
|
2016-09-08 13:11:39 -04:00
|
|
|
fd, isTerminal := term.GetFdInfo(in)
|
2019-01-28 08:30:31 -05:00
|
|
|
return &In{commonStream: commonStream{fd: fd, isTerminal: isTerminal}, in: in}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|