github.com/catandhorse/git-lfs@v2.5.2+incompatible/subprocess/cmd.go (about)

     1  package subprocess
     2  
     3  import (
     4  	"io"
     5  	"os/exec"
     6  )
     7  
     8  // Thin wrapper around exec.Cmd. Takes care of pipe shutdown by
     9  // keeping an internal reference to any created pipes. Whenever
    10  // Cmd.Wait() is called, all created pipes are closed.
    11  type Cmd struct {
    12  	*exec.Cmd
    13  
    14  	pipes []io.Closer
    15  }
    16  
    17  func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
    18  	stdout, err := c.Cmd.StdoutPipe()
    19  	c.pipes = append(c.pipes, stdout)
    20  	return stdout, err
    21  }
    22  
    23  func (c *Cmd) StderrPipe() (io.ReadCloser, error) {
    24  	stderr, err := c.Cmd.StderrPipe()
    25  	c.pipes = append(c.pipes, stderr)
    26  	return stderr, err
    27  }
    28  
    29  func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
    30  	stdin, err := c.Cmd.StdinPipe()
    31  	c.pipes = append(c.pipes, stdin)
    32  	return stdin, err
    33  }
    34  
    35  func (c *Cmd) Wait() error {
    36  	for _, pipe := range c.pipes {
    37  		pipe.Close()
    38  	}
    39  
    40  	return c.Cmd.Wait()
    41  }
    42  
    43  func newCmd(cmd *exec.Cmd) *Cmd {
    44  	wrapped := &Cmd{Cmd: cmd}
    45  	return wrapped
    46  }