I'm using golang pty library for interactive shell console, where user is able to run any kind of commands.
The problem is that I can't find a good way to separate user's input and shell's output, because Stdin, Stdout and Stderr are assigned to the same file.
Here is an example:
func main() {
cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost test")
f, _ := pty.Start(cmd)
cmd.Start()
time.Sleep(1 * time.Second) // some time to start
for ;; {
io.WriteString(f, "sleep(1000);Date.now();
")
stdoutScanner := bufio.NewScanner(f)
go func() {
for stdoutScanner.Scan() {
text := stdoutScanner.Text()
println(text)
}
}()
time.Sleep(1200 * time.Millisecond)
}
}
This code produces output like that:
> sleep(1000);Date.now();sleep(1000);Date.now();
1559185677551
sleep(1000);Date.now();
1559185679053
1559185691069
now();
1559185692573
);Date.now();sleep(1000);Date.now();
1559185694074
ep(1000);Date.now();sleep(1000);Date.now();
What I need is just
1559185677551
1559185679053
1559185691069
1559185692573
1559185694074
I can filter out all scanned text which contains
user input, but that does not work stable - in the example you can find pieces like ep(1000);Date.now();
Is there a better solution?