From 6a938e20abc6c4b61683db447f912f67482f4779 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Fri, 22 Dec 2017 21:35:14 +0100 Subject: Added splitter method selection in eows lib. --- golib/eows/eows-out.go | 13 ++++++++++++- golib/eows/eows.go | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'golib/eows') diff --git a/golib/eows/eows-out.go b/golib/eows/eows-out.go index 3abbdc0..2a6b110 100644 --- a/golib/eows/eows-out.go +++ b/golib/eows/eows-out.go @@ -3,9 +3,10 @@ package eows import ( "bufio" "io" + "strings" ) -// scanBlocks +// scanBlocks - gain character by character (or as soon as one or more characters are available) func scanBlocks(data []byte, atEOF bool) (advance int, token []byte, err error) { if atEOF && len(data) == 0 { return 0, nil, nil @@ -20,7 +21,12 @@ func (e *ExecOverWS) cmdPumpStdout(r io.Reader, done chan struct{}) { }() sc := bufio.NewScanner(r) + + // else use default sc.ScanLines + if e.OutSplit == SplitChar { sc.Split(scanBlocks) + } + for sc.Scan() { e.OutputCB(e, sc.Text(), "") } @@ -37,7 +43,12 @@ func (e *ExecOverWS) cmdPumpStderr(r io.Reader) { defer func() { }() sc := bufio.NewScanner(r) + + // else use default sc.ScanLines + if e.OutSplit == SplitChar { sc.Split(scanBlocks) + } + for sc.Scan() { e.OutputCB(e, "", sc.Text()) } diff --git a/golib/eows/eows.go b/golib/eows/eows.go index 6fc3550..a5767ed 100644 --- a/golib/eows/eows.go +++ b/golib/eows/eows.go @@ -20,6 +20,16 @@ type EmitOutputCB func(e *ExecOverWS, stdout, stderr string) // EmitExitCB is the function callback used to emit exit proc code type EmitExitCB func(e *ExecOverWS, code int, err error) +// SplitType Type of spliting method to tokenize stdout/stderr +type SplitType uint8 + +const ( + // SplitLine Split line by line + SplitLine SplitType = iota + // SplitChar Split character by character + SplitChar +) + // Inspired by : // https://github.com/gorilla/websocket/blob/master/examples/command/main.go @@ -40,6 +50,7 @@ type ExecOverWS struct { OutputCB EmitOutputCB // stdout/stderr callback ExitCB EmitExitCB // exit proc callback UserData *map[string]interface{} // user data passed to callbacks + OutSplit SplitType // split method to tokenize stdout/stderr // Private fields proc *os.Process @@ -56,7 +67,8 @@ func New(cmd string, args []string, so *socketio.Socket, soID, cmdID string) *Ex SocketIO: so, Sid: soID, CmdID: cmdID, - CmdExecTimeout: -1, // default no timeout + CmdExecTimeout: -1, // default no timeout + OutSplit: SplitChar, // default split by character } cmdIDMap[cmdID] = e -- cgit 1.2.3-korg