summary refs log tree commit diff
path: root/bin/secretmenu
blob: 42399cc4fd43215f555a5e8fe6841aab07ca9f51 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash

set -eo pipefail
[ -n "$DEBUG" ] && set -x

ico=$(iconez -fs -n dialog-password-symbolic)
trap '{ notify-send -a secretmenu -i "${ico}" -c error "secretmenu: failed :(" "<i>pipefail</i>"; exit 1; }' ERR

if [ "$1" = "-b" ]; then
	secretmgr=rbw
	__list_secrets() {
		rbw list
	}
	__query_secret() {
		rbw get "$1" 2>&1
	}
else
	secretmgr=pass
	__list_secrets() {
		prefix=${PASSWORD_STORE_DIR-~/.password-store}
		prefix=$(readlink -f -- "${prefix}")
		fd -e gpg . "${prefix}" | sed -e "s#${prefix}/##" -e 's#\.gpg$##'
	}
	__query_secret() {
		pass show "$1" 2>&1
	}
fi

__handle() {
	awk 'NR==1 {system("wl-copy -n " $0);hit+=1} /^Username:/ {system("wl-copy -n -p " $2);hit+=1} END{if(hit==0) print("no secret found")}'
}

# check requirements
for tool in bemenu fd fzf $secretmgr; do
	if ! command -v "$tool" >/dev/null; then
		printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2
		exit 1
	fi
done

# TODO:
# typeit=
# if [ "$1" = "--type" ]; then
# 	typeit=1
# 	shift
# fi

__menu() {
	if [ ! -t 1 ]; then
		MENU_PROMPT="󰌆 secret" menu
	else
		fzf
	fi
}

pw=$({
	__list_secrets
	echo ">>pass"
	echo ">>bw"
} | __menu "$@")

case "$pw" in
"") exit 1 ;;
">>pass") exec "$0" ;;
">>bw") exec "$0" -b ;;
esac

err=$(__query_secret "$pw" | __handle)

# shellcheck disable=SC2181
if [ "$?" -ne 0 ] || [ -n "$err" ]; then
	notify-send \
		-a passmenu \
		-i "${ico}" \
		-c error \
		'secretmenu: failed :(' \
		"<i>${err}</i>"
	exit 1
fi

notify-send \
	-a secretmenu \
	-i "${ico}" \
	'secretmenu: secret copied' \
	'<i>will be removed after next paste</i>'