diff --git a/README.md b/README.md index fbec6fb..ddc8ded 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ - [x] Minimal length - [x] Include symbols - [x] Include numbers -- [ ] As a user I want the generated password to be copied to the clipboard +- [x] As a user I want the generated password to be copied to the clipboard - [x] As a user I want the generated password to be saved to a file diff --git a/app/main.py b/app/main.py index 8fc05f2..733cdc9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,6 @@ import random as r import subprocess +from typing import Optional import typer @@ -18,13 +19,21 @@ def main( symbols: bool = typer.Option( False, help="If the generated password should include special characters." ), + file: Optional[str] = typer.Option( + None, help="Path to the file where the generated password should be saved." + ), ) -> None: seed = r.randint(0, 100) if random else 0 password = generate_password(seed, length, symbols, numbers) - subprocess.run("pbcopy", universal_newlines=True, input=password) - typer.echo(typer.style(f"🔐 {password}", fg=typer.colors.GREEN, bold=True)) + + if file is not None: + with open(file, "w") as f: + f.write(password) + return + + subprocess.run("pbcopy", universal_newlines=True, input=password) typer.echo( "The password has been copied to your clipboard 😉\nPaste it using cmd + v" ) diff --git a/app/main_test.py b/app/main_test.py index 391eb34..9ebab17 100644 --- a/app/main_test.py +++ b/app/main_test.py @@ -30,6 +30,14 @@ def test_cli_can_set_numbers() -> None: assert "yWAcqGFz" in result.stdout +def test_cli_can_save_to_file() -> None: + args = ["--file", "test.txt"] + _ = _run_cli(*args) + with open("test.txt", "r") as f: + content = f.read() + assert "2yW4AcqG" in content + + def _run_cli(*args) -> Result: result = runner.invoke(app, ["--no-random", *args]) assert result.exit_code == 0 diff --git a/psswd.txt b/psswd.txt new file mode 100644 index 0000000..f40ee0d --- /dev/null +++ b/psswd.txt @@ -0,0 +1 @@ +-'R>;X*cut&2^aOK \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..10d99eb --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +2yW4AcqG \ No newline at end of file