Parent directory

opswd

1413 bytes
 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.
 6function 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 username
18  # Copy the username to the clipboard
19  if ! username=$(op item get "$service" --fields username 2>/dev/null); then
20    echo "error: could not obtain username for $service"
21    return 1
22  fi
23
24  echo -n "$username" | clipcopy
25  echo "โœ” username for service $service copied to the clipboard. Press Enter to continue"
26  read
27
28  local password
29  # Copy the password to the clipboard
30  if ! password=$(op item get "$service" --reveal --fields password 2>/dev/null); then
31    echo "error: could not obtain password for $service"
32    return 1
33  fi
34
35  echo -n "$password" | clipcopy
36  echo "โœ” password for $service copied to clipboard. Press Enter to continue"
37  read
38
39  # If there's a one time password, copy it to the clipboard
40  local totp
41  if totp=$(op item get --otp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
42    echo -n "$totp" | clipcopy
43    echo "โœ” TOTP for $service copied to clipboard"
44  fi
45
46  (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
47}
48
49opswd "$@"