diff options
-rwxr-xr-x | pt | 51 |
1 files changed, 46 insertions, 5 deletions
@@ -60,23 +60,64 @@ function check_env { } +# +# Gets a field's value from the specified [multiline] string, using the +# specified delimiter. +# +# @param content Content to extract field value from +# @param delim Delimiter seperating fields and values +# @param field Field name to get value for +# +getfield() { + local _content="${1:-}" + local _delim="${2:-}" + local _field="${3:-}" + + printf "${_content}" \ + | sed -n "s/${_field}${_delim} *\(.*\)/\1/p" \ + | head -n 1 +} + + function main { + local _passentry # Contents of the requested pass entry + local _passpassword # Password from the pass entry + local _passusername # Username field from the pass entry + local _passdelim # UI Field delimiter (Tab, Return), from the pass entry + local _passsubmit # Boolean submit (1,y,yes...), from the pass entry + + local _delim=Tab # Default delimiter if not specified in pass entry + local _submit=0 # Default submit value if not specified in pass entry + # Exit failure if no password was specified [[ -z ${1} ]] && log "Please specify a password to be typed" && exit 1 check_env || exit 0 # Check if the password exists in the store - pass ${@} &> /dev/null + # Also, copy the contents into a variable if it exists + _passentry=$(pass ${@} 2>/dev/null) [[ $? -gt 0 ]] && log "Error: '${@}' is not in the password store" && exit 1 # Parse pass output into appropriate variables - local passfile="$(pass ${@})" - local username=$(echo -e "${passfile}" | sed -n 's/username: \(.*\)/\1/p' | head -n 1) - local pass=$(echo -e "${passfile}" | head -n 1) + _passpassword=$(printf "${_passentry}" | head -n1) + _passusername=$(getfield "${_passentry}" ':' username) + _passdelim=$(getfield "${_passentry}" ':' delim) + _passsubmit=$(getfield "${_passentry}" ':' submit) + + # If any of the known 'submit' values are specified, set submit to 1 + if [ "${_passsubmit}" = '1' ] \ + || [ "${_passsubmit}" = 'y' ] \ + || [ "${_passsubmit}" = 'yes' ]; then + _submit=1 + fi # Type the password - xdotool type --delay 15 "${username} ${pass}" + xdotool type --delay 15 "${_passusername}" + xdotool key "${_delim}" + xdotool type --delay 15 "${_passpassword}" + + [ "${_submit}" = 1 ] && xdotool key Return } main ${@} |