-
-
Save torbiak/7f4e50a304fd5ed1f45634950603257d to your computer and use it in GitHub Desktop.
Copy passwords from a vim-encrypted file to the clipboard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
IFS=$'\t\n' | |
set -eu | |
set -o pipefail | |
PASS_FILE=${PASS_FILE:-~/notes/numbers.md} | |
PASS_CLIP_TIME=${PASS_CLIP_TIME:-30} | |
not_found=2 | |
no_password=3 | |
function put { | |
case $(uname) in | |
CYGWIN*) putclip;; | |
Darwin*) pbcopy;; | |
*) xsel;; | |
esac | |
} | |
function get { | |
case $(uname) in | |
CYGWIN*) putclip;; | |
Darwin*) pbpaste;; | |
*) xsel;; | |
esac | |
} | |
# remove pairs with capitalized keys or where the key is `pass`. | |
function scrub { | |
sed -re 's/pass=[^,]*(,|$)//' -e 's/,$//' -e 's/(^|,)[A-Z][^=]*=[^,]*(,|$)//' | |
} | |
function clip { | |
local line=$(vimdecrypt $PASS_FILE | fgrep -m 1 -i "name=$1") | |
[[ -z "$line" ]] && exit $not_found | |
echo $line | scrub | |
if ! [[ $line =~ pass=([^,]*)(,|$) ]]; then | |
exit $no_password | |
fi | |
local pass=${BASH_REMATCH[1]} | |
echo $pass | put | |
( | |
sleep $PASS_CLIP_TIME | |
[[ "$(get)" == "$pass" ]] && echo "" | put | |
) & | |
} | |
function list { | |
vimdecrypt $PASS_FILE | fgrep -i "name=${1:-}" | scrub | |
} | |
cmd=${1:-} | |
case $cmd in | |
l*) list ${2:-};; | |
c*) clip ${2:?No pattern given};; | |
*) echo "\ | |
pass list [PATTERN] | |
pass clip PATTERN | |
PATTERN is a prefix of an entry name." | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment