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 all commits
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,4 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
[0.12.0]: https://github.com/theskumar/python-dotenv/compare/v0.11.0...v0.12.0
[0.11.0]: https://github.com/theskumar/python-dotenv/compare/v0.10.5...v0.11.0
[0.10.5]: https://github.com/theskumar/python-dotenv/compare/v0.10.4...v0.10.5
[0.10.4]: https://github.com/theskumar/python-dotenv/compare/v0.10.3...v0.10.4
[0.10.4]: https://github.com/theskumar/python-dotenv/compare/v0.10.3...v0.10.4
29 changes: 28 additions & 1 deletion src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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 @@ -165,6 +164,34 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
exit(ret)


@cli.command()
@click.pass_context
def generate_sample(ctx: click.Context) -> None:
'''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
)
newEnvList = []
with open(filedir, 'r') as file:
for line in file:
line = line.strip()
if line[0] != "#":
line = line.replace(" ", "").replace("=", " = ").split("=", 1)[0] + "="
else:
line = "# " + line[1:].strip() # inserts space between comment and '#'
newEnvList.append(line) # slicing removes trailing newline
newEnvList[-1] += "\n"

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

for line in newEnvList:
click.echo(line)


def run_command(command: List[str], env: Dict[str, str]) -> int:
"""Run command in sub process.

Expand Down
26 changes: 26 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,29 @@ def test_run_with_version(cli):

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


def test_generate_sample_non_existent_file(cli):
result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'generate-sample'])

assert result.exit_code == 2
assert "does not exist" in result.output


def test_generate_sample_cleanup_and_comment_preservation(cli, dotenv_file):
sample = "# a = b\n#c = d\n # e=f\n #g = h\ni=j"
with open(dotenv_file, "w") as f:
f.write(sample)
result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample'])

assert result.exit_code == 0
assert result.output == "# a = b\n# c = d\n# e=f\n# g = h\ni =\n\n"


def test_generate_sample_value_removal(cli, dotenv_file):
with open(dotenv_file, "w") as f:
f.write("a=b")
result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'generate-sample'])

assert result.exit_code == 0, result.output
assert 'b' not in result.output