                      #out: :err | :buffer | pid | {:file, ...},
                      #err: :out | :buffer | pid | {:file, ...})
 
-#p = Porc.call("cat", in: "Hello world!")
+#Porc.call("cat", in: "Hello world!")
 # ==>
 #p = Port.open({:spawn_executable, '/usr/local/bin/go'}, [{:args, ["run", "main.go", "cat"]}, :binary, {:packet, 2}, :exit_status])
 

    end
  end

  defp split(args) when is_binary(args) do
    String.split args, " "
  end

  defp open_port(opts) do
    go = :os.find_executable 'go'
    Port.open { :spawn_executable, go }, opts
  end

  defp init_port_connection(cmd, args, options) do
    port = open_port(port_options(options, cmd, args))

    input  = process_input_opts(port, options[:in])
    output = process_output_opts(port, options[:out])
    error  = process_error_opts(port, options[:err])

    { port, input, output, error }
  end

  @doc """

    port_opts
  end

  defp open_port(opts) do
    go = :os.find_executable 'go'
    Port.open { :spawn_executable, go }, opts
  end

  # Processes port options opens a port. Used in both call() and spawn()
  defp init_port_connection(cmd, args, options) do
    port = open_port(port_options(options, cmd, args))

    input  = process_input_opts(options[:in])
    output = process_output_opts(options[:out])
    error  = process_error_opts(options[:err])

    { port, input, output, error }
  end

  defp process_input_opts(opt) do
    case opt do
      nil                     -> nil
      { :file, fid }          -> { :file, fid }
      { pid, ref }            -> { pid, ref }
      bin when is_binary(bin) -> bin
    end
  end

  defp process_output_opts(opt) do
    case opt do
      :err                          -> nil
      nil                           -> { :buffer, "" }
      :buffer                       -> { :buffer, "" }
      { :file, fid }                -> { :file, fid }
      { pid, ref } when is_pid(pid) -> { pid, ref }
    end
  end

  defp process_error_opts(opt) do
    case opt do
      :out                          -> nil
      nil                           -> { :buffer, "" }
      :buffer                       -> { :buffer, "" }
      { :file, fid }                -> { :file, fid }
      { pid, ref } when is_pid(pid) -> { pid, ref }
    end
  end
end

