Skip to content

Commit 6f93998

Browse files
Merge pull request #48 from greut/flush-then-close
fix: last line is never sent
2 parents 75bd92d + b66c7d8 commit 6f93998

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cmd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ func (c *Cmd) run(in io.Reader) {
334334
// who's waiting for us to close them.
335335
if c.stdoutStream != nil {
336336
defer func() {
337+
c.stdoutStream.Flush()
338+
c.stderrStream.Flush()
337339
// exec.Cmd.Wait has already waited for all output:
338340
// Otherwise, during the execution of the command a separate goroutine
339341
// reads from the process over a pipe and delivers that data to the
@@ -643,3 +645,11 @@ func (rw *OutputStream) SetLineBufferSize(n int) {
643645
rw.bufSize = n
644646
rw.buf = make([]byte, rw.bufSize)
645647
}
648+
649+
// Flush empties the buffer of its last line.
650+
func (rw *OutputStream) Flush() {
651+
if rw.lastChar > 0 {
652+
line := string(rw.buf[0:rw.lastChar])
653+
rw.streamChan <- line
654+
}
655+
}

cmd_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func TestStreamingMultipleLines(t *testing.T) {
560560
}
561561

562562
// Write two short lines
563-
input := "foo\nbar\n"
563+
input := "foo\nbar"
564564
n, err := out.Write([]byte(input))
565565
if n != len(input) {
566566
t.Errorf("Write n = %d, expected %d", n, len(input))
@@ -582,6 +582,8 @@ func TestStreamingMultipleLines(t *testing.T) {
582582
t.Errorf("got line: '%s', expected 'foo'", gotLine)
583583
}
584584

585+
out.Flush()
586+
585587
// Get next line
586588
select {
587589
case gotLine = <-lines:

0 commit comments

Comments
 (0)