gleamyshell

Types

Represents the reason why the execution of a command was aborted.

Most of these reasons are common POSIX errors.

pub type AbortReason {
  Enomem
  Eagain
  Enametoolong
  Emfile
  Enfile
  Eacces
  Enoent
  Other(String)
}

Constructors

  • Enomem

    Not enough memory.

  • Eagain

    Resource temporarily unavailable.

  • Enametoolong

    Too long file name.

  • Emfile

    Too many open files.

  • Enfile

    File table overflow.

  • Eacces

    Insufficient permissions.

  • Enoent

    No such file or directory.

  • Other(String)

    An error not represented by the other options.

Represents information about why a command execution failed.

pub type CommandError {
  Failure(output: String, exit_code: Int)
  Abort(reason: AbortReason)
}

Constructors

  • Failure(output: String, exit_code: Int)

    The command could be executed but returned a non-zero exit code.

  • Abort(reason: AbortReason)

    The command did not execute and instead was aborted.

Functions

pub fn cwd() -> Option(String)

Returns the current working directory.

This function returns an Option because it can fail on Unix-like systems in rare circumstances.

Example

case gleamyshell.cwd() {
  Some(working_directory) -> io.println("Current working directory: " <> working_directory)
  None -> io.println("Couldn't detect the current working directory.")
}
pub fn execute(
  command command: String,
  args args: List(String),
) -> Result(String, CommandError)

Executes the given command with arguments.

Example

let result = gleamyshell.execute("whoami", [])

case result {
  Ok(username) -> io.println("Hello there, " <> string.trim(username) <> "!")
  Error(Failure(output, exit_code)) -> io.println("Whoops!\nError (" <> int.to_string(exit_code) <> "): " <> string.trim(output))
  Error(Abort(_)) -> io.println("Something went terribly wrong.")
}

This function can also be invoked by using labelled arguments.

let result = gleamyshell.execute("ls", args: ["-la"])
let another_result = gleamyshell.execute(command: "ls", args: ["-la"])
pub fn execute_in(
  command command: String,
  args args: List(String),
  working_directory working_directory: String,
) -> Result(String, CommandError)

Executes the given command with arguments in a specified working directory.

Example

let result = gleamyshell.execute_in("cat", [".bashrc"], "/home/username")

case result {
  Ok(file_content) -> io.println(file_content)
  Error(Failure(output, exit_code)) -> io.println("Whoops!\nError (" <> int.to_string(exit_code) <> "): " <> string.trim(output))
  Error(Abort(_)) -> io.println("Something went terribly wrong.")
}

This function can also be invoked by using labelled arguments.

let result = gleamyshell.execute_in("ls", args: ["-la"], working_directory: "/usr/bin")
let another_result = gleamyshell.execute_in(command: "ls", args: ["-la"], working_directory: "/usr/bin")
Search Document