File: pty.go

package info (click to toggle)
git-lfs 2.7.1-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,976 kB
  • sloc: sh: 13,903; makefile: 333; ruby: 100
file content (45 lines) | stat: -rw-r--r-- 806 bytes parent folder | download
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
package subprocess

import (
	"io"
	"os"
	"os/exec"
)

// Tty is a convenience wrapper to allow pseudo-TTYs on *nix systems, create with NewTty()
// Do not use any of the struct members directly, call the Stderr() and Stdout() methods
// Remember to call Close() when finished
type Tty struct {
	cmd    *exec.Cmd
	outpty *os.File
	outtty *os.File
	errpty *os.File
	errtty *os.File
}

func (t *Tty) Close() {
	if t.outtty != nil {
		t.outtty.Close()
		t.outtty = nil
	}
	if t.errtty != nil {
		t.errtty.Close()
		t.errtty = nil
	}
}

func (t *Tty) Stdout() (io.ReadCloser, error) {
	if t.outpty != nil {
		return t.outpty, nil
	} else {
		return t.cmd.StdoutPipe()
	}
}

func (t *Tty) Stderr() (io.ReadCloser, error) {
	if t.errpty != nil {
		return t.errpty, nil
	} else {
		return t.cmd.StderrPipe()
	}
}