Skip to content

Commit

Permalink
flush stderr earlier when not redirected
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Natel de Moura <tiago.natel@neoway.com.br>
  • Loading branch information
i4ki committed Jun 18, 2017
1 parent 4d32882 commit 6ad6f9d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions internal/sh/shell.go
Expand Up @@ -1822,7 +1822,7 @@ func (shell *Shell) concatElements(expr *ast.ConcatExpr) (string, error) {
return value, nil
}

func (shell *Shell) execCmdOutput(cmd ast.Node, ignoreError bool) ([]byte, []byte, sh.Obj, error) {
func (shell *Shell) execCmdOutput(cmd ast.Node, getstderr, ignoreError bool) ([]byte, []byte, sh.Obj, error) {
var (
outBuf, errBuf bytes.Buffer
err error
Expand All @@ -1837,7 +1837,9 @@ func (shell *Shell) execCmdOutput(cmd ast.Node, ignoreError bool) ([]byte, []byt

bkStdout, bkStderr := shell.stdout, shell.stderr
shell.SetStdout(&outBuf)
shell.SetStderr(&errBuf)
if getstderr {
shell.SetStderr(&errBuf)
}
defer func() {
shell.SetStdout(bkStdout)
shell.SetStderr(bkStderr)
Expand Down Expand Up @@ -1869,16 +1871,14 @@ func (shell *Shell) execCmdOutput(cmd ast.Node, ignoreError bool) ([]byte, []byt
return trimnl(outb), trimnl(errb), status, err
}

func (shell *Shell) executeExecAssignCmd(v ast.Node, local bool) error {
func (shell *Shell) executeExecAssignCmd(v ast.Node, where bool) error {
assign := v.(*ast.ExecAssignNode)
cmd := assign.Command()

outbuf, errbuf, status, cmdErr := shell.execCmdOutput(cmd, false)

// Only getting command output
// In this case the script must abort in case of errors
if len(assign.Names) == 1 {
shell.stderr.Write(errbuf) // flush stderr
outbuf, _, _, cmdErr := shell.execCmdOutput(cmd, false, false)
err := shell.setvar(assign.Names[0], sh.NewStrObj(string(outbuf)), Local)

if cmdErr != nil {
Expand All @@ -1894,7 +1894,7 @@ func (shell *Shell) executeExecAssignCmd(v ast.Node, local bool) error {

// Only getting stdout and exit status
if len(assign.Names) == 2 {
shell.stderr.Write(errbuf) // flush stderr
outbuf, _, status, _ := shell.execCmdOutput(cmd, false, false)
err := shell.setvar(assign.Names[0], sh.NewStrObj(string(outbuf)), Local)

if err != nil {
Expand All @@ -1910,6 +1910,7 @@ func (shell *Shell) executeExecAssignCmd(v ast.Node, local bool) error {
}

if len(assign.Names) == 3 {
outbuf, errbuf, status, _ := shell.execCmdOutput(cmd, true, false)
err1 := shell.setvar(assign.Names[0], sh.NewStrObj(string(outbuf)), Local)
err2 := shell.setvar(assign.Names[1], sh.NewStrObj(string(errbuf)), Local)
err3 := shell.setvar(assign.Names[2], status, Local)
Expand Down

0 comments on commit 6ad6f9d

Please sign in to comment.