a64d9403775f45036daf3b66693c35df7bab3c53

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(1password): extract `opswd` function

Diff

  1diff --git a/plugins/1password/1password.plugin.zsh b/plugins/1password/1password.plugin.zsh
  2index 9398b02b4911f99db1403d19dee102b9e75f518b..941523ca8c1fec5d892901d0fb7552297ce7f6cb 100644
  3--- a/plugins/1password/1password.plugin.zsh
  4+++ b/plugins/1password/1password.plugin.zsh
  5@@ -1,46 +1,9 @@
  6-if (( ${+commands[op]} )); then
  7-  eval "$(op completion zsh)"
  8-  compdef _op op
  9-fi
 10+# Do nothing if op is not installed
 11+(( ${+commands[op]} )) || return
 12 
 13-# opswd puts the password of the named service into the clipboard. If there's a
 14-# one time password, it will be copied into the clipboard after 10 seconds. The
 15-# clipboard is cleared after another 20 seconds.
 16-function opswd() {
 17-  if [[ $# -lt 1 ]]; then
 18-    echo "Usage: opswd <service>"
 19-    return 1
 20-  fi
 21+# Load op completion
 22+eval "$(op completion zsh)"
 23+compdef _op op
 24 
 25-  local service=$1
 26-
 27-  # If not logged in, print error and return
 28-  op list users > /dev/null || return
 29-
 30-  local password
 31-  # Copy the password to the clipboard
 32-  if ! password=$(op get item "$service" --fields password 2>/dev/null); then
 33-    echo "error: could not obtain password for $service"
 34-    return 1
 35-  fi
 36-
 37-  echo -n "$password" | clipcopy
 38-  echo "✔ password for $service copied to clipboard"
 39-
 40-  # If there's a one time password, copy it to the clipboard after 5 seconds
 41-  local totp
 42-  if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
 43-    sleep 10 && echo -n "$totp" | clipcopy
 44-    echo "✔ TOTP for $service copied to clipboard"
 45-  fi
 46-
 47-  (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
 48-}
 49-
 50-function _opswd() {
 51-  local -a services
 52-  services=("${(@f)$(op list items --categories Login 2>/dev/null | op get item - --fields title 2>/dev/null)}")
 53-  [[ -z "$services" ]] || compadd -a -- services
 54-}
 55-
 56-compdef _opswd opswd
 57+# Load opswd function
 58+autoload -Uz opswd
 59diff --git a/plugins/1password/_opswd b/plugins/1password/_opswd
 60new file mode 100644
 61index 0000000000000000000000000000000000000000..b92bf8fc8c28cb8007a5c9864c65578771d08418
 62--- /dev/null
 63+++ b/plugins/1password/_opswd
 64@@ -0,0 +1,9 @@
 65+#compdef opswd
 66+
 67+function _opswd() {
 68+  local -a services
 69+  services=("${(@f)$(op list items --categories Login 2>/dev/null | op get item - --fields title 2>/dev/null)}")
 70+  [[ -z "$services" ]] || compadd -a -- services
 71+}
 72+
 73+_opswd "$@"
 74diff --git a/plugins/1password/opswd b/plugins/1password/opswd
 75new file mode 100644
 76index 0000000000000000000000000000000000000000..6849d42b35ef9f30980f380f83535e44d60e8153
 77--- /dev/null
 78+++ b/plugins/1password/opswd
 79@@ -0,0 +1,37 @@
 80+#autoload
 81+
 82+# opswd puts the password of the named service into the clipboard. If there's a
 83+# one time password, it will be copied into the clipboard after 10 seconds. The
 84+# clipboard is cleared after another 20 seconds.
 85+function opswd() {
 86+  if [[ $# -lt 1 ]]; then
 87+    echo "Usage: opswd <service>"
 88+    return 1
 89+  fi
 90+
 91+  local service=$1
 92+
 93+  # If not logged in, print error and return
 94+  op list users > /dev/null || return
 95+
 96+  local password
 97+  # Copy the password to the clipboard
 98+  if ! password=$(op get item "$service" --fields password 2>/dev/null); then
 99+    echo "error: could not obtain password for $service"
100+    return 1
101+  fi
102+
103+  echo -n "$password" | clipcopy
104+  echo "✔ password for $service copied to clipboard"
105+
106+  # If there's a one time password, copy it to the clipboard after 5 seconds
107+  local totp
108+  if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
109+    sleep 10 && echo -n "$totp" | clipcopy
110+    echo "✔ TOTP for $service copied to clipboard"
111+  fi
112+
113+  (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
114+}
115+
116+opswd "$@"