password-store-type 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. # pylint: disable=invalid-name; script name
  3. import argparse
  4. import os
  5. import pathlib
  6. import subprocess
  7. import time
  8. def _get_password_store_prefix_path() -> pathlib.Path:
  9. # $ grep ^PREFIX $(which pass)
  10. return pathlib.Path(
  11. subprocess.run(
  12. ["pass", "git", "rev-parse", "--show-toplevel"],
  13. stdout=subprocess.PIPE,
  14. check=True,
  15. )
  16. .stdout.rstrip()
  17. .decode()
  18. )
  19. def _main():
  20. argparser = argparse.ArgumentParser(allow_abbrev=False)
  21. argparser.add_argument("password_path", nargs="?")
  22. args = argparser.parse_args()
  23. if not args.password_path:
  24. prefix = _get_password_store_prefix_path()
  25. for password_path in prefix.rglob("*.gpg"):
  26. print(
  27. password_path.relative_to(prefix).as_posix()[
  28. : -len(password_path.suffix)
  29. ]
  30. )
  31. else:
  32. password = (
  33. subprocess.run(
  34. ["pass", "show", args.password_path],
  35. stdout=subprocess.PIPE,
  36. check=True,
  37. )
  38. .stdout.splitlines()[0]
  39. .decode()
  40. )
  41. assert password.isprintable()
  42. assert "'" not in password
  43. os.close(1) # close rofi's window
  44. time.sleep(0.1) # wait for rofi's window to close
  45. subprocess.run(
  46. ["xdotool", "-"], input=f"type '{password}'".encode(), check=True
  47. )
  48. if __name__ == "__main__":
  49. _main()