-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flags for specifying name and path environment file (#941)
- Loading branch information
Showing
9 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
use crate::common::*; | ||
|
||
const DEFAULT_DOTENV_FILENAME: &str = ".env"; | ||
|
||
pub(crate) fn load_dotenv( | ||
config: &Config, | ||
settings: &Settings, | ||
working_directory: &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 !settings.dotenv_load.unwrap_or(true) { | ||
if !settings.dotenv_load.unwrap_or(true) | ||
&& config.dotenv_filename.is_none() | ||
&& config.dotenv_path.is_none() | ||
{ | ||
return Ok(BTreeMap::new()); | ||
} | ||
|
||
for directory in working_directory.ancestors() { | ||
let path = directory.join(".env"); | ||
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(); | ||
|
||
for directory in working_directory.ancestors() { | ||
let path = directory.join(&filename); | ||
if path.is_file() { | ||
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) | ||
{ | ||
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); | ||
} | ||
} | ||
return Ok(dotenv); | ||
return load_from_file(config, settings, &path); | ||
} | ||
} | ||
|
||
Ok(BTreeMap::new()) | ||
} | ||
|
||
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") | ||
.map(|val| val.as_os_str().to_str() == Some("1")) | ||
.unwrap_or(false) | ||
{ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters