Add StartWithSize to allow resizing pty before starting a command to avoid races (#62)

This commit is contained in:
Dustin Spicuzza
2018-09-15 11:00:18 -04:00
committed by Guillaume J. Charmes
parent fa756f09ee
commit db8e3cd836

16
run.go
View File

@@ -12,11 +12,27 @@ import (
// and c.Stderr, calls c.Start, and returns the File of the tty's
// corresponding pty.
func Start(c *exec.Cmd) (pty *os.File, err error) {
return StartWithSize(c, nil)
}
// StartWithSize assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
// and c.Stderr, calls c.Start, and returns the File of the tty's
// corresponding pty.
//
// This will resize the pty to the specified size before starting the command
func StartWithSize(c *exec.Cmd, sz *Winsize) (pty *os.File, err error) {
pty, tty, err := Open()
if err != nil {
return nil, err
}
defer tty.Close()
if sz != nil {
err = Setsize(pty, sz)
if err != nil {
pty.Close()
return nil, err
}
}
c.Stdout = tty
c.Stdin = tty
c.Stderr = tty