shellout

A Gleam wrapper for Elixir.System.cmd/3

Types

pub type CmdOpt {
  Into(List(Nil))
  Cd(String)
  Env(List(tuple(String, String)))
  Arg0(String)
  StderrToStdout(Bool)
  Parallelism(Bool)
}

Constructors

  • Into(List(Nil))
  • Cd(String)
  • Env(List(tuple(String, String)))
  • Arg0(String)
  • StderrToStdout(Bool)
  • Parallelism(Bool)
pub type CmdResult =
  Result(tuple(List(String), Int), String)

Functions

pub fn cmd(
  bin command: String,
  args args: List(String),
  opts opts: List(CmdOpt),
) -> Result(tuple(List(String), Int), String)

Executes the given command with args.

command is expected to be an executable available in PATH unless an absolute path is given.

args must be a List(String) which the executable will receive as its arguments as is. This means that:

  • environment variables will not be interpolated
  • wildcard expansion will not happen
  • arguments do not need to be escaped or quoted for shell safety

This function returns a Result, where success is a tuple(List(String), Int) containing a List of lines collected from stdout, and the command exit status.

Examples

> shellout.cmd("printf", ["%s\n", "hi"], [])
Ok(tuple(["hi\n"], 0))

> let options = [Env([tuple("MIX_ENV", "test")])]
> shellout.cmd("printf", ["%s\n", "hi"], options)
Ok(tuple(["hi\n"], 0))

> shellout.cmd("", [], [StderrToStdout(True)])
Error("Error: Could not execute ``\n`` does not exist")

Options

  • Cd(String) - the directory to run the command in
  • Env(List(tuple(String, String))) - Tuples contain environment key-value Strings. The child process inherits all environment variables from its parent process, the Gleam application, except those overwritten or cleared using this option. Specify a value of Nil to clear (unset) an environment variable, which is useful for preventing credentials passed to the application from leaking into child processes.
  • Arg0(String) - sets the command arg0
  • StderrToStdout(Bool) - redirects stderr to stdout when True
  • Parallelism(Bool) - when True, the VM will schedule port tasks to improve parallelism in the system. If set to False, the VM will try to perform commands immediately, improving latency at the expense of parallelism. The default can be set on system startup by passing the “+spp” argument to --erl.

Documentation adapted from Elixir.System.cmd/3