gleamyshell

Types

Represents information about the exit code and output of the executed command.

output equals the output stream (stdout) when the command exited with an exit code of one, otherwise it equals the error stream (stderr).

pub type CommandOutput {
  CommandOutput(exit_code: Int, output: String)
}

Constructors

  • CommandOutput(exit_code: Int, output: String)

Represents names of operating systems.

pub type Os {
  Darwin
  FreeBsd
  OpenBsd
  Linux
  SunOs
  OtherOs(String)
}

Constructors

  • Darwin

    The Unix operating system used by Apple as a core for its operating systems (e.g., macOS).

  • FreeBsd

    A free Unix-like operating system descended from AT&T’s UNIX.

  • OpenBsd

    A free Unix-like operating system forked from NetBSD.

  • Linux

    The Linux kernel is the base for many Unix-like operating systems like Debian.

  • SunOs

    The Unix-like operating system SunOS is used as a core for other distributions like Solaris.

  • OtherOs(String)

    An operating system not represented by the other options.

Represents families of operating systems.

pub type OsFamily {
  Unix(Os)
  Windows
}

Constructors

  • Unix(Os)

    The operating system is part of the Unix family.

  • Windows

    The operating system is part of the Windows family.

Functions

pub fn cwd() -> Result(String, Nil)

Returns the current working directory.

Example

case gleamyshell.cwd() {
  Ok(working_directory) ->
    io.println("Current working directory: " <> working_directory)
  Error(_) -> io.println("Couldn't detect the current working directory.")
}
pub fn env(identifier: String) -> Result(String, Nil)

Returns the value of the given environment variable if it is set.

Example

case gleamyshell.env("JAVA_HOME") {
  Ok(dir) -> io.println("Java runtime location: " <> dir)
  Error(_) -> 
    io.println("The location of the Java runtime could not be found.")
}
pub fn execute(
  executable: String,
  in working_directory: String,
  args args: List(String),
) -> Result(CommandOutput, String)

Executes the given command with arguments.

Example

case gleamyshell.execute("whoami", in: ".", args: []) {
  Ok(CommandOutput(0, username)) ->
    io.println("Hello there, " <> string.trim(username) <> "!")
  Ok(CommandOutput(exit_code, output)) ->
    io.println(
      "Whoops!\nError ("
      <> int.to_string(exit_code)
      <> "): "
      <> string.trim(output),
    )
  Error(reason) -> io.println("Fatal: " <> reason)
}
pub fn home_directory() -> Result(String, Nil)

Returns the home directory of the current user.

Example

case gleamyshell.home_directory() {
  Ok(home_directory) -> io.println("Home directory: " <> home_directory)
  Error(_) ->
    io.println("Couldn't detect the home directory of the current user.")
}
pub fn os() -> OsFamily

Returns information about the host’s operating system.

Example

case gleamyshell.os() {
  Windows -> io.println("Doing stuff on Windows.")
  Unix(_) -> io.println("Doing stuff on a Unix(-like) system.")
}
pub fn which(executable: String) -> Result(String, Nil)

Returns the location of the given executable if it could be found.

Example

case gleamyshell.which("git") {
  Ok(_) -> io.println("Doing something with Git.")
  Error(_) -> io.println("Git could not be found.")
}
Search Document