summaryrefslogtreecommitdiffstats
path: root/golib/eows/eows-out.go
blob: 2a6b1106d4750cd30d7ef61cdc535cace08e9e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package eows

import (
	"bufio"
	"io"
	"strings"
)

// 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
	}
	return len(data), data, nil
}

// cmdPumpStdout is in charge to forward stdout in websocket
func (e *ExecOverWS) cmdPumpStdout(r io.Reader, done chan struct{}) {

	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(), "")
	}
	if sc.Err() != nil {
		e.logError("stdout scan: %v", sc.Err())
	}

	close(done)
}

// cmdPumpStderr is in charge to forward stderr in websocket
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())
	}
	if sc.Err() != nil {
		e.logError("stderr scan: %v", sc.Err())
	}
}