6df14641ac48b380c56e1c72aa86b57861fbfb70

Author
Gheritarish <ilissiae@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(1password): add username copy to `opswd` (#10812)

Diff

 1diff --git a/plugins/1password/README.md b/plugins/1password/README.md
 2index f6854da533e4e56cde42011a97dc155a912b2623..ace6da8e199057e257db10fbcc80f06efa54d392 100644
 3--- a/plugins/1password/README.md
 4+++ b/plugins/1password/README.md
 5@@ -14,16 +14,18 @@ clipboard.
 6 ## `opswd`
 7 
 8 The `opswd` command is a wrapper around the `op` command. It takes a service
 9-name as an argument and copies the password for that service to the clipboard.
10+name as an argument and copies the username, then the password for that service
11+to the clipboard, after confirmation on the user part.
12 
13-If the service also contains a TOTP, it is copied to the clipboard after 10 seconds.
14-Finally, after 20 seconds, the clipboard is cleared.
15+If the service also contains a TOTP, it is copied to the clipboard after confirmation
16+on the user part. Finally, after 20 seconds, the clipboard is cleared.
17 
18-The function has completion support, so you can use tab completion to select
19-which service you want to get.
20+For example, `opswd github.com` will put your GitHub username into your clipboard. Then,
21+it will ask for confirmation to continue, and copy the password to your clipboard. Finally,
22+if a TOTP is available, it will be copied to the clipboard after your confirmation.
23 
24-For example, `opswd github.com` will put your GitHub password into your clipboard, and if
25-a TOTP is available, it will be copied to the clipboard after 10 seconds.
26+This function has completion support, so you can use tab completion to select which
27+service you want to get.
28 
29 > NOTE: you need to be signed in for `opswd` to work. If you are using biometric unlock,
30 > 1Password CLI will automatically prompt you to sign in. See:
31diff --git a/plugins/1password/opswd b/plugins/1password/opswd
32index 57672807ef69fd1c7c31c475668219c6ec9960fe..0f667d2ffe8bea74af2219674ad75e28f7818378 100644
33--- a/plugins/1password/opswd
34+++ b/plugins/1password/opswd
35@@ -14,6 +14,17 @@ function opswd() {
36   # If not logged in, print error and return
37   op user list > /dev/null || return
38 
39+  local username
40+  # Copy the username to the clipboard
41+  if ! username=$(op item get "$service" --fields username 2>/dev/null); then
42+    echo "error: could not obtain username for $service"
43+    return 1
44+  fi
45+
46+  echo -n "$username" | clipcopy
47+  echo "✔ username for service $service copied to the clipboard. Press Enter to continue"
48+  read
49+
50   local password
51   # Copy the password to the clipboard
52   if ! password=$(op item get "$service" --fields password 2>/dev/null); then
53@@ -22,12 +33,13 @@ function opswd() {
54   fi
55 
56   echo -n "$password" | clipcopy
57-  echo "✔ password for $service copied to clipboard"
58+  echo "✔ password for $service copied to clipboard. Press Enter to continue"
59+  read
60 
61-  # If there's a one time password, copy it to the clipboard after 10 seconds
62+  # If there's a one time password, copy it to the clipboard
63   local totp
64   if totp=$(op item get --otp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
65-    sleep 10 && echo -n "$totp" | clipcopy
66+    echo -n "$totp" | clipcopy
67     echo "✔ TOTP for $service copied to clipboard"
68   fi
69