File: cmd.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (74 lines) | stat: -rw-r--r-- 1,310 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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) Run() error {
	c.trace()
	return c.Cmd.Run()
}

func (c *Cmd) Start() error {
	c.trace()
	return c.Cmd.Start()
}

func (c *Cmd) Output() ([]byte, error) {
	c.trace()
	return c.Cmd.Output()
}

func (c *Cmd) CombinedOutput() ([]byte, error) {
	c.trace()
	return c.Cmd.CombinedOutput()
}

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 (c *Cmd) trace() {
	if len(c.Args) > 0 {
		Trace(c.Args[0], c.Args[1:]...)
	} else {
		Trace(c.Path)
	}
}

func newCmd(cmd *exec.Cmd) *Cmd {
	wrapped := &Cmd{Cmd: cmd}
	return wrapped
}