-
Notifications
You must be signed in to change notification settings - Fork 3
/
console.go
50 lines (41 loc) · 848 Bytes
/
console.go
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
package wrapper
import (
"bufio"
"fmt"
)
type Console interface {
Start() error
Kill() error
WriteCmd(string) error
ReadLine() (string, error)
}
type defaultConsole struct {
cmd JavaExec
stdout *bufio.Reader
stdin *bufio.Writer
}
func newConsole(cmd JavaExec) *defaultConsole {
c := &defaultConsole{
cmd: cmd,
}
c.stdout = bufio.NewReader(cmd.Stdout())
c.stdin = bufio.NewWriter(cmd.Stdin())
return c
}
func (c *defaultConsole) Start() error {
return c.cmd.Start()
}
func (c *defaultConsole) Kill() error {
return c.cmd.Kill()
}
func (c *defaultConsole) WriteCmd(cmd string) error {
wrappedCmd := fmt.Sprintf("%s\r\n", cmd)
_, err := c.stdin.WriteString(wrappedCmd)
if err != nil {
return err
}
return c.stdin.Flush()
}
func (c *defaultConsole) ReadLine() (string, error) {
return c.stdout.ReadString('\n')
}