-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv.gleam
42 lines (33 loc) · 816 Bytes
/
dotenv.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//// Support for `.env` files.
import gleam/dict
import gleam/list
import gleam/result.{try}
import glenvy/error.{type Error}
import glenvy/internal/file
import glenvy/internal/os
import glenvy/internal/parser
/// Loads the `.env` file.
pub fn load() -> Result(Nil, Error) {
load_from(".env")
}
/// Loads the file at the specified path as a `.env` file.
pub fn load_from(path filepath: String) -> Result(Nil, Error) {
use env_file <- try(find(filepath))
let env_vars =
env_file
|> parser.parse_env_file
env_vars
|> dict.to_list
|> list.each(fn(env_var) {
let #(key, value) = env_var
os.set_env(key, value)
})
Ok(Nil)
}
fn find(filepath: String) -> Result(String, Error) {
use contents <- try(
file.read(filepath)
|> result.map_error(error.Io),
)
Ok(contents)
}