59 lines
1.5 KiB
Python
Executable File
59 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
from functools import partial
|
|
from pathlib import PosixPath
|
|
|
|
PASSWORD_STORE = PosixPath(
|
|
os.environ.get("PASSWORD_STORE_DIR", PosixPath("~/.password-store").expanduser())
|
|
)
|
|
|
|
ALIASES = {
|
|
"salt": "cms-net-svcs",
|
|
}
|
|
|
|
cmd = partial(subprocess.run, capture_output=True, encoding="ascii")
|
|
|
|
|
|
def notify(summary: str, body: str) -> None:
|
|
subprocess.run(["notify-send", summary, body])
|
|
|
|
|
|
def rofi_select(options: list[str]) -> str:
|
|
options_str = "\n".join(options)
|
|
rofi = cmd(["rofi", "-dmenu"], input=options_str)
|
|
return rofi.stdout.strip()
|
|
|
|
|
|
def get_password(password_name: str) -> None:
|
|
pass_result = cmd(["pass", password_name])
|
|
password, _, _ = pass_result.stdout.partition("\n")
|
|
return password
|
|
|
|
|
|
def select_and_type(glob: str) -> None:
|
|
files = PASSWORD_STORE.glob(glob)
|
|
file_list = [str(f.relative_to(PASSWORD_STORE).with_suffix("")) for f in files]
|
|
|
|
selected = rofi_select(file_list)
|
|
password = get_password(selected)
|
|
|
|
subprocess.run(["xdotool", "type", password + '\n'])
|
|
|
|
|
|
window_name = cmd(["xdotool", "getactivewindow", "getwindowname"]).stdout.strip()
|
|
|
|
ssh_match = re.search(":(mosh|ssh) (?P<server>.*)", window_name)
|
|
if ssh_match:
|
|
raw_server_name = ssh_match.group("server")
|
|
server_name = ALIASES.get(raw_server_name, raw_server_name)
|
|
|
|
notify(f"Matched server '{server_name}'", f"Window name: {window_name}")
|
|
|
|
select_and_type(f"servers/**/{server_name}.gpg")
|
|
|
|
else:
|
|
notify("Window name did not match any rules", f"Window name: {window_name}")
|