1password.plugin.zsh 1.3 KB

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