RunCommandSplit runs a command with a supplied environment and optional arguments and returns the resulting stdout and stderr output as separate variables. If the supplied environment is nil then the default environment is used. If the command fails to start or returns a non-zero exit code then an e
(ctx context.Context, env []string, filesInherit []*os.File, name string, arg ...string)
| 58 | // the default environment is used. If the command fails to start or returns a non-zero exit code |
| 59 | // then an error is returned containing the output of stderr too. |
| 60 | func RunCommandSplit(ctx context.Context, env []string, filesInherit []*os.File, name string, arg ...string) (string, string, error) { |
| 61 | cmd := exec.CommandContext(ctx, name, arg...) |
| 62 | |
| 63 | if env != nil { |
| 64 | cmd.Env = env |
| 65 | } |
| 66 | |
| 67 | if filesInherit != nil { |
| 68 | cmd.ExtraFiles = filesInherit |
| 69 | } |
| 70 | |
| 71 | var stdout bytes.Buffer |
| 72 | var stderr bytes.Buffer |
| 73 | cmd.Stdout = &stdout |
| 74 | cmd.Stderr = &stderr |
| 75 | |
| 76 | err := cmd.Run() |
| 77 | if err != nil { |
| 78 | return stdout.String(), stderr.String(), NewRunError(name, arg, err, &stdout, &stderr) |
| 79 | } |
| 80 | |
| 81 | return stdout.String(), stderr.String(), nil |
| 82 | } |
| 83 | |
| 84 | // RunCommandContext runs a command with optional arguments and returns stdout. If the command fails to |
| 85 | // start or returns a non-zero exit code then an error is returned containing the output of stderr. |
no test coverage detected
searching dependent graphs…