I'm trying to use different shell commands for a console go application, and for some reason the behavior is different for the following interactive shells.
This code prints result of a mongoDB query:
cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")
stdout, _ := cmd.StdoutPipe()
stdin, _ := cmd.StdinPipe()
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
println(stdoutScanner.Text())
}
}()
cmd.Start()
io.WriteString(stdin, "db.getCollection('posts').find({status:'ACTIVE'}).itcount()
")
//can't finish command, need to reuse it for other queries
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)
But the same code for Neo4J shell does not print anything:
cmd := exec.Command("sh", "-c", "cypher-shell -u neo4j -p 121314 --format plain")
stdout, _ := cmd.StdoutPipe()
stdin, _ := cmd.StdinPipe()
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
println(stdoutScanner.Text())
}
}()
cmd.Start()
io.WriteString(stdin, "match (n) return count(n);
")
//can't finish the command, need to reuse it for other queries
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)
What is the difference? How can I make the second one work? (without closing the command)
P.S
Neo4J works fine when I print directly to os.Stdout
:
cmd := exec.Command("sh", "-c", "cypher-shell -u neo4j -p 121314 --format plain")
cmd.Stdout = os.Stdout
stdin, _ := cmd.StdinPipe()
cmd.Start()
io.WriteString(stdin, "match (n) return count(n);
")
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)