Skip to content

Commit 20725c6

Browse files
authored
Merge pull request #200 from go-task/fix-output-issues
Fixes some bugs relatated to commands output handling
2 parents 659fd2a + 9061322 commit 20725c6

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

internal/output/group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type Group struct{}
99

10-
func (Group) WrapWriter(w io.Writer, _ string) io.WriteCloser {
10+
func (Group) WrapWriter(w io.Writer, _ string) io.Writer {
1111
return &groupWriter{writer: w}
1212
}
1313

internal/output/interleaved.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import (
66

77
type Interleaved struct{}
88

9-
func (Interleaved) WrapWriter(w io.Writer, _ string) io.WriteCloser {
10-
return nopWriterCloser{w: w}
11-
}
12-
13-
type nopWriterCloser struct {
14-
w io.Writer
15-
}
16-
17-
func (wc nopWriterCloser) Write(p []byte) (int, error) {
18-
return wc.w.Write(p)
19-
}
20-
21-
func (wc nopWriterCloser) Close() error {
22-
return nil
9+
func (Interleaved) WrapWriter(w io.Writer, _ string) io.Writer {
10+
return w
2311
}

internal/output/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
)
66

77
type Output interface {
8-
WrapWriter(w io.Writer, prefix string) io.WriteCloser
8+
WrapWriter(w io.Writer, prefix string) io.Writer
99
}

internal/output/output_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package output_test
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"testing"
78

89
"github.com/go-task/task/v2/internal/output"
@@ -24,7 +25,7 @@ func TestInterleaved(t *testing.T) {
2425
func TestGroup(t *testing.T) {
2526
var b bytes.Buffer
2627
var o output.Output = output.Group{}
27-
var w = o.WrapWriter(&b, "")
28+
var w = o.WrapWriter(&b, "").(io.WriteCloser)
2829

2930
fmt.Fprintln(w, "foo\nbar")
3031
assert.Equal(t, "", b.String())
@@ -37,7 +38,7 @@ func TestGroup(t *testing.T) {
3738
func TestPrefixed(t *testing.T) {
3839
var b bytes.Buffer
3940
var o output.Output = output.Prefixed{}
40-
var w = o.WrapWriter(&b, "prefix")
41+
var w = o.WrapWriter(&b, "prefix").(io.WriteCloser)
4142

4243
t.Run("simple use cases", func(t *testing.T) {
4344
b.Reset()

internal/output/prefixed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
type Prefixed struct{}
1111

12-
func (Prefixed) WrapWriter(w io.Writer, prefix string) io.WriteCloser {
12+
func (Prefixed) WrapWriter(w io.Writer, prefix string) io.Writer {
1313
return &prefixWriter{writer: w, prefix: prefix}
1414
}
1515

task.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,18 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
248248

249249
stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix)
250250
stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix)
251-
defer stdOut.Close()
252-
defer stdErr.Close()
251+
defer func() {
252+
if _, ok := stdOut.(*os.File); !ok {
253+
if closer, ok := stdOut.(io.Closer); ok {
254+
closer.Close()
255+
}
256+
}
257+
if _, ok := stdErr.(*os.File); !ok {
258+
if closer, ok := stdErr.(io.Closer); ok {
259+
closer.Close()
260+
}
261+
}
262+
}()
253263

254264
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
255265
Command: cmd.Cmd,

0 commit comments

Comments
 (0)