feat: save to file

This commit is contained in:
Ruidy 2021-07-05 20:26:27 +02:00
parent 89beba48cc
commit 2277ba85ad
5 changed files with 22 additions and 3 deletions

View file

@ -6,5 +6,5 @@
- [x] Minimal length - [x] Minimal length
- [x] Include symbols - [x] Include symbols
- [x] Include numbers - [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 - [x] As a user I want the generated password to be saved to a file

View file

@ -1,5 +1,6 @@
import random as r import random as r
import subprocess import subprocess
from typing import Optional
import typer import typer
@ -18,13 +19,21 @@ def main(
symbols: bool = typer.Option( symbols: bool = typer.Option(
False, help="If the generated password should include special characters." 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: ) -> None:
seed = r.randint(0, 100) if random else 0 seed = r.randint(0, 100) if random else 0
password = generate_password(seed, length, symbols, numbers) 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)) 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( typer.echo(
"The password has been copied to your clipboard 😉\nPaste it using cmd + v" "The password has been copied to your clipboard 😉\nPaste it using cmd + v"
) )

View file

@ -30,6 +30,14 @@ def test_cli_can_set_numbers() -> None:
assert "yWAcqGFz" in result.stdout 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: def _run_cli(*args) -> Result:
result = runner.invoke(app, ["--no-random", *args]) result = runner.invoke(app, ["--no-random", *args])
assert result.exit_code == 0 assert result.exit_code == 0

1
psswd.txt Normal file
View file

@ -0,0 +1 @@
-'R>;X*cut&2^aOK

1
test.txt Normal file
View file

@ -0,0 +1 @@
2yW4AcqG