Debian Codegraph @

subprocess/cmd.go

raw symbols SCIP
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) Run() error {
18 c.trace()
19 return c.Cmd.Run()
20 }
21
22 func (c *Cmd) Start() error {
23 c.trace()
24 return c.Cmd.Start()
25 }
26
27 func (c *Cmd) Output() ([]byte, error) {
28 c.trace()
29 return c.Cmd.Output()
30 }
31
32 func (c *Cmd) CombinedOutput() ([]byte, error) {
33 c.trace()
34 return c.Cmd.CombinedOutput()
35 }
36
37 func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
38 stdout, err := c.Cmd.StdoutPipe()
39 c.pipes = append(c.pipes, stdout)
40 return stdout, err
41 }
42
43 func (c *Cmd) StderrPipe() (io.ReadCloser, error) {
44 stderr, err := c.Cmd.StderrPipe()
45 c.pipes = append(c.pipes, stderr)
46 return stderr, err
47 }
48
49 func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
50 stdin, err := c.Cmd.StdinPipe()
51 c.pipes = append(c.pipes, stdin)
52 return stdin, err
53 }
54
55 func (c *Cmd) Wait() error {
56 for _, pipe := range c.pipes {
57 pipe.Close()
58 }
59
60 return c.Cmd.Wait()
61 }
62
63 func (c *Cmd) trace() {
64 if len(c.Args) > 0 {
65 Trace(c.Args[0], c.Args[1:]...)
66 } else {
67 Trace(c.Path)
68 }
69 }
70
71 func newCmd(cmd *exec.Cmd) *Cmd {
72 wrapped := &Cmd{Cmd: cmd}
73 return wrapped
74 }