diff --git a/CHANGELOG.md b/CHANGELOG.md index 220d1888..1d26a357 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 65ead461..17321f72 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -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) @@ -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. diff --git a/tests/test_cli.py b/tests/test_cli.py index 8afbe59d..4362e48b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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