From d6a1a2b568fba217004248a3a2c85c131122d926 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 23 May 2020 20:41:12 -0700 Subject: [PATCH] Don't load `.env` file when `--no-dotenv` is passed (#627) Add a `--no-dotenv` flag that suppresses loading `.env` files. --- completions/just.bash | 2 +- completions/just.elvish | 1 + completions/just.fish | 1 + completions/just.powershell | 1 + completions/just.zsh | 1 + src/config.rs | 9 +++++++++ src/justfile.rs | 6 +++++- tests/integration.rs | 13 +++++++++++++ 8 files changed, 32 insertions(+), 2 deletions(-) diff --git a/completions/just.bash b/completions/just.bash index 49f7b25c..9f425c0c 100644 --- a/completions/just.bash +++ b/completions/just.bash @@ -20,7 +20,7 @@ _just() { case "${cmd}" in just) - opts=" -q -v -e -l -h -V -f -d -s --dry-run --highlight --no-highlight --quiet --clear-shell-args --verbose --dump --edit --evaluate --init --list --summary --variables --help --version --color --justfile --set --shell --shell-arg --working-directory --completions --show ... " + opts=" -q -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --verbose --dump --edit --evaluate --init --list --summary --variables --help --version --color --justfile --set --shell --shell-arg --working-directory --completions --show ... " if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/just.elvish b/completions/just.elvish index b1b128cd..a9716155 100644 --- a/completions/just.elvish +++ b/completions/just.elvish @@ -27,6 +27,7 @@ edit:completion:arg-completer[just] = [@words]{ cand --show 'Show information about ' cand --dry-run 'Print what just would do without doing it' cand --highlight 'Highlight echoed recipe lines in bold' + cand --no-dotenv 'Don''t load `.env` file' cand --no-highlight 'Don''t highlight echoed recipe lines in bold' cand -q 'Suppress all output' cand --quiet 'Suppress all output' diff --git a/completions/just.fish b/completions/just.fish index 8494228e..8dfce77c 100644 --- a/completions/just.fish +++ b/completions/just.fish @@ -19,6 +19,7 @@ complete -c just -n "__fish_use_subcommand" -l completions -d 'Print shell compl complete -c just -n "__fish_use_subcommand" -s s -l show -d 'Show information about ' complete -c just -n "__fish_use_subcommand" -l dry-run -d 'Print what just would do without doing it' complete -c just -n "__fish_use_subcommand" -l highlight -d 'Highlight echoed recipe lines in bold' +complete -c just -n "__fish_use_subcommand" -l no-dotenv -d 'Don\'t load `.env` file' complete -c just -n "__fish_use_subcommand" -l no-highlight -d 'Don\'t highlight echoed recipe lines in bold' complete -c just -n "__fish_use_subcommand" -s q -l quiet -d 'Suppress all output' complete -c just -n "__fish_use_subcommand" -l clear-shell-args -d 'Clear shell arguments' diff --git a/completions/just.powershell b/completions/just.powershell index 9f92c24c..6c565cb8 100644 --- a/completions/just.powershell +++ b/completions/just.powershell @@ -32,6 +32,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock { [CompletionResult]::new('--show', 'show', [CompletionResultType]::ParameterName, 'Show information about ') [CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'Print what just would do without doing it') [CompletionResult]::new('--highlight', 'highlight', [CompletionResultType]::ParameterName, 'Highlight echoed recipe lines in bold') + [CompletionResult]::new('--no-dotenv', 'no-dotenv', [CompletionResultType]::ParameterName, 'Don''t load `.env` file') [CompletionResult]::new('--no-highlight', 'no-highlight', [CompletionResultType]::ParameterName, 'Don''t highlight echoed recipe lines in bold') [CompletionResult]::new('-q', 'q', [CompletionResultType]::ParameterName, 'Suppress all output') [CompletionResult]::new('--quiet', 'quiet', [CompletionResultType]::ParameterName, 'Suppress all output') diff --git a/completions/just.zsh b/completions/just.zsh index 6f042890..7b7888b1 100644 --- a/completions/just.zsh +++ b/completions/just.zsh @@ -28,6 +28,7 @@ _just() { '--show=[Show information about ]: :_just_commands' \ '(-q --quiet)--dry-run[Print what just would do without doing it]' \ '--highlight[Highlight echoed recipe lines in bold]' \ +'--no-dotenv[Don'\''t load `.env` file]' \ '--no-highlight[Don'\''t highlight echoed recipe lines in bold]' \ '(--dry-run)-q[Suppress all output]' \ '(--dry-run)--quiet[Suppress all output]' \ diff --git a/src/config.rs b/src/config.rs index 52df3958..aaa837fa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub(crate) struct Config { pub(crate) dry_run: bool, pub(crate) highlight: bool, pub(crate) invocation_directory: PathBuf, + pub(crate) load_dotenv: bool, pub(crate) quiet: bool, pub(crate) search_config: SearchConfig, pub(crate) shell: String, @@ -64,6 +65,7 @@ mod arg { pub(crate) const DRY_RUN: &str = "DRY-RUN"; pub(crate) const HIGHLIGHT: &str = "HIGHLIGHT"; pub(crate) const JUSTFILE: &str = "JUSTFILE"; + pub(crate) const NO_DOTENV: &str = "NO-DOTENV"; pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT"; pub(crate) const QUIET: &str = "QUIET"; pub(crate) const SET: &str = "SET"; @@ -105,6 +107,11 @@ impl Config { .help("Highlight echoed recipe lines in bold") .overrides_with(arg::NO_HIGHLIGHT), ) + .arg( + Arg::with_name(arg::NO_DOTENV) + .long("no-dotenv") + .help("Don't load `.env` file"), + ) .arg( Arg::with_name(arg::NO_HIGHLIGHT) .long("no-highlight") @@ -399,6 +406,7 @@ impl Config { highlight: !matches.is_present(arg::NO_HIGHLIGHT), quiet: matches.is_present(arg::QUIET), shell: matches.value_of(arg::SHELL).unwrap().to_owned(), + load_dotenv: !matches.is_present(arg::NO_DOTENV), color, invocation_directory, search_config, @@ -695,6 +703,7 @@ FLAGS: --highlight Highlight echoed recipe lines in bold --init Initialize new justfile in project root -l, --list List available recipes and their arguments + --no-dotenv Don't load `.env` file --no-highlight Don't highlight echoed recipe lines in bold -q, --quiet Suppress all output --summary List names of available recipes diff --git a/src/justfile.rs b/src/justfile.rs index a027db40..929555f9 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -90,7 +90,11 @@ impl<'src> Justfile<'src> { }); } - let dotenv = load_dotenv()?; + let dotenv = if config.load_dotenv { + load_dotenv()? + } else { + BTreeMap::new() + }; let scope = { let mut scope = Scope::new(); diff --git a/tests/integration.rs b/tests/integration.rs index 57bca015..bc329673 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1971,6 +1971,19 @@ echo: stderr: "echo dotenv-value\necho dotenv-value\n", } +test! { + name: no_dotenv, + justfile: " +# +X:=env_var_or_default('DOTENV_KEY', 'DEFAULT') +echo: + echo {{X}} + ", + args: ("--no-dotenv"), + stdout: "DEFAULT\n", + stderr: "echo DEFAULT\n", +} + test! { name: invalid_escape_sequence_message, justfile: r#"