-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: os/exec: add CombinedOutputPipe function #27611
Comments
Is https://golang.org/pkg/io/#MultiReader not enough? Granted that you may have to handle closing separately, but it seems to me like most users won't need to do that. For most use cases, simply using |
/cc @bradfitz for thoughts. We already have StdoutPipe and StderrPipe so it doesn't seem unreasonable to ask for a "both pipe" especially since "CombinedOutput" exists. Ironically I believe the pre-Go1 equivalent did support this better. You set Stdout to Pipe (an enum) and Stderr to "SameAsStdout" or something like that. |
Seems easy enough to do yourself if that's what you want: package main
import (
"io"
"log"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("perl", "-e", `
$| = 1;
print "hi\n";
print STDERR "warn: foo\n";
print "bye.\n"
`)
out, _ := cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout // <======= this.
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
io.Copy(os.Stdout, out)
} |
I was thinking that CombinedOutputPipe would be analogous to CombinedOutput, which would mean you wouldn't have to write Start, Wait, etc, but of course if you do that then nothing is reading from the pipe. So a "CombinedOutputPipe" analogous to "CombinedOutput" makes no sense. The best you can do is a BothPipe() that turns the two lines in Brad's example to one. That doesn't seem worth its weight. |
CombinedOutputPipe returns a pipe that will be connected to the command's standard output and standard error, so that developers can access both standard output and standard error messages.
I have an implementation and I'd be happy to submit.
The text was updated successfully, but these errors were encountered: