2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2018-03-05 13:21:35 -08:00
|
|
|
|
2020-07-19 05:01:46 -07:00
|
|
|
pub(crate) fn load_dotenv(
|
2021-03-30 17:30:32 -07:00
|
|
|
config: &Config,
|
2021-03-28 22:38:07 -07:00
|
|
|
settings: &Settings,
|
2021-03-30 17:30:32 -07:00
|
|
|
working_directory: &Path,
|
2020-07-19 05:01:46 -07:00
|
|
|
) -> RunResult<'static, BTreeMap<String, String>> {
|
|
|
|
// `dotenv::from_path_iter` should eventually be un-deprecated, see:
|
2019-10-22 19:51:50 -07:00
|
|
|
// https://github.com/dotenv-rs/dotenv/issues/13
|
|
|
|
#![allow(deprecated)]
|
2021-03-28 22:38:07 -07:00
|
|
|
|
2021-03-30 17:30:32 -07:00
|
|
|
if !settings.dotenv_load.unwrap_or(true) {
|
2021-03-28 22:38:07 -07:00
|
|
|
return Ok(BTreeMap::new());
|
|
|
|
}
|
|
|
|
|
2020-07-19 05:01:46 -07:00
|
|
|
for directory in working_directory.ancestors() {
|
|
|
|
let path = directory.join(".env");
|
|
|
|
|
|
|
|
if path.is_file() {
|
2021-07-28 00:33:44 -07:00
|
|
|
if settings.dotenv_load.is_none()
|
|
|
|
&& config.verbosity.loud()
|
|
|
|
&& !std::env::var_os("JUST_SUPPRESS_DOTENV_LOAD_WARNING")
|
|
|
|
.map(|val| val.as_os_str().to_str() == Some("1"))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2021-07-28 18:06:57 -07:00
|
|
|
eprintln!(
|
|
|
|
"{}",
|
|
|
|
Warning::DotenvLoad.color_display(config.color.stderr())
|
|
|
|
);
|
2021-07-28 00:33:44 -07:00
|
|
|
}
|
2021-03-30 17:30:32 -07:00
|
|
|
|
2020-07-19 05:01:46 -07:00
|
|
|
let iter = dotenv::from_path_iter(&path)?;
|
2020-07-16 21:37:33 -07:00
|
|
|
let mut dotenv = BTreeMap::new();
|
|
|
|
for result in iter {
|
2020-07-19 05:01:46 -07:00
|
|
|
let (key, value) = result?;
|
2020-07-16 21:37:33 -07:00
|
|
|
if env::var_os(&key).is_none() {
|
|
|
|
dotenv.insert(key, value);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 05:01:46 -07:00
|
|
|
return Ok(dotenv);
|
|
|
|
}
|
2018-03-05 13:21:35 -08:00
|
|
|
}
|
2020-07-19 05:01:46 -07:00
|
|
|
|
|
|
|
Ok(BTreeMap::new())
|
2018-03-05 13:21:35 -08:00
|
|
|
}
|