Skip to content

Commit efc5182

Browse files
zuevebbc2
authored andcommitted
Add --override/--no-override flag to "dotenv run"
This makes it possible to not override previously defined environment variables when running `dotenv run`. It defaults to `--override` for compatibility with the previous behavior.
1 parent b96db46 commit efc5182

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10-
_There are no unreleased changes at this time._
10+
### Added
11+
12+
- Add `--override`/`--no-override` option to `dotenv run` (#312 by [@zueve] and [@bbc2]).
1113

1214
## [0.16.0] - 2021-03-27
1315

@@ -242,6 +244,7 @@ _There are no unreleased changes at this time._
242244
[@venthur]: https://github.com/venthur
243245
[@x-yuri]: https://github.com/x-yuri
244246
[@yannham]: https://github.com/yannham
247+
[@zueve]: https://github.com/zueve
245248

246249
[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.16.0...HEAD
247250
[0.16.0]: https://github.com/theskumar/python-dotenv/compare/v0.15.0...v0.16.0

src/dotenv/cli.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,26 @@ def unset(ctx, key):
107107

108108
@cli.command(context_settings={'ignore_unknown_options': True})
109109
@click.pass_context
110+
@click.option(
111+
"--override/--no-override",
112+
default=True,
113+
help="Override variables from the environment file with those from the .env file.",
114+
)
110115
@click.argument('commandline', nargs=-1, type=click.UNPROCESSED)
111-
def run(ctx, commandline):
112-
# type: (click.Context, List[str]) -> None
116+
def run(ctx, override, commandline):
117+
# type: (click.Context, bool, List[str]) -> None
113118
"""Run command with environment variables present."""
114119
file = ctx.obj['FILE']
115120
if not os.path.isfile(file):
116121
raise click.BadParameter(
117122
'Invalid value for \'-f\' "%s" does not exist.' % (file),
118123
ctx=ctx
119124
)
120-
dotenv_as_dict = {to_env(k): to_env(v) for (k, v) in dotenv_values(file).items() if v is not None}
125+
dotenv_as_dict = {
126+
to_env(k): to_env(v)
127+
for (k, v) in dotenv_values(file).items()
128+
if v is not None and (override or to_env(k) not in os.environ)
129+
}
121130

122131
if not commandline:
123132
click.echo('No command given.')

tests/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ def test_run(tmp_path):
138138
assert result == "b\n"
139139

140140

141+
def test_run_with_existing_variable(tmp_path):
142+
sh.cd(str(tmp_path))
143+
dotenv_file = str(tmp_path / ".env")
144+
with open(dotenv_file, "w") as f:
145+
f.write("a=b")
146+
147+
result = sh.dotenv("run", "printenv", "a", _env={"LANG": "en_US.UTF-8", "a": "c"})
148+
149+
assert result == "b\n"
150+
151+
152+
def test_run_with_existing_variable_not_overridden(tmp_path):
153+
sh.cd(str(tmp_path))
154+
dotenv_file = str(tmp_path / ".env")
155+
with open(dotenv_file, "w") as f:
156+
f.write("a=b")
157+
158+
result = sh.dotenv(
159+
"run",
160+
"--no-override",
161+
"printenv",
162+
"a",
163+
_env={"LANG": "en_US.UTF-8", "a": "c"},
164+
)
165+
166+
assert result == "c\n"
167+
168+
141169
def test_run_with_none_value(tmp_path):
142170
sh.cd(str(tmp_path))
143171
dotenv_file = str(tmp_path / ".env")

0 commit comments

Comments
 (0)