How to make System.cmd accept input

I’m trying to make a exs script that would execute a couple of mix phx.gen.live, however at some point, mix generator asks for a confirmation:

The fact two entities are related in the database does not mean they belong to the same context.
If you are not sure, prefer creating a new context over adding to the existing one.
Would you like to proceed? [Yn] Y

And this is where it’s actually stuck and not proceeding to accept the input. I’m using my wrapper to execute commands via cmd /c under Windows.

def cmd([_ | _] = what, options \\ []) do
    {cmd, args, options} =
      case :os.type() do
        {:win32, _} ->
          {"cmd", ["/c"] ++ what, options}

        #       _os_name
        #     ------------
        #       :darwin
        #       ...
        {:unix, _os_name} ->
          [head | tail] = what
          {head, tail, options}
      end

    stream = IO.binstream(:stdio, :line)
    options = Keyword.put_new(options, :into, stream)

    System.cmd(cmd, args, options)
  end

Is there anything else I might be missing?

You can always use a Port.open for these kinds of programs

Thank you, any chance you’ve got an example of how to handle stdin?

cmd is not designed streaming input/output. You can use ports, but it’s limited compared to the shell. Releated SO question: elixir - Use an OS process like a bash pipe: Send it STDIN and get its STDOUT - Stack Overflow

5 Likes

Thank you, Adam.
I’m assuming I lack :stdio forwarding as an input to the running process, and I have a feeling Elixir has no solution to this out of the box.