Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ type Options struct {
// with CombinedOutput.
Streaming bool

// If Streaming is true and SplitChar is set, Cmd.Stdout and Cmd.Stderr
// channels will be split using this char instead of the default '\n'.
SplitChar *byte
// BeforeExec is a list of functions called immediately before starting
// the real command. These functions can be used to customize the underlying
// os/exec.Cmd. For example, to set SysProcAttr. If Stop is called while
Expand Down Expand Up @@ -219,6 +222,12 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd {

c.Stderr = make(chan string, DEFAULT_STREAM_CHAN_SIZE)
c.stderrStream = NewOutputStream(c.Stderr)

if options.SplitChar != nil {
c.stdoutStream.splitChar = *options.SplitChar
c.stderrStream.splitChar = *options.SplitChar
}

c.stderrStream.SetLineBufferSize(int(options.LineBufferSize))
}

Expand Down Expand Up @@ -698,6 +707,7 @@ type OutputStream struct {
bufSize int
buf []byte
lastChar int
splitChar byte
}

// NewOutputStream creates a new streaming output on the given channel. The
Expand All @@ -707,9 +717,10 @@ func NewOutputStream(streamChan chan string) *OutputStream {
out := &OutputStream{
streamChan: streamChan,
// --
bufSize: DEFAULT_LINE_BUFFER_SIZE,
buf: make([]byte, DEFAULT_LINE_BUFFER_SIZE),
lastChar: 0,
bufSize: DEFAULT_LINE_BUFFER_SIZE,
buf: make([]byte, DEFAULT_LINE_BUFFER_SIZE),
lastChar: 0,
splitChar: '\n',
}
return out
}
Expand All @@ -725,7 +736,7 @@ func (rw *OutputStream) Write(p []byte) (n int, err error) {
// can contain multiple lines, like "foo\nbar". So in that case nextLine
// will be 0 ("foo\nbar\n") then 4 ("bar\n") on next iteration. And i
// will be 3 and 7, respectively. So lines are [0:3] are [4:7].
newlineOffset := bytes.IndexByte(p[firstChar:], '\n')
newlineOffset := bytes.IndexByte(p[firstChar:], rw.splitChar)
if newlineOffset < 0 {
break // no newline in stream, next line incomplete
}
Expand Down
Loading