pty

package
v0.27.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2023 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = xerrors.New("pty: closed")

ErrClosed is returned when a PTY is used after it has been closed.

Functions

func Start

func Start(cmd *Cmd, opt ...StartOption) (PTYCmd, Process, error)

Start the command in a TTY. The calling code must not use cmd after passing it to the PTY, and instead rely on the returned Process to manage the command/process.

Types

type Cmd added in v0.23.1

type Cmd struct {
	Context context.Context
	Path    string
	Args    []string
	Env     []string
	Dir     string
}

Cmd is a drop-in replacement for exec.Cmd with most of the same API, but it exposes the context.Context to our PTY code so that we can still kill the process when the Context expires. This is required because on Windows, we don't start the command using the `exec` library, so we have to manage the context ourselves.

func Command added in v0.23.1

func Command(name string, arg ...string) *Cmd

func CommandContext added in v0.23.1

func CommandContext(ctx context.Context, name string, arg ...string) *Cmd

func (*Cmd) AsExec added in v0.23.1

func (c *Cmd) AsExec() *exec.Cmd

type Option added in v0.8.15

type Option func(*ptyOptions)

Options represents a an option for a PTY.

func WithGPGTTY added in v0.14.0

func WithGPGTTY() Option

WithGPGTTY sets the GPG_TTY environment variable to the PTY name. This only applies to non-Windows platforms.

func WithLogger added in v0.8.15

func WithLogger(logger *log.Logger) Option

WithLogger sets a logger for logging errors.

func WithSSHRequest added in v0.8.15

func WithSSHRequest(req ssh.Pty) Option

WithSSHRequest applies the ssh.Pty request to the PTY.

Only partially supported on Windows (e.g. window size).

type PTY

type PTY interface {
	io.Closer

	// Resize sets the size of the PTY.
	Resize(height uint16, width uint16) error

	// Name of the TTY. Example on Linux would be "/dev/pts/1".
	Name() string

	// Output handles TTY output.
	//
	// cmd.SetOutput(pty.Output()) would be used to specify a command
	// uses the output stream for writing.
	//
	// The same stream could be read to validate output.
	Output() ReadWriter

	// Input handles TTY input.
	//
	// cmd.SetInput(pty.Input()) would be used to specify a command
	// uses the PTY input for reading.
	//
	// The same stream would be used to provide user input: pty.Input().Write(...)
	Input() ReadWriter
}

PTY is a minimal interface for interacting with pseudo-TTY where this process retains access to _both_ ends of the pseudo-TTY (i.e. `ptm` & `pts` on Linux).

func New

func New(opts ...Option) (PTY, error)

New constructs a new Pty.

type PTYCmd added in v0.23.0

type PTYCmd interface {
	io.Closer

	// Resize sets the size of the PTY.
	Resize(height uint16, width uint16) error

	// OutputReader returns an io.Reader for reading the output from the process
	// controlled by the pseudo-TTY
	OutputReader() io.Reader

	// InputWriter returns an io.Writer for writing into to the process
	// controlled by the pseudo-TTY
	InputWriter() io.Writer
}

PTYCmd is an interface for interacting with a pseudo-TTY where we control only one end, and the other end has been passed to a running os.Process. nolint:revive

type Process added in v0.8.2

type Process interface {
	// Wait for the command to complete.  Returned error is as for exec.Cmd.Wait()
	Wait() error

	// Kill the command process.  Returned error is as for os.Process.Kill()
	Kill() error
}

Process represents a process running in a PTY. We need to trigger special processing on the PTY on process completion, meaning that we will have goroutines calling Wait() on the process. Since the caller will also typically wait for the process, and it is not safe for multiple goroutines to Wait() on a process, this abstraction provides a goroutine-safe interface for interacting with the process.

type ReadWriter added in v0.6.0

type ReadWriter struct {
	Reader io.Reader
	Writer io.Writer
}

ReadWriter is an implementation of io.ReadWriter that wraps two separate underlying file descriptors, one for reading and one for writing, and allows them to be accessed separately.

func (ReadWriter) Read added in v0.6.0

func (rw ReadWriter) Read(p []byte) (int, error)

func (ReadWriter) Write added in v0.6.0

func (rw ReadWriter) Write(p []byte) (int, error)

type StartOption added in v0.8.15

type StartOption func(*startOptions)

StartOption represents a configuration option passed to Start.

func WithPTYOption added in v0.8.15

func WithPTYOption(opts ...Option) StartOption

WithPTYOption applies the given options to the underlying PTY.

type WithFlags added in v0.6.0

type WithFlags interface {
	PTY

	// EchoEnabled determines whether local echo is currently enabled for this terminal.
	EchoEnabled() (bool, error)
}

WithFlags represents a PTY whose flags can be inspected, in particular to determine whether local echo is enabled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL