summary refs log tree commit diff
path: root/bin/secretmenu
diff options
context:
space:
mode:
Diffstat (limited to 'bin/secretmenu')
-rwxr-xr-xbin/secretmenu47
1 files changed, 24 insertions, 23 deletions
diff --git a/bin/secretmenu b/bin/secretmenu
index 8c447fb..0ae5e43 100755
--- a/bin/secretmenu
+++ b/bin/secretmenu
@@ -3,18 +3,17 @@
 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
-
-secretmgr=rbw
-printpass=
-while getopts bpPh name; do
-	case ${name} in
+secretmgr=${_SECM_MGR:-rbw}
+printpass=${_SECM_PRINT:-}
+listandexit=
+while getopts bpPlh opt; do
+	case ${opt} in
 	b) secretmgr=rbw ;;
 	p) secretmgr=pass ;;
 	P) printpass=1 ;;
+	l) listandexit=1 ;;
 	h | ?)
-		echo "usage: secretmenu [-bp] [-P] [query]"
+		echo "usage: secretmenu [-bp] [-P] [-l] [query]"
 		echo
 		exit 2
 		;;
@@ -30,17 +29,12 @@ for tool in bemenu fd flock fzf $secretmgr; do
 	fi
 done
 
-# ensure only one menu runs at a time
-lockfile="$XDG_RUNTIME_DIR"/secretmenu.lock
-[ "$FLOCKER" != "$lockfile" ] && exec env FLOCKER="$lockfile" flock -ew 30 "$lockfile" "$0" "$@"
-trap '{ env UNSAFE=1 rm $lockfile 2>/dev/null || true; }' EXIT TERM INT
-
 __menu() {
 	if [ -t 1 ]; then
 		echo "terminal mode not implemented"
 		exit 1
 	fi
-	MENU_PROMPT="󰌆 secret" menu --list=10 --accept-single ${@:+--filter "$@"}
+	MENU_PROMPT="󰌆 secret" menu -l10 --accept-single ${@:+--filter "$@"}
 }
 
 if [ "$secretmgr" = "rbw" ] && command -v rbw >/dev/null 2>&1; then
@@ -70,12 +64,25 @@ if [ -n "$printpass" ]; then
 	}
 else
 	__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") >/dev/stderr; exit(1);}}'
+		awk 'NR==1 {system("wl-copy -n \"" $0 "\"");hit+=1} /^Username: / {gsub("Username: ", "", $0); system("wl-copy -n -p \"" $0 "\"");hit+=1} END{if(hit==0){print("no secret found") >/dev/stderr; exit(1);}}'
 	}
 fi
 
 # main
 
+if [ -n "$listandexit" ]; then
+	__list_secrets
+	exit 0
+fi
+
+# ensure only one menu runs at a time
+lockfile="$XDG_RUNTIME_DIR"/secretmenu.lock
+[ "$FLOCKER" != "$lockfile" ] && exec env FLOCKER="$lockfile" _SECM_MGR="$secretmgr" _SECM_PRINT="$printpass" flock -ew 30 "$lockfile" "$0" "$@"
+trap '{ env UNSAFE=1 rm $lockfile 2>/dev/null || true; }' EXIT TERM INT
+
+ico=$(iconez -fs -n dialog-password-symbolic)
+trap '{ notify-send -a secretmenu -i "${ico}" -c error "secretmenu: failed :(" "<i>pipefail</i>"; exit 1; }' ERR
+
 pw=$({
 	__list_secrets
 	echo ">>pass"
@@ -100,12 +107,6 @@ if [ "$__ok" -eq 0 ]; then
 		-i "${ico}" \
 		'secretmenu: secret copied' \
 		'<i>will be removed after next paste</i>'
-else
-	notify-send \
-		-a passmenu \
-		-i "${ico}" \
-		-c error \
-		'secretmenu: failed :(' \
-		"<i>check stderr for details</i>"
-	exit 1
 fi
+
+exit "$__ok"