forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
46 lines (37 loc) · 898 Bytes
/
cmd.go
File metadata and controls
46 lines (37 loc) · 898 Bytes
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
package subprocess
import (
"io"
"os/exec"
)
// Thin wrapper around exec.Cmd. Takes care of pipe shutdown by
// keeping an internal reference to any created pipes. Whenever
// Cmd.Wait() is called, all created pipes are closed.
type Cmd struct {
*exec.Cmd
pipes []io.Closer
}
func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
stdout, err := c.Cmd.StdoutPipe()
c.pipes = append(c.pipes, stdout)
return stdout, err
}
func (c *Cmd) StderrPipe() (io.ReadCloser, error) {
stderr, err := c.Cmd.StderrPipe()
c.pipes = append(c.pipes, stderr)
return stderr, err
}
func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
stdin, err := c.Cmd.StdinPipe()
c.pipes = append(c.pipes, stdin)
return stdin, err
}
func (c *Cmd) Wait() error {
for _, pipe := range c.pipes {
pipe.Close()
}
return c.Cmd.Wait()
}
func newCmd(cmd *exec.Cmd) *Cmd {
wrapped := &Cmd{Cmd: cmd}
return wrapped
}