forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-first-matching-password.sh
executable file
·60 lines (50 loc) · 1.63 KB
/
copy-first-matching-password.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Dependencies:
# 1. The Bitwarden CLI: https://bitwarden.com/help/article/cli/
# 2. The `jq` utility: https://stedolan.github.io/jq/
#
# Install via homebrew: `brew install bitwarden-cli jq`
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Copy First Matching Password
# @raycast.mode silent
#
# Optional parameters:
# @raycast.packageName Bitwarden
# @raycast.icon images/bitwarden.png
# @raycast.argument1 { "type": "text", "placeholder": "Query" }
#
# Documentation
# @raycast.author Phil Salant
# @raycast.authorURL https://github.com/PSalant726
# @raycast.description Search all items in a Bitwarden vault, and copy the password of the first search result to the clipboard.
notFound() {
echo "The query '${BASH_ARGV[0]}' did not return a password."
exit 1
}
if ! command -v bw &> /dev/null; then
echo "The Bitwarden CLI is not installed."
exit 1
elif ! command -v jq &> /dev/null; then
echo "The jq utility is not installed."
exit 1
fi
token=$(security find-generic-password -a ${USER} -s raycast-bitwarden -w 2> /dev/null)
token_status=$?
session=""
if [ $token_status -eq 0 ]; then
session="--session $token"
fi
bw unlock --check $session > /dev/null 2>&1
unlocked_status=$?
if [ $unlocked_status -ne 0 ]; then
echo "Vault is locked!"
exit 1
fi
item=$(bw list items --search "$1" $session 2> /dev/null | jq ".[0] | { name: .name, password: .login.password }")
name=$(echo $item | jq --exit-status ".name") || notFound
password=$(echo $item | jq --raw-output --exit-status ".password") || notFound
echo -n $password | pbcopy
unset password
echo "Copied the password for '$name' to the clipboard."
exit 0