My application works with all kind of shell commands, provided from the console (curl
, date
, ping
, whatever). Now I'd like to cover the case with interactive shell commands (like mongo shell), using os/exec
.
e.g. as a first step, connect to mongodb:
mongo --quiet --host=localhost blog
then perform arbitrary number of commands, getting the result on every step
db.getCollection('posts').find({status:'INACTIVE'})
and then
exit
I tried the following, but it allows me to perform only one command per mongo connection:
func main() {
cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdin, _ := cmd.StdinPipe()
go func() {
defer stdin.Close()
io.WriteString(stdin, "db.getCollection('posts').find({status:'INACTIVE'}).itcount()")
// fails, if I'll do one more here
}()
cmd.Run()
cmd.Wait()
}
Is there a way to run multiple commands, getting stdout result per executed command?