#!/usr/bin/env bash ## Copy a password using the password manager `pass` and open the URL specified ## in the password's file (e.g., `url: https//example.com/`). The username in ## the password's file (e.g., `user: johnsmith`) is appended to the URL as a ## fragment in case it needs to be cut and pasted. ## ## Usage: pass-use [] ## ## Options: ## -b Open the URL using this web browser unless a browser is ## specified in the password's file (e.g., `browser: firefox`). ## If this option is excluded and no browser is specified in ## the password's file, the default browser will be used. ## ## Dependencies: fzf, GNU coreutils, GNU sed, pass, ripgrep, xdg-utils set -euo pipefail shopt -s inherit_errexit die() { echo "$(basename "$0"): $1" >&2; exit 1; } [[ " $* " =~ ' --help ' ]] && sed -n 's/^## \?//p' "$0" && exit while getopts b: opt; do case $opt in b) browser=$OPTARG ;; *) exit 1 ;; esac done shift "$(( OPTIND - 1 ))" # Get password name. cd "${PASSWORD_STORE_DIR:-~/.password-store}" password_name=$(fzf | sed 's/\.gpg$//') # Copy password. pass show -c "$password_name" # Open URL. file_text=$(pass show "$password_name") url=$(rg -r '$1' '^url: (.*)' <<< "$file_text") || die 'URL not found' url+=#$(sed -n 's/^user: //p' <<< "$file_text") # Append username to URL. browser=$(rg -r '$1' '^browser: (.*)' <<< "$file_text" || echo "${browser:-xdg-open}") type "${browser/ *}" &> /dev/null || die "command not found: $browser" $browser "$url" &> /dev/null &