opswd 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #autoload
  2. # opswd puts the password of the named service into the clipboard. If there's a
  3. # one time password, it will be copied into the clipboard after 10 seconds. The
  4. # clipboard is cleared after another 20 seconds.
  5. function opswd() {
  6. if [[ $# -lt 1 ]]; then
  7. echo "Usage: opswd <service>"
  8. return 1
  9. fi
  10. local service=$1
  11. # If not logged in, print error and return
  12. op list users > /dev/null || return
  13. local password
  14. # Copy the password to the clipboard
  15. if ! password=$(op get item "$service" --fields password 2>/dev/null); then
  16. echo "error: could not obtain password for $service"
  17. return 1
  18. fi
  19. echo -n "$password" | clipcopy
  20. echo "✔ password for $service copied to clipboard"
  21. # If there's a one time password, copy it to the clipboard after 5 seconds
  22. local totp
  23. if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
  24. sleep 10 && echo -n "$totp" | clipcopy
  25. echo "✔ TOTP for $service copied to clipboard"
  26. fi
  27. (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
  28. }
  29. opswd "$@"