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