123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/usr/bin/env python3
- # pylint: disable=invalid-name; script name
- import argparse
- import os
- import pathlib
- import subprocess
- import time
- def _get_password_store_prefix_path() -> pathlib.Path:
- # $ grep ^PREFIX $(which pass)
- return pathlib.Path(
- subprocess.run(
- ["pass", "git", "rev-parse", "--show-toplevel"],
- stdout=subprocess.PIPE,
- check=True,
- )
- .stdout.rstrip()
- .decode()
- )
- def _main():
- argparser = argparse.ArgumentParser(allow_abbrev=False)
- argparser.add_argument("password_path", nargs="?")
- args = argparser.parse_args()
- if not args.password_path:
- prefix = _get_password_store_prefix_path()
- for password_path in prefix.rglob("*.gpg"):
- print(
- password_path.relative_to(prefix).as_posix()[
- : -len(password_path.suffix)
- ]
- )
- else:
- password = (
- subprocess.run(
- ["pass", "show", args.password_path],
- stdout=subprocess.PIPE,
- check=True,
- )
- .stdout.splitlines()[0]
- .decode()
- )
- assert password.isprintable()
- assert "'" not in password
- os.close(1) # close rofi's window
- time.sleep(0.1) # wait for rofi's window to close
- subprocess.run(
- ["xdotool", "-"], input=f"type '{password}'".encode(), check=True
- )
- if __name__ == "__main__":
- _main()
|