From a64d9403775f45036daf3b66693c35df7bab3c53 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 26 Mar 2022 15:10:56 +0100 Subject: refactor(1password): extract `opswd` function --- plugins/1password/opswd | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 plugins/1password/opswd (limited to 'plugins/1password/opswd') diff --git a/plugins/1password/opswd b/plugins/1password/opswd new file mode 100644 index 000000000..6849d42b3 --- /dev/null +++ b/plugins/1password/opswd @@ -0,0 +1,37 @@ +#autoload + +# opswd puts the password of the named service into the clipboard. If there's a +# one time password, it will be copied into the clipboard after 10 seconds. The +# clipboard is cleared after another 20 seconds. +function opswd() { + if [[ $# -lt 1 ]]; then + echo "Usage: opswd " + return 1 + fi + + local service=$1 + + # If not logged in, print error and return + op list users > /dev/null || return + + local password + # Copy the password to the clipboard + if ! password=$(op get item "$service" --fields password 2>/dev/null); then + echo "error: could not obtain password for $service" + return 1 + fi + + echo -n "$password" | clipcopy + echo "✔ password for $service copied to clipboard" + + # If there's a one time password, copy it to the clipboard after 5 seconds + local totp + if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then + sleep 10 && echo -n "$totp" | clipcopy + echo "✔ TOTP for $service copied to clipboard" + fi + + (sleep 20 && clipcopy /dev/null) &! +} + +opswd "$@" -- cgit v1.2.3-70-g09d2