Command

Command

Represents a command to be executed in a child process.

{
    status : Result {} [ExitCode I32, KilledBySignal, IOError Str],
    stdout : List U8,
    stderr : List U8,
}

Output

Represents the output of a command.

{
    status : Result {} [ExitCode I32, KilledBySignal, IOError Str],
    stdout : List U8,
    stderr : List U8,
}

new : Str -> Command

Create a new command to execute the given program in a child process.

Command.new "sqlite3" # Execute "sqlite3" program

arg : Command, Str -> Command

Add a single argument to the command.

# Represent the command "ls -l"
Command.new "ls"
|> Command.arg "-l"

args : Command, List Str -> Command

Add multiple arguments to the command.

# Represent the command "ls -l -a"
Command.new "ls"
|> Command.args ["-l", "-a"]

env : Command, Str, Str -> Command

Add a single environment variable to the command.

# Run "env" and add the environment variable "FOO" with value "BAR"
Command.new "env"
|> Command.env "FOO" "BAR"

envs : Command, List ( Str, Str ) -> Command

Add multiple environment variables to the command.

# Run "env" and add the variables "FOO" and "BAZ"
Command.new "env"
|> Command.envs [("FOO", "BAR"), ("BAZ", "DUCK")]

clearEnvs : Command -> Command

Clear all environment variables, and prevent inheriting from parent, only the environment variables provided to command are available to the child.

# Represents "env" with only "FOO" environment variable set
Command.new "env"
|> Command.clearEnvs
|> Command.env "FOO" "BAR"

output : Command -> Task Output *

Execute command and capture stdout and stderr

Stdin is not inherited from the parent and any attempt by the child process to read from the stdin stream will result in the stream immediately closing.

output =
    Command.new "sqlite3"
    |> Command.arg dbPath
    |> Command.arg ".mode json"
    |> Command.arg "SELECT id, task, status FROM todos;"
    |> Command.output!

when output.status is
    Ok {} -> jsonResponse output.stdout
    Err _ -> byteResponse 500 output.stderr

status : Command -> Task {} Error

Execute command and inheriting stdin, stdout and stderr from parent

# Log request date, method and url using echo program
date = Utc.now |> Task.map! Utc.toIso8601Str
result =
    Command.new "echo"
    |> Command.arg "$(date) $(Http.methodToStr req.method) $(req.url)"
    |> Command.status
    |> Task.result!

when result is
    Ok {} -> respond "Command succeeded"
    Err (ExitCode code) -> respond "Command exited with code $(Num.toStr code)"
    Err (KilledBySignal) -> respond "Command was killed by signal"
    Err (IOError str) -> respond "IO Error: $(str)"