diff options
| author | Robert Günzler <r@gnzler.io> | 2023-11-02 06:55:19 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2023-11-02 06:58:29 +0100 |
| commit | 0eddc8b11aa6777bf322ff2823b28fa47e00fc08 (patch) | |
| tree | 61737abd43ba2b369efd3b1b92436144e4bd3f1c /bin | |
| parent | c20ca44a7cf009e2c8d76f0c3c2ae9254ee39f61 (diff) | |
bin/secretmenu: all in on rbw
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rwxr-xr-x | bin/secretmenu | 71 |
1 files changed, 28 insertions, 43 deletions
diff --git a/bin/secretmenu b/bin/secretmenu index 0ae5e43..972ffbd 100755 --- a/bin/secretmenu +++ b/bin/secretmenu @@ -3,17 +3,16 @@ set -eo pipefail [ -n "$DEBUG" ] && set -x -secretmgr=${_SECM_MGR:-rbw} printpass=${_SECM_PRINT:-} +printraw=${_SECM_PRINTRAW:-} listandexit= -while getopts bpPlh opt; do +while getopts Plrh opt; do case ${opt} in - b) secretmgr=rbw ;; - p) secretmgr=pass ;; P) printpass=1 ;; + r) printraw=1 ;; l) listandexit=1 ;; h | ?) - echo "usage: secretmenu [-bp] [-P] [-l] [query]" + echo "usage: secretmenu [-bp] [-Pr] [-l] [query]" echo exit 2 ;; @@ -22,7 +21,7 @@ done shift $((OPTIND - 1)) # check requirements -for tool in bemenu fd flock fzf $secretmgr; do +for tool in bemenu fd flock fzf jq rbw; do if ! command -v "$tool" >/dev/null; then printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2 exit 1 @@ -37,34 +36,31 @@ __menu() { MENU_PROMPT=" secret" menu -l10 --accept-single ${@:+--filter "$@"} } -if [ "$secretmgr" = "rbw" ] && command -v rbw >/dev/null 2>&1; then - __list_secrets() { - rbw list --fields name,user,folder,id | column -t -s $'\t' - } - __query_secret() { - # query by the last space-separated item (id) - rbw get --full -- "${1##* }" 2>&1 - } -fi -if [ "$secretmgr" = "pass" ] && command -v pass >/dev/null 2>&1; then - __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 +__list_secrets() { + rbw list --fields name,user,folder,id | column -t -s $'\t' +} -if [ -n "$printpass" ]; then +__query_secret() { + # query by the last space-separated item (id) + local id=${1##* } + + echo "$id" >&2 + rbw get --raw -- "$id" 2>&1 + # jq '.data + (.fields|map({(.name|@text|ascii_downcase): .value})|add)' -r | column -s: -t +} + +if [ -n "$printraw" ]; then __handle() { - awk 'NR==1 {print $0;hit+=1} END{if(hit==0){print("no secret found") >/dev/stderr; exit(1);}}' + jq } -else +elif [ -n "$printpass" ]; then __handle() { - 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);}}' + jq -r '.data.password' + } +else # copy to clipboard + __handle() { + jq -r '.data.username, .data.password' | + awk 'NR==2 {system("wl-copy -n \"" $0 "\"");hit+=1} NR==1 {system("wl-copy -n -p \"" $0 "\"");hit+=1} END{if(hit==0){print("no secret found") >/dev/stderr; exit(1);}}' } fi @@ -77,24 +73,13 @@ 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" "$@" +[ "$FLOCKER" != "$lockfile" ] && exec env FLOCKER="$lockfile" _SECM_PRINT="$printpass" _SECM_PRINTRAW="$printraw" 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" - echo ">>rbw" -} | - __menu "$@") - -case "$pw" in -"") exit 1 ;; -">>pass") exec "$0" -p ;; -">>rbw") exec "$0" -b ;; -esac +pw=$(__list_secrets | __menu "$@") __query_secret "$pw" | __handle __ok=$? |