added better help message and intelligent squashing

This commit is contained in:
Joel Beckmeyer 2020-03-30 20:18:22 -04:00
parent 16b747fc50
commit 7aa90cda15

View File

@ -1,7 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
shopt -s nullglob globstar
_trim() { _trim() {
local var="$*" local var="$*"
# remove leading whitespace characters # remove leading whitespace characters
@ -12,19 +10,24 @@ _trim() {
} }
_parse_fields() { _parse_fields() {
has_username=0
IFS=$'\n' IFS=$'\n'
fields="$(pass show $password | tail -n +2 | cut -d: -f1 -s)" fields="$(pass show $password | tail -n +2 | cut -d: -f1 -s)"
if [[ $typeit -eq 1 ]]; then field_list+="password\n"
printf "autotype\n"
fi
printf "password\n"
for line in $fields; do for line in $fields; do
if [[ $line == "otpauth" ]]; then if [[ $line == "username" ]]; then
printf "OTP\n" has_username=1
field_list+="$line\n"
elif [[ $line == "otpauth" ]]; then
field_list+="OTP\n"
else else
printf "$line\n" field_list+="$line\n"
fi fi
done done
if [[ $typeit -eq 1 ]] && [[ $has_username -eq 1 ]]; then
printf "autotype\n"
fi
printf "$field_list"
unset IFS unset IFS
} }
@ -49,10 +52,30 @@ _pass_get() {
fi fi
} }
typeit=0 _usage() {
if [[ $1 == "--type" ]]; then printf "Usage: wofi-pass [options]\n"
typeit=1 printf " -h, --help show this help message\n"
shift printf " -s, --squash-choice don't show field choice if password file only contains password\n"
printf " -t, --type type the selection instead of copying to clipboard\n"
}
shopt -s nullglob globstar
OPTS="$(getopt --options hst --longoptions help,squash-choice,type -n 'wofi-pass' -- "$@")"
eval set -- "$OPTS"
while true; do
case "$1" in
-h | --help ) help=1; shift ;;
-s | --squash-choice) squash=1; shift ;;
-t | --type ) typeit=1; shift;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [[ $help -eq 1 ]]; then
echo "$(_usage)" >&2
exit 0
fi fi
prefix=${PASSWORD_STORE_DIR-~/.password-store} prefix=${PASSWORD_STORE_DIR-~/.password-store}
@ -63,8 +86,12 @@ password_files=( "${password_files[@]%.gpg}" )
password=$(printf '%s\n' "${password_files[@]}" | wofi --dmenu "$@") password=$(printf '%s\n' "${password_files[@]}" | wofi --dmenu "$@")
[[ -n $password ]] || exit [[ -n $password ]] || exit
field_list="$(_parse_fields)" field_list="$(_parse_fields)"
field_count="$(echo "$field_list" | wc -l)"
if [[ squash -eq 1 ]] && [[ field_count -eq 1 ]]; then
field="password"
else
field=$(printf "$field_list" | wofi --dmenu) field=$(printf "$field_list" | wofi --dmenu)
echo $field fi
if [[ $typeit -eq 0 ]]; then if [[ $typeit -eq 0 ]]; then
wl-copy "$(_pass_get $field)" wl-copy "$(_pass_get $field)"