|
17 | 17 | ) |
18 | 18 | sys.exit(1) |
19 | 19 |
|
20 | | -from .main import dotenv_values, set_key, unset_key |
| 20 | +from .main import dotenv_values, generate_template, set_key, unset_key |
21 | 21 | from .version import __version__ |
22 | 22 |
|
23 | 23 |
|
@@ -201,6 +201,37 @@ def run(ctx: click.Context, override: bool, commandline: tuple[str, ...]) -> Non |
201 | 201 | run_command([*commandline, *ctx.args], dotenv_as_dict) |
202 | 202 |
|
203 | 203 |
|
| 204 | +@cli.command() |
| 205 | +@click.pass_context |
| 206 | +@click.argument("output", default=None, required=False, type=click.Path()) |
| 207 | +@click.option( |
| 208 | + "--keep-directives", |
| 209 | + is_flag=True, |
| 210 | + default=False, |
| 211 | + help="Keep ::dotenv-template-preserve and ::dotenv-template-exclude directives in output.", |
| 212 | +) |
| 213 | +def template(ctx: click.Context, output: Any, keep_directives: bool) -> None: |
| 214 | + """Generate a template from the .env file. |
| 215 | +
|
| 216 | + Values are replaced with their key names. Use ::dotenv-template-preserve |
| 217 | + in a line to keep its value, or ::dotenv-template-exclude to omit it. |
| 218 | +
|
| 219 | + If OUTPUT is given, the template is written to that file. Otherwise it is |
| 220 | + printed to stdout. |
| 221 | + """ |
| 222 | + file = ctx.obj["FILE"] |
| 223 | + |
| 224 | + with stream_file(file) as stream: |
| 225 | + result = generate_template(stream=stream, keep_directives=keep_directives) |
| 226 | + |
| 227 | + if output: |
| 228 | + with open(output, "w") as f: |
| 229 | + f.write(result) |
| 230 | + click.echo(f"Template written to {output}") |
| 231 | + else: |
| 232 | + click.echo(result, nl=False) |
| 233 | + |
| 234 | + |
204 | 235 | def run_command(command: List[str], env: Dict[str, str]) -> None: |
205 | 236 | """Replace the current process with the specified command. |
206 | 237 |
|
|
0 commit comments