summaryrefslogtreecommitdiffstats
path: root/golib/eows/eows-out.go
blob: 2d9bdb062aa622ad3a9e56d31622939ca3b2ca93 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package eows

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

// scanChars - gain character by character (or as soon as one or more characters are available)
func scanChars(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return len(data), data, nil
}

// _pumper is in charge to collect
func (e *ExecOverWS) _pumper(sc *bufio.Scanner, fctCB func(s string)) {

	// Select split function (default sc.ScanLines)
	if e.OutSplit == SplitChar || e.OutSplit == SplitLineTime || e.OutSplit == SplitTime {
		sc.Split(scanChars)
	}

	// Scan method according to split type
	if e.OutSplit == SplitLineTime || e.OutSplit == SplitTime {
		t0 := time.Now()
		buf := ""
		for sc.Scan() {
			buf += sc.Text()
			if time.Since(t0).Nanoseconds() > e.LineTimeSpan ||
				(e.OutSplit == SplitLineTime && strings.Contains(buf, "\n")) {
				fctCB(buf)
				buf = ""
				t0 = time.Now()
			}
			if e.procExited {
				break
			}
		}
		// Send remaining characters
		if len(buf) > 0 {
			fctCB(buf)
		}

	} else {

		for sc.Scan() {
			fctCB(sc.Text())
			if e.procExited {
				break
			}
		}
	}

}

// 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)

	e._pumper(sc, func(b string) {
		e.OutputCB(e, b, "")
	})

	e.logDebug("STDOUT pump exit")

	if sc.Err() != nil && !strings.Contains(sc.Err().Error(), "file already closed") {
		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)

	e._pumper(sc, func(b string) {
		e.OutputCB(e, "", b)
	})

	e.logDebug("STDERR pump exit")

	if sc.Err() != nil && !strings.Contains(sc.Err().Error(), "file already closed") {
		e.logError("stderr scan: %v", sc.Err())
	}
}