|
| 1 | +package exec |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "sync" |
| 9 | +) |
| 10 | + |
| 11 | +// LookPath is the wrapper of os/exec.LookPath |
| 12 | +func LookPath(file string) (string, error) { |
| 13 | + return exec.LookPath(file) |
| 14 | +} |
| 15 | + |
| 16 | +// RunCommandAndReturn runs a command, then returns the output |
| 17 | +func RunCommandAndReturn(name, dir string, args ...string) (result string, err error) { |
| 18 | + stdout := &bytes.Buffer{} |
| 19 | + if err = RunCommandWithBuffer(name, dir, stdout, nil, args...); err == nil { |
| 20 | + result = stdout.String() |
| 21 | + } |
| 22 | + return |
| 23 | +} |
| 24 | + |
| 25 | +// RunCommandWithBuffer runs a command with buffer |
| 26 | +// stdout and stderr could be nil |
| 27 | +func RunCommandWithBuffer(name, dir string, stdout, stderr *bytes.Buffer, args ...string) error { |
| 28 | + if stdout == nil { |
| 29 | + stdout = &bytes.Buffer{} |
| 30 | + } |
| 31 | + if stderr != nil { |
| 32 | + stderr = &bytes.Buffer{} |
| 33 | + } |
| 34 | + return RunCommandWithIO(name, dir, stdout, stderr, args...) |
| 35 | +} |
| 36 | + |
| 37 | +// RunCommandWithIO runs a command with given IO |
| 38 | +func RunCommandWithIO(name, dir string, stdout, stderr io.Writer, args ...string) (err error) { |
| 39 | + command := exec.Command(name, args...) |
| 40 | + if dir != "" { |
| 41 | + command.Dir = dir |
| 42 | + } |
| 43 | + |
| 44 | + //var stdout []byte |
| 45 | + //var errStdout error |
| 46 | + stdoutIn, _ := command.StdoutPipe() |
| 47 | + stderrIn, _ := command.StderrPipe() |
| 48 | + err = command.Start() |
| 49 | + if err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + // cmd.Wait() should be called only after we finish reading |
| 54 | + // from stdoutIn and stderrIn. |
| 55 | + // wg ensures that we finish |
| 56 | + var wg sync.WaitGroup |
| 57 | + wg.Add(1) |
| 58 | + go func() { |
| 59 | + _, _ = copyAndCapture(stdout, stdoutIn) |
| 60 | + wg.Done() |
| 61 | + }() |
| 62 | + |
| 63 | + _, _ = copyAndCapture(stderr, stderrIn) |
| 64 | + |
| 65 | + wg.Wait() |
| 66 | + |
| 67 | + err = command.Wait() |
| 68 | + return |
| 69 | +} |
| 70 | + |
| 71 | +// RunCommandInDir runs a command |
| 72 | +func RunCommandInDir(name, dir string, args ...string) error { |
| 73 | + return RunCommandWithIO(name, dir, os.Stdout, os.Stderr, args...) |
| 74 | +} |
| 75 | + |
| 76 | +// RunCommand runs a command |
| 77 | +func RunCommand(name string, arg ...string) (err error) { |
| 78 | + return RunCommandInDir(name, "", arg...) |
| 79 | +} |
| 80 | + |
| 81 | +// RunCommandWithSudo runs a command with sudo |
| 82 | +func RunCommandWithSudo(name string, args ...string) (err error) { |
| 83 | + newArgs := make([]string, 0) |
| 84 | + newArgs = append(newArgs, name) |
| 85 | + newArgs = append(newArgs, args...) |
| 86 | + return RunCommand("sudo", newArgs...) |
| 87 | +} |
| 88 | + |
| 89 | +func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) { |
| 90 | + var out []byte |
| 91 | + buf := make([]byte, 1024, 1024) |
| 92 | + for { |
| 93 | + n, err := r.Read(buf[:]) |
| 94 | + if n > 0 { |
| 95 | + d := buf[:n] |
| 96 | + out = append(out, d...) |
| 97 | + _, err := w.Write(d) |
| 98 | + if err != nil { |
| 99 | + return out, err |
| 100 | + } |
| 101 | + } |
| 102 | + if err != nil { |
| 103 | + // Read returns io.EOF at the end of file, which is not an error for us |
| 104 | + if err == io.EOF { |
| 105 | + err = nil |
| 106 | + } |
| 107 | + return out, err |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments