refactor to use POSIX sh (linted with shellcheck)

this is the initial work needed to convert common bash-isms to POSIX
compliant solutions. This is mostly untested so probably some work will
need to be done to standardize now that the script is following best
practices for quotes, whitespace separation, and the like.
This commit is contained in:
Joel Beckmeyer 2021-11-30 10:41:47 -05:00
parent 68bc2ff66b
commit 6b340362d5

View File

@ -1,13 +1,13 @@
#!/usr/bin/env bash #!/bin/sh
set -euo pipefail set -eu
TYPE_CMD="wtype -" TYPE_CMD="wtype -"
help=0 help=0
autotype=0 autotype=0
_trim() { _trim() {
local var="$*" var="$*"
# remove leading whitespace characters # remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}" var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters # remove trailing whitespace characters
@ -17,25 +17,26 @@ _trim() {
_parse_fields() { _parse_fields() {
has_username=0 has_username=0
IFS=$'\n' # Note: \n can not be last, or it will be stripped by $()
fields="$(pass show $password | tail -n +2 | cut -d: -f1 -s)" IFS=$(printf '\n\t')
field_list+="password\n" fields="$(pass show "$password" | tail -n +2 | cut -d: -f1 -s)"
field_list="${field_list}password\n"
for line in $fields; do for line in $fields; do
if [[ $line == "username" ]]; then if [ "$line" = "username" ]; then
has_username=1 has_username=1
field_list+="$line\n" field_list="${field_list}$line\n"
elif [[ $line == "otpauth" ]]; then elif [ "$line" = "otpauth" ]; then
field_list+="OTP\n" field_list="${field_list}OTP\n"
elif [[ $line == autotype_always ]]; then elif [ "$line" = autotype_always ]; then
autotype=1 autotype=1
else else
field_list+="$line\n" field_list="${field_list}$line\n"
fi fi
done done
if [[ $typeit -eq 1 ]] && [[ $has_username -eq 1 ]]; then if [ "$typeit" -eq 1 ] && [ "$has_username" -eq 1 ]; then
printf "autotype\n" printf "autotype\n"
fi fi
printf "$field_list" printf '%s' "$field_list"
unset IFS unset IFS
} }
@ -44,12 +45,12 @@ _pass_field() {
} }
_pass_get() { _pass_get() {
if [[ $1 == "password" ]]; then if [ "$1" = "password" ]; then
pass show "$password" | { IFS= read -r pass; printf %s "$pass"; } pass show "$password" | { IFS= read -r pass; printf %s "$pass"; }
elif [[ $1 == "OTP" ]]; then elif [ "$1" = "OTP" ]; then
pass otp "$password" | tail -n1 | { IFS= read -r pass; printf %s "$pass"; } pass otp "$password" | tail -n1 | { IFS= read -r pass; printf %s "$pass"; }
else else
printf "$(_pass_field $*)" printf '%s' "$(_pass_field "$@")"
fi fi
} }
@ -61,8 +62,6 @@ _usage() {
printf " -t, --type type the selection instead of copying to clipboard\n" printf " -t, --type type the selection instead of copying to clipboard\n"
} }
shopt -s nullglob globstar
OPTS="$(getopt --options ahst --longoptions autotype,help,squash,type -n 'wofi-pass' -- "$@")" OPTS="$(getopt --options ahst --longoptions autotype,help,squash,type -n 'wofi-pass' -- "$@")"
eval set -- "$OPTS" eval set -- "$OPTS"
while true; do while true; do
@ -76,34 +75,32 @@ while true; do
esac esac
done done
if [[ $help -eq 1 ]]; then if [ "$help" -eq 1 ]; then
echo "$(_usage)" >&2 _usage >&2
exit 0 exit 0
fi fi
prefix=${PASSWORD_STORE_DIR-~/.password-store} prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg ) password_files="$(find "$prefix" -name "*.gpg" -execdir basename {} .gpg ';')"
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
password=$(printf '%s\n' "${password_files[@]}" | wofi --dmenu "$@") password=$(printf '%s' "$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)" field_count="$(echo "$field_list" | wc -l)"
if [[ $squash -eq 1 ]] && [[ $field_count -eq 1 ]]; then if [ "$squash" -eq 1 ] && [ "$field_count" -eq 1 ]; then
field="password" field="password"
elif [[ $autotype -ne 1 ]]; then elif [ "$autotype" -ne 1 ]; then
field=$(printf "$field_list" | wofi --dmenu) field=$(printf '%s' "$field_list" | wofi --dmenu)
fi fi
if [[ $typeit -eq 0 ]]; then if [ $typeit -eq 0 ]; then
wl-copy "$(_pass_get $field)" wl-copy "$(_pass_get "$field")"
else else
if [[ $field == "autotype" ]] || [[ $autotype -eq 1 ]]; then if [ "$field" = "autotype" ] || [ "$autotype" -eq 1 ]; then
username=$(_pass_get "username") username=$(_pass_get "username")
password=$(_pass_get "password") password=$(_pass_get "password")
printf "%b\t%b\n" "${username}" "${password}" | $TYPE_CMD printf "%b\t%b\n" "${username}" "${password}" | $TYPE_CMD
else else
_pass_get $field | $TYPE_CMD _pass_get "$field" | "$TYPE_CMD"
fi fi
fi fi