]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/1password/opswd
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / 1password / opswd
1 #autoload
2
3 # opswd puts the password of the named service into the clipboard. If there's a
4 # one time password, it will be copied into the clipboard after 10 seconds. The
5 # clipboard is cleared after another 20 seconds.
6 function opswd() {
7   if [[ $# -lt 1 ]]; then
8     echo "Usage: opswd <service>"
9     return 1
10   fi
11
12   local service=$1
13
14   # If not logged in, print error and return
15   op user list > /dev/null || return
16
17   local password
18   # Copy the password to the clipboard
19   if ! password=$(op item get "$service" --fields password 2>/dev/null); then
20     echo "error: could not obtain password for $service"
21     return 1
22   fi
23
24   echo -n "$password" | clipcopy
25   echo "✔ password for $service copied to clipboard"
26
27   # If there's a one time password, copy it to the clipboard after 10 seconds
28   local totp
29   if totp=$(op item get --otp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
30     sleep 10 && echo -n "$totp" | clipcopy
31     echo "✔ TOTP for $service copied to clipboard"
32   fi
33
34   (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
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   print -ru2 ${(%):-"%F{yellow}opswd: usage with op version $(op --version) is deprecated. Upgrade to CLI 2 and reload zsh.
41 For instructions, see https://developer.1password.com/docs/cli/upgrade.%f"}
42
43   # opswd puts the password of the named service into the clipboard. If there's a
44   # one time password, it will be copied into the clipboard after 10 seconds. The
45   # clipboard is cleared after another 20 seconds.
46   function opswd() {
47     if [[ $# -lt 1 ]]; then
48       echo "Usage: opswd <service>"
49       return 1
50     fi
51
52     local service=$1
53
54     # If not logged in, print error and return
55     op list users > /dev/null || return
56
57     local password
58     # Copy the password to the clipboard
59     if ! password=$(op get item "$service" --fields password 2>/dev/null); then
60       echo "error: could not obtain password for $service"
61       return 1
62     fi
63
64     echo -n "$password" | clipcopy
65     echo "✔ password for $service copied to clipboard"
66
67     # If there's a one time password, copy it to the clipboard after 5 seconds
68     local totp
69     if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
70       sleep 10 && echo -n "$totp" | clipcopy
71       echo "✔ TOTP for $service copied to clipboard"
72     fi
73
74     (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
75   }
76 }
77
78 opswd "$@"