#!/usr/bin/env bash shopt -s nullglob globstar output_method=("cat") action_descr="" if [[ $1 == "--clipboard" ]]; then if [[ -n $DISPLAY ]]; then output_method=("xclip" "-selection" "clipboard") action_descr="copied to clipboard" else echo "Only X11 is supported" exit 1 fi shift fi if [[ $1 == "--type" ]]; then if [[ -n $WAYLAND_DISPLAY ]]; then xdotool=("ydotool" "type" "--file" "-") elif [[ -n $DISPLAY ]]; then xdotool=("xdotool" "type" "--clearmodifiers" "--file" "-") else echo "Error: No Wayland or X11 display detected" >&2 exit 1 fi #output_method=("{ IFS= read -r pass; printf %s $pass; } | $xdotool") output_method=("${xdotool[@]}") action_descr="typed" shift fi #echo ${output_method[@]} if [[ -n $WAYLAND_DISPLAY ]]; then dmenu="dmenu-wl" elif [[ -n $DISPLAY ]]; then dmenu="$HOME/.local/bin/cdmenu" else echo "Error: No Wayland or X11 display detected" >&2 exit 1 fi prefix=${PASSWORD_STORE_DIR-~/.password-store} password_files=( "$prefix"/**/*.gpg ) password_files=( "${password_files[@]#"$prefix"/}" ) password_files=( "${password_files[@]%.gpg}" ) password_file=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -l 30 "$@") if [[ -z "$password_file" ]]; then exit 1 fi if [[ ! ${password_files[*]} =~ "${password_file}" ]]; then notify-send -u critical "No entry \"$password_file\" in the password store" exit 1 fi action=$(printf "password\nlogin\nOTP\ninfo" | "$dmenu" -l 4) case $action in login) login=$(pass show "$password_file" | sed -n -e '/^login:/p' | awk '{print $2}') if [[ -z $login ]]; then notify-send -u critical "No login for \"$password_file\" was found" exit 1 fi printf "%s" "$login" | "${output_method[@]}" if [[ -n $action_descr ]]; then notify-send "Login for \"$password_file\" was $action_descr" fi ;; password) password=$(pass show "$password_file" | head -n 1) if [[ -z $password ]]; then notify-send -u critical "No password for \"$password_file\" was found" exit 1 fi printf "%s" "$password" | "${output_method[@]}" if [[ -n $action_descr ]]; then notify-send "Password for \"$password_file\" was $action_descr" fi ;; OTP) otp=$(pass otp "$password_file") if [[ -z $otp ]]; then notify-send -u critical "No OTP for \"$password_file\" was found" exit 1 fi printf "%s" "$otp" | "${output_method[@]}" if [[ -n $action_descr ]]; then notify-send "OTP for \"$password_file\" was $action_descr" fi ;; info) data="$(pass show "$password_file" | tail -n +2 | sed '/login\:/d')" if [[ -z $data ]]; then notify-send -u critical "No additional info for \"$password_file\" was found" exit 1 fi data="$(echo "$data" | "$dmenu" -l 30)" if [[ -z "$data" ]]; then notify-send "No info for $password_file was selected" exit 1 fi do_trim=$(printf 'Yes\nNo' | "$dmenu" -p "Trim \"$(echo "$data" | awk '{print $1}')\" in front?" -l 2) if [[ $do_trim == "Yes" ]]; then data=$(echo "$data" | cut -f 2- -d ' ') fi printf "%s" "$data" | "${output_method[@]}" if [[ -n $action_descr ]]; then notify-send "Data for $password_file was $action_descr" fi ;; esac