Skip to content

Instantly share code, notes, and snippets.

@alco
Last active June 28, 2023 19:39
Show Gist options
  • Save alco/10992110 to your computer and use it in GitHub Desktop.
Save alco/10992110 to your computer and use it in GitHub Desktop.
def open_port(executable, args) do
Port.open({:spawn_executable, executable}, [:stream, :binary, {:args, args}, :use_stdio, :stderr_to_stdout, :exit_status])
end
def process_port(port) do
case collect_output(InternalData[port: port], []) do
{status, data} -> Result[status: status, output: to_string(data)]
end
end
defp collect_output(InternalData[port: port]=idata, data) do
receive do
{^port, {:data, new_data}} ->
data = [data, new_data]
collect_output(idata, data)
{^port, {:exit_status, status}} ->
{status, data}
end
end
port = open_port("myscript.sh", [])
result = process_port(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment