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
|
|
|
)
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// In is an input stream to read user input. It implements [io.ReadCloser]
|
|
|
|
// with additional utilities, such as putting the terminal in raw mode.
|
2019-01-28 08:30:31 -05:00
|
|
|
type In struct {
|
|
|
|
commonStream
|
2017-03-30 20:21:14 -04:00
|
|
|
in io.ReadCloser
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// Read implements the [io.Reader] interface.
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// Close implements the [io.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()
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// SetRawTerminal sets raw mode on the input terminal. It is a no-op if In
|
|
|
|
// is not a TTY, or if the "NORAW" environment variable is set to a non-empty
|
|
|
|
// value.
|
2019-01-28 08:30:31 -05:00
|
|
|
func (i *In) SetRawTerminal() (err error) {
|
2023-04-01 14:11:42 -04:00
|
|
|
if !i.isTerminal || os.Getenv("NORAW") != "" {
|
2017-04-25 12:25:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-01 14:11:42 -04:00
|
|
|
i.state, err = term.SetRawTerminal(i.fd)
|
2017-04-25 12:25:05 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -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 {
|
2023-04-01 14:11:42 -04:00
|
|
|
const eText = "the input device is not a TTY"
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// NewIn returns a new [In] from an [io.ReadCloser].
|
2019-01-28 08:30:31 -05:00
|
|
|
func NewIn(in io.ReadCloser) *In {
|
2023-04-01 14:11:42 -04:00
|
|
|
i := &In{in: in}
|
|
|
|
i.fd, i.isTerminal = term.GetFdInfo(in)
|
|
|
|
return i
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|