Prompt before overwriting an existing key file, with option to rename
This commit is contained in:
@@ -72,6 +72,29 @@ def format_mnemonic(words: str) -> str:
|
|||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_output_path(requested: Path) -> Path:
|
||||||
|
"""Return the path to write to, prompting if the file already exists."""
|
||||||
|
path = requested
|
||||||
|
while path.exists():
|
||||||
|
print(f"File already exists: {path}")
|
||||||
|
print(" [o] Overwrite [r] Rename [q] Quit")
|
||||||
|
choice = input("Choice: ").strip().lower()
|
||||||
|
if choice == "o":
|
||||||
|
break
|
||||||
|
elif choice == "r":
|
||||||
|
new_name = input(f"New filename (in {path.parent}): ").strip()
|
||||||
|
if not new_name:
|
||||||
|
print("No name entered, try again.")
|
||||||
|
continue
|
||||||
|
path = path.parent / new_name
|
||||||
|
elif choice == "q":
|
||||||
|
print("Aborted.")
|
||||||
|
sys.exit(0)
|
||||||
|
else:
|
||||||
|
print("Please enter o, r, or q.")
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
def ask_passphrase(confirm: bool = True) -> bytes | None:
|
def ask_passphrase(confirm: bool = True) -> bytes | None:
|
||||||
passphrase = getpass.getpass("Key passphrase (leave blank for none): ")
|
passphrase = getpass.getpass("Key passphrase (leave blank for none): ")
|
||||||
if not passphrase:
|
if not passphrase:
|
||||||
@@ -93,7 +116,7 @@ def cmd_generate(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
passphrase = ask_passphrase(confirm=True) if args.passphrase else None
|
passphrase = ask_passphrase(confirm=True) if args.passphrase else None
|
||||||
|
|
||||||
output = Path(args.output)
|
output = resolve_output_path(Path(args.output))
|
||||||
key_path, pub_path = save_keypair(private_key, output, args.comment, passphrase)
|
key_path, pub_path = save_keypair(private_key, output, args.comment, passphrase)
|
||||||
|
|
||||||
print(f"\nGenerated Ed25519 SSH key pair")
|
print(f"\nGenerated Ed25519 SSH key pair")
|
||||||
@@ -129,7 +152,7 @@ def cmd_recover(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
passphrase = ask_passphrase(confirm=True) if args.passphrase else None
|
passphrase = ask_passphrase(confirm=True) if args.passphrase else None
|
||||||
|
|
||||||
output = Path(args.output)
|
output = resolve_output_path(Path(args.output))
|
||||||
key_path, pub_path = save_keypair(private_key, output, args.comment, passphrase)
|
key_path, pub_path = save_keypair(private_key, output, args.comment, passphrase)
|
||||||
|
|
||||||
print(f"\nRecovered Ed25519 SSH key pair")
|
print(f"\nRecovered Ed25519 SSH key pair")
|
||||||
|
|||||||
Reference in New Issue
Block a user