opswd 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 user list > /dev/null || return
  13. local username
  14. # Copy the username to the clipboard
  15. if ! username=$(op item get "$service" --fields username 2>/dev/null); then
  16. echo "error: could not obtain username for $service"
  17. return 1
  18. fi
  19. echo -n "$username" | clipcopy
  20. echo "✔ username for service $service copied to the clipboard. Press Enter to continue"
  21. read
  22. local password
  23. # Copy the password to the clipboard
  24. if ! password=$(op item get "$service" --fields password 2>/dev/null); then
  25. echo "error: could not obtain password for $service"
  26. return 1
  27. fi
  28. echo -n "$password" | clipcopy
  29. echo "✔ password for $service copied to clipboard. Press Enter to continue"
  30. read
  31. # If there's a one time password, copy it to the clipboard
  32. local totp
  33. if totp=$(op item get --otp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
  34. echo -n "$totp" | clipcopy
  35. echo "✔ TOTP for $service copied to clipboard"
  36. fi
  37. (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
  38. }
  39. # TODO: 2022-03-26: Remove support for op CLI 1
  40. autoload -Uz is-at-least
  41. is-at-least 2.0.0 $(op --version) || {
  42. print -ru2 ${(%):-"%F{yellow}opswd: usage with op version $(op --version) is deprecated. Upgrade to CLI 2 and reload zsh.
  43. For instructions, see https://developer.1password.com/docs/cli/upgrade.%f"}
  44. # opswd puts the password of the named service into the clipboard. If there's a
  45. # one time password, it will be copied into the clipboard after 10 seconds. The
  46. # clipboard is cleared after another 20 seconds.
  47. function opswd() {
  48. if [[ $# -lt 1 ]]; then
  49. echo "Usage: opswd <service>"
  50. return 1
  51. fi
  52. local service=$1
  53. # If not logged in, print error and return
  54. op list users > /dev/null || return
  55. local password
  56. # Copy the password to the clipboard
  57. if ! password=$(op get item "$service" --fields password 2>/dev/null); then
  58. echo "error: could not obtain password for $service"
  59. return 1
  60. fi
  61. echo -n "$password" | clipcopy
  62. echo "✔ password for $service copied to clipboard"
  63. # If there's a one time password, copy it to the clipboard after 5 seconds
  64. local totp
  65. if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
  66. sleep 10 && echo -n "$totp" | clipcopy
  67. echo "✔ TOTP for $service copied to clipboard"
  68. fi
  69. (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
  70. }
  71. }
  72. opswd "$@"