Skip to content
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

Closed
Dliv3 opened this issue Sep 11, 2018 · 4 comments
Closed

proposal: os/exec: add CombinedOutputPipe function #27611

Dliv3 opened this issue Sep 11, 2018 · 4 comments

Comments

@Dliv3
Copy link

Dliv3 commented Sep 11, 2018

func (c *Cmd) CombinedOutputPipe() (io.ReadCloser, error) 

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.

@gopherbot gopherbot added this to the Proposal milestone Sep 11, 2018
@mvdan
Copy link
Member

mvdan commented Sep 11, 2018

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 cmd.Wait() is enough, without needing to close the stdout/stderr pipes.

@ianlancetaylor ianlancetaylor changed the title proposal: os/exec add CombinedOutputPipe function proposal: os/exec: add CombinedOutputPipe function Sep 12, 2018
@rsc
Copy link
Contributor

rsc commented Sep 19, 2018

/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.

@bradfitz
Copy link
Contributor

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)
}

@rsc
Copy link
Contributor

rsc commented Oct 3, 2018

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.

@rsc rsc closed this as completed Oct 3, 2018
@golang golang locked and limited conversation to collaborators Oct 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants