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
OtherAbortReason(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.
-
OtherAbortReason(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.
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.
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 env(identifier: String) -> Option(String)
Returns the value of the given environment variable when it is set.
Example
case gleamyshell.env("JAVA_HOME") {
Some(dir) -> io.println("Java runtime location: " <> dir)
None ->
io.println("The location of the Java runtime could not be found.")
}
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",
)
pub fn home_directory() -> Option(String)
Returns the home directory of the current user.
This function returns an Option
because it can fail in rare circumstances.
Example
case gleamyshell.home_directory() {
Some(home_directory) -> io.println("Home directory: " <> home_directory)
None ->
io.println("Couldn't detect the home directory of the current user.")
}
pub fn os() -> OsFamily
Returns information about the host’s operating system.
This function is meant to be a quality-of-life feature where someone needs to execute different shell commands that differ depending on the operating system.
Example
case gleamyshell.os() {
Windows -> io.println("Doing stuff on Windows.")
Unix(_) -> io.println("Doing stuff on a Unix(-like) system.")
}