dbadfa0810c0be346b98c02cc802c33fa43bee11

Author
Adam Pike <adam.pike@1password.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(1password): support CLI 2 and soft-deprecate CLI 1 (#10787)

This change still supports CLI 1, but shows a deprecation warning
on the first run of `opswd`. Support for CLI 1 shall be removed
in the near future.

Closes #10787

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

  1diff --git a/plugins/1password/README.md b/plugins/1password/README.md
  2index f6790ca8a20996e0c026aeef2a270a5af4be289c..f6854da533e4e56cde42011a97dc155a912b2623 100644
  3--- a/plugins/1password/README.md
  4+++ b/plugins/1password/README.md
  5@@ -25,11 +25,14 @@ which service you want to get.
  6 For example, `opswd github.com` will put your GitHub password into your clipboard, and if
  7 a TOTP is available, it will be copied to the clipboard after 10 seconds.
  8 
  9-> NOTE: you need to be logged in for `opswd` to work. See:
 10+> NOTE: you need to be signed in for `opswd` to work. If you are using biometric unlock,
 11+> 1Password CLI will automatically prompt you to sign in. See:
 12 >
 13-> - [Sign in or out](https://support.1password.com/command-line/#sign-in-or-out)
 14-> - [Session management](https://support.1password.com/command-line/#appendix-session-management)
 15+> - [Get started with 1Password CLI 2: Sign in](https://developer.1password.com/docs/cli/get-started#sign-in)
 16+> - [Sign in to your 1Password account manually](https://developer.1password.com/docs/cli/sign-in-manually)
 17 
 18 ## Requirements
 19 
 20-- [1Password's command line utility](https://1password.com/downloads/command-line/).
 21+- [1Password CLI 2](https://developer.1password.com/docs/cli/get-started#install)
 22+
 23+  > NOTE: if you're using 1Password CLI 1, [see how to upgrade to CLI 2](https://developer.1password.com/docs/cli/upgrade).
 24diff --git a/plugins/1password/_opswd b/plugins/1password/_opswd
 25index b92bf8fc8c28cb8007a5c9864c65578771d08418..dbc094f87ec3a62959baf5ab77dfc10977e7dfa1 100644
 26--- a/plugins/1password/_opswd
 27+++ b/plugins/1password/_opswd
 28@@ -2,8 +2,18 @@
 29 
 30 function _opswd() {
 31   local -a services
 32-  services=("${(@f)$(op list items --categories Login 2>/dev/null | op get item - --fields title 2>/dev/null)}")
 33+  services=("${(@f)$(op item list --categories Login --cache 2>/dev/null | awk 'NR != 1 { print $2 }')}")
 34   [[ -z "$services" ]] || compadd -a -- services
 35 }
 36 
 37+# TODO: 2022-03-26: Remove support for op CLI 1
 38+autoload -Uz is-at-least
 39+is-at-least 2.0.0 $(op --version) || {
 40+  function _opswd() {
 41+    local -a services
 42+    services=("${(@f)$(op list items --categories Login 2>/dev/null | op get item - --fields title 2>/dev/null)}")
 43+    [[ -z "$services" ]] || compadd -a -- services
 44+  }
 45+}
 46+
 47 _opswd "$@"
 48diff --git a/plugins/1password/opswd b/plugins/1password/opswd
 49index 6849d42b35ef9f30980f380f83535e44d60e8153..57672807ef69fd1c7c31c475668219c6ec9960fe 100644
 50--- a/plugins/1password/opswd
 51+++ b/plugins/1password/opswd
 52@@ -12,11 +12,11 @@ function opswd() {
 53   local service=$1
 54 
 55   # If not logged in, print error and return
 56-  op list users > /dev/null || return
 57+  op user list > /dev/null || return
 58 
 59   local password
 60   # Copy the password to the clipboard
 61-  if ! password=$(op get item "$service" --fields password 2>/dev/null); then
 62+  if ! password=$(op item get "$service" --fields password 2>/dev/null); then
 63     echo "error: could not obtain password for $service"
 64     return 1
 65   fi
 66@@ -24,9 +24,9 @@ function opswd() {
 67   echo -n "$password" | clipcopy
 68   echo "✔ password for $service copied to clipboard"
 69 
 70-  # If there's a one time password, copy it to the clipboard after 5 seconds
 71+  # If there's a one time password, copy it to the clipboard after 10 seconds
 72   local totp
 73-  if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
 74+  if totp=$(op item get --otp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
 75     sleep 10 && echo -n "$totp" | clipcopy
 76     echo "✔ TOTP for $service copied to clipboard"
 77   fi
 78@@ -34,4 +34,45 @@ function opswd() {
 79   (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
 80 }
 81 
 82+# TODO: 2022-03-26: Remove support for op CLI 1
 83+autoload -Uz is-at-least
 84+is-at-least 2.0.0 $(op --version) || {
 85+  print -ru2 ${(%):-"%F{yellow}opswd: usage with op version $(op --version) is deprecated. Upgrade to CLI 2 and reload zsh.
 86+For instructions, see https://developer.1password.com/docs/cli/upgrade.%f"}
 87+
 88+  # opswd puts the password of the named service into the clipboard. If there's a
 89+  # one time password, it will be copied into the clipboard after 10 seconds. The
 90+  # clipboard is cleared after another 20 seconds.
 91+  function opswd() {
 92+    if [[ $# -lt 1 ]]; then
 93+      echo "Usage: opswd <service>"
 94+      return 1
 95+    fi
 96+
 97+    local service=$1
 98+
 99+    # If not logged in, print error and return
100+    op list users > /dev/null || return
101+
102+    local password
103+    # Copy the password to the clipboard
104+    if ! password=$(op get item "$service" --fields password 2>/dev/null); then
105+      echo "error: could not obtain password for $service"
106+      return 1
107+    fi
108+
109+    echo -n "$password" | clipcopy
110+    echo "✔ password for $service copied to clipboard"
111+
112+    # If there's a one time password, copy it to the clipboard after 5 seconds
113+    local totp
114+    if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
115+      sleep 10 && echo -n "$totp" | clipcopy
116+      echo "✔ TOTP for $service copied to clipboard"
117+    fi
118+
119+    (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
120+  }
121+}
122+
123 opswd "$@"