-3

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?

silent-box
  • 1,649
  • 3
  • 21
  • 40
  • 6
    You shouldn't use os/exec for this. You should use a mongo driver, to talk to mongodb directly. – Jonathan Hall Apr 02 '19 at 13:07
  • Possible duplicate of https://stackoverflow.com/q/39735171/13860 – Jonathan Hall Apr 02 '19 at 13:09
  • Possible duplicate of https://stackoverflow.com/q/27322722/13860 – Jonathan Hall Apr 02 '19 at 13:10
  • @Flimzy I can't use any driver, my application should not know about the provided commands. It can be mongo, mysql, postgres, whatever. – silent-box Apr 02 '19 at 14:10
  • Then you should use drivers for each of those. Your application already has to know about them, if it knows enough to know which commands to send to the os.exec invocation. – Jonathan Hall Apr 02 '19 at 14:35
  • I understand how strange it looks, but it is indeed what I need. My application works with arbitrary shell commands, provided in console (all kinds of stuff - `curl`, `date`, `cat`, anything). Right now I'm covering the case to work with interactive shell commands - databases are just an example – silent-box Apr 02 '19 at 14:43

1 Answers1

1

As Flimzy noted, you should absolutely be using a mongo driver to work with mongo, not trying to interact with it via shell exec.

However, to answer the root question, of course you can execute multiple commands - there's no reason you can't. Every time you write to the process' stdin, it's like you're at a terminal typing into it. There's no secret limitation on that, other than processes which specifically detect if they're connected to a TTY.

Your code has several issues, though - you should definitely review the os/exec package documentation. You're calling cmd.Run, which:

starts the specified command and waits for it to complete.

And then calling cmd.Wait, which... also waits for the command to complete. You're writing to the stdin pipe in a goroutine, even though this is a very serialized process: you want to write to the pipe to execute a command, get the result, write another command, get another result... concurrency only muddles matters and should not be used here. And you're not sending newlines to tell Mongo you're done writing a command (just like you'd do in the shell - Mongo won't just start executing as soon as you enter the closing paren, you have to hit enter).

What you would want to do to interact with a process via stdin/stdout (again, noting that this is absolutely not the way to interact with a database, but could be valid for other external commands):

cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

stdin, _ := cmd.StdinPipe()

// Start command but don't wait for it to exit (yet) so we can interact with it
cmd.Start()

// Newlines, like hitting enter in a terminal, tell Mongo you're done writing a command
io.WriteString(stdin, "db.getCollection('posts').find({status:'INACTIVE'}).itcount()\n")
io.WriteString(stdin, "db.getCollection('posts').find({status:'ACTIVE'}).itcount()\n")

// Quit tells it you're done interacting with it, otherwise it won't exit
io.WriteString(stdin, "quit()\n")

stdin.Close()

// Lastly, wait for the process to exit
cmd.Wait()
Adrian
  • 42,911
  • 6
  • 107
  • 99
  • 1
    Some programs don't exit until their standard input is closed, so it is usually safer to call stdin.Close before cmd.Wait. Not sure how mongo behaves, though. The explicit quit command may be enough in this case. – Peter Apr 02 '19 at 14:52
  • My understanding was that `stdin.Close` would only close the `io.Pipe` but not actually close the underlying stdin fd, please correct me if I'm wrong though. – Adrian Apr 02 '19 at 15:19
  • @Adrian Thank you very much, that's exactly what I need. As I mentioned above, my app is the console utility, and the mongo command is just a regular user input for it - that's why I can't use any particular DB driver (and the DB interaction is just an example - user can provide any type of command, I just want to cover the case with interactive shell) – silent-box Apr 02 '19 at 16:24
  • "he pipe will be closed automatically after Wait sees the command exit. A caller need only call Close to force the pipe to close sooner. For example, if the command being run will not exit until standard input is closed, the caller must close the pipe." https://golang.org/pkg/os/exec/#Cmd.StdinPipe – Peter Apr 02 '19 at 16:29
  • I guess it just seemed kind of ambiguous to me when I read it at first but yeah you're right. I've updated the code with your suggestion. – Adrian Apr 02 '19 at 16:40