You will use
pooka/process:process.run(command, args)Output, withstatus,stdoutandstderr- Guards to treat a non-zero status as a failure
The whole program
module main
import pooka/int
import pooka/io
import pooka/process
otp_version() -> Result(String, String) {
let script = "io:format(\"~s\", [erlang:system_info(otp_release)]), halt()."
match process.run("erl", ["-noshell", "-eval", script]) {
Ok(output) if output.status == 0 -> Ok(output.stdout)
Ok(output) -> Error(
"erl exited with status " + int.to_string(output.status)
)
Error(problem) -> Error(problem.message)
}
}
main() {
match otp_version() {
Ok(version) -> {
io.println("Erlang/OTP " + version)
Nil
}
Error(reason) -> {
io.eprintln("Could not ask Erlang: " + reason)
Nil
}
}
match process.run("erl", ["-noshell", "-eval", "halt(3)."]) {
Ok(output) -> {
io.println("halt(3) exits with " + int.to_string(output.status))
Nil
}
Error(problem) -> {
io.eprintln(problem.message)
Nil
}
}
match process.run("no-such-command", []) {
Ok(_) -> Nil
Error(problem) -> {
io.println("no-such-command: " + problem.message)
Nil
}
}
}
Run it
$ pooka run
Erlang/OTP 29
halt(3) exits with 3
no-such-command: command not found
How it works
- A command and its arguments.
process.run("erl", ["-noshell", "-eval", script])runserlwith those arguments. They are passed along as they are, never interpreted by a shell, so spaces, quotes and*arrive exactly as written. - Two kinds of trouble. If the program cannot be started at all, you get
Error(ProcessError). If it starts and fails, you getOk(output)with a non-zerooutput.status. - Decide what success means. The guard
Ok(output) if output.status == 0accepts only a clean exit; the next arm turns any other status into an error message. - Output is text.
stdoutandstderrare strings, ready forstring.trimandstring.split.
Try next: process.run_in(command, args, folder) runs a command in another folder, and process.run_with_timeout gives up after a while.