2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2018-03-05 13:21:35 -08:00
|
|
|
|
2021-03-30 17:59:15 -07:00
|
|
|
// Remove this on 2021-07-01.
|
|
|
|
#[allow(unused)]
|
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-03-30 17:59:15 -07:00
|
|
|
// Un-comment this on 2021-07-01.
|
|
|
|
//
|
|
|
|
// if settings.dotenv_load.is_none() && config.verbosity.loud() {
|
|
|
|
// if config.color.stderr().active() {
|
|
|
|
// eprintln!("{:#}", Warning::DotenvLoad);
|
|
|
|
// } else {
|
|
|
|
// eprintln!("{}", Warning::DotenvLoad);
|
|
|
|
// }
|
|
|
|
// }
|
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
|
|
|
}
|