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
43
44
45
46
47
48
49
50
51
use std::fs;
use std::env;
use std::path::{Path, PathBuf};
/// Loads environment variables from a `.env` file in the current directory.
/// Returns the path to the loaded file on success.
pub fn dotenv() -> Result<PathBuf, std::io::Error> {
let path = Path::new(".env");
if !path.exists() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"File .env tidak ditemukan",
));
}
let content = fs::read_to_string(path)?;
for line in content.lines() {
let trimmed = line.trim();
// Skip empty lines or comments
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if let Some(pos) = trimmed.find('=') {
let key = trimmed[..pos].trim();
let val = trimmed[pos + 1..].trim();
// Strip optional quotes around value
let clean_val = if (val.starts_with('"') && val.ends_with('"'))
|| (val.starts_with('\'') && val.ends_with('\''))
{
if val.len() >= 2 {
&val[1..val.len() - 1]
} else {
""
}
} else {
val
};
// Hanya set jika variabel belum ada di environment sistem
if env::var(key).is_err() {
unsafe {
env::set_var(key, clean_val);
}
}
}
}
Ok(path.to_path_buf())
}