forked from creack/pty
/
run.go
49 lines (45 loc) · 966 Bytes
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pty
import (
"io"
"os"
"os/exec"
"syscall"
)
// Start 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.
func Start(c *exec.Cmd) (pty *os.File, err error) {
pty, tty, err := Open()
if err != nil {
return nil, err
}
defer tty.Close()
c.Stdout = tty
c.Stdin = tty
c.Stderr = tty
c.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
err = c.Start()
if err != nil {
pty.Close()
return nil, err
}
return pty, err
}
type runner interface {
SetReadWriteCloser(io.Reader, io.Writer, io.Closer)
Start() error
}
//StartFaker allows for handing in an interface that can act like a process
func StartFaker(c runner) (pty *os.File, err error) {
pty, tty, err := Open()
if err != nil {
return nil, err
}
c.SetReadWriteCloser(tty, tty, tty)
err = c.Start()
if err != nil {
pty.Close()
return nil, err
}
return pty, err
}