dot_env/env
Functions
pub fn get(key: String) -> Result(String, String)
Deprecated: Use `get_string` instead, this will be removed in the next release
Get an environment variable (supports both Erlang and JavaScript targets)
Example:
import dot_env/env
import gleam/io
import gleam/result
env.get("FOO")
|> result.unwrap("NOT SET")
|> io.println
pub fn get_bool(key: String) -> Result(Bool, String)
Get an environment variable as a boolean
Usage
import dot_env/env
import gleam/io
import gleam/result
fn main() {
env.get_bool("IS_DEBUG")
|> result.unwrap(False)
|> io.println
}
pub fn get_bool_or(key: String, default: Bool) -> Bool
Get an environment variable as a boolean or return a default value if it is not set
Usage
import dot_env/env
import gleam/io
fn main() {
let is_debug = env.get_bool_or("IS_DEBUG", True)
io.debug(is_debug)
}
pub fn get_int(key: String) -> Result(Int, String)
Get an environment variable as an integer (this is the same as calling get_string
and then parsing the Ok
value)
Usage
import dot_env/env
import gleam/io
import gleam/result
fn main() {
env.get_int("PORT")
|> result.unwrap(9000)
|> io.println
}
pub fn get_int_or(key: String, default: Int) -> Int
Get an environment variable as an integer or return a default value if it is not set
Usage
import dot_env/env
import gleam/io
fn main() {
let port = env.get_int_or("PORT", 9000)
io.debug(port)
}
pub fn get_or(key: String, default: String) -> String
Deprecated: Use `get_string_or` instead, this will be removed in the next release
Get an environment variable or return a default value if it is not set
pub fn get_string(key: String) -> Result(String, String)
Get an environment variable (supports both Erlang and JavaScript targets)
Usage
import dot_env/env
import gleam/io
import gleam/result
fn main() {
env.get_string("APP_NAME")
|> result.unwrap("app")
|> io.println
}
pub fn get_string_or(key: String, default: String) -> String
Get an environment variable or return a default value if it is not set
Usage
import dot_env/env
import gleam/io
fn main() {
let app_name = env.get_string_or("APP_NAME", "My App")
io.println(app_name)
}
pub fn get_then(
key: String,
next: fn(String) -> Result(a, String),
) -> Result(a, String)
An alternative implementation of get
that allows for chaining using use
statements and for early returns.
Usage
import dot_env/env
import gleam/io
fn main() {
use app_name <- env.get_then("APP_NAME")
io.println(app_name)
}