Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example env file generator #362

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b76b26
.example.env command
ethsanders Nov 10, 2021
b5cb2e3
implemented suggestions
ethsanders May 31, 2022
f844040
Update test_cli.py
ethsanders May 31, 2022
d38a792
Merge branch 'master' into master
ethsanders Jun 6, 2022
cde7a58
Added tests for comment preservation and value removal
ethsanders Jun 6, 2022
afb206e
fixing spacing
ethsanders Jun 6, 2022
e0d0f91
might have fixed test error
ethsanders Jun 6, 2022
eb2c82b
python 3.5 doesn't support f-strings
ethsanders Jun 6, 2022
f96ddc7
tests still not working
ethsanders Jun 6, 2022
dbb8bc6
Merge branch 'master' of https://github.com/ProfessorPiggos/python-do…
ethsanders Jun 6, 2022
a9d8776
Fixing test syntax
ethsanders Jun 6, 2022
4ef6e12
Fixed how test handled newlines
ethsanders Jun 6, 2022
1e4928b
Clean up duplicate whitespace in generated sample
ethsanders Jun 6, 2022
33492dc
Fixed test to work with cleanup of duplicate whitespace
ethsanders Jun 6, 2022
4c6ce11
New whitespace issues
ethsanders Jun 6, 2022
897ce24
finalized behavior of whitespace
ethsanders Jun 6, 2022
d68fad5
added more cleanup to make behavior more consistent
ethsanders Jun 6, 2022
6062be6
forgot that equals sign needed another space
ethsanders Jun 6, 2022
1f834aa
updated tests for the final time
ethsanders Jun 6, 2022
ebe5a8b
Update cli.py
ethsanders Jun 6, 2022
415a051
preserves equal sign spacing in a comment
ethsanders Jun 6, 2022
f71f160
modified test to be accurate and add testing of equals sign cleanup
ethsanders Jun 6, 2022
3b307fb
fixed comment preservation and it's test
ethsanders Jun 6, 2022
706d181
fixed what are presumably linting errors
ethsanders Jun 6, 2022
d1cc3b8
Merge branch 'master' into master
ethsanders Aug 31, 2022
693a5ab
Merge branch 'main' into master
theskumar Jun 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.19.2] - 2021-11-11

### Fixed

- In `set_key`, add missing newline character before new entry if necessary. (#361 by
[@bbc2])

## [0.19.1] - 2021-08-09

### Added
Expand Down Expand Up @@ -292,7 +299,8 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
[@yannham]: https://github.com/yannham
[@zueve]: https://github.com/zueve

[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...HEAD
[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.2...HEAD
[0.19.2]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...v0.19.2
[0.19.1]: https://github.com/theskumar/python-dotenv/compare/v0.19.0...v0.19.1
[0.19.0]: https://github.com/theskumar/python-dotenv/compare/v0.18.0...v0.19.0
[0.18.0]: https://github.com/theskumar/python-dotenv/compare/v0.17.1...v0.18.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.19.1
current_version = 0.19.2
commit = True
tag = True

Expand Down
29 changes: 27 additions & 2 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def get(ctx: click.Context, key: Any) -> None:
else:
exit(1)


@cli.command()
@click.pass_context
@click.argument('key', required=True)
Expand Down Expand Up @@ -125,6 +124,32 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
ret = run_command(commandline, dotenv_as_dict)
exit(ret)

@cli.command()
@click.pass_context
def example_file(ctx: click.Context) -> None:
ethsanders marked this conversation as resolved.
Show resolved Hide resolved
'''Generates a .example.env file without values.'''
filedir = ctx.obj['FILE'].replace("\\", "/")
if not os.path.isfile(filedir):
raise click.BadParameter(
'Path "%s" does not exist.' % (filedir),
ctx=ctx
)
newFileList = []
with open(filedir,'r') as file:
for line in file:
line = line.strip()
if line[0] != "#":
line = line.split("=", 1)[0] + "="
newFileList.append(line + "\n")

while newFileList[-1] == "\n":
newFileList.pop(-1)

newFileName = f"{os.path.basename(file.name).split('.', 1)[0]}.example.env"
ethsanders marked this conversation as resolved.
Show resolved Hide resolved
with open(newFileName, "w") as newFile:
for line in newFileList:
newFile.write(line)
click.echo(f"{newFileName} exported.")

def run_command(command: List[str], env: Dict[str, str]) -> int:
"""Run command in sub process.
Expand Down Expand Up @@ -161,4 +186,4 @@ def run_command(command: List[str], env: Dict[str, str]) -> int:


if __name__ == "__main__":
cli()
cli()
4 changes: 4 additions & 0 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,17 @@ def set_key(

with rewrite(dotenv_path) as (source, dest):
replaced = False
missing_newline = False
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
if mapping.key == key_to_set:
dest.write(line_out)
replaced = True
else:
dest.write(mapping.original.string)
missing_newline = not mapping.original.string.endswith("\n")
if not replaced:
if missing_newline:
dest.write("\n")
dest.write(line_out)

return True, key_to_set, value_to_set
Expand Down
2 changes: 1 addition & 1 deletion src/dotenv/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.19.1"
__version__ = "0.19.2"
6 changes: 6 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,9 @@ def test_run_with_version(cli):

assert result.exit_code == 0
assert result.output.strip().endswith(__version__)

def test_example_file_non_existent_file(cli):
result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'example_file'])

assert result.exit_code == 2, result.output
assert "does not exist" in result.output
1 change: 1 addition & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_set_key_no_file(tmp_path):
("a=b\nc=d", "a", "e", (True, "a", "e"), "a='e'\nc=d"),
("a=b\nc=d\ne=f", "c", "g", (True, "c", "g"), "a=b\nc='g'\ne=f"),
("a=b\n", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"),
("a=b", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"),
],
)
def test_set_key(dotenv_file, before, key, value, expected, after):
Expand Down