aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorGravatar Étienne Deparis 2015-04-28 11:54:16 +0200
committerGravatar Étienne Deparis 2018-08-09 19:03:46 +0200
commitff175677136f1fdfb40fbb3bf1efbb3248a9ea20 (patch)
tree0a322f4d824360b15b0c5ea0afe34df9ca372c0a
parent74fdfb5022f317ad48d449e29543710bdad1afda (diff)
downloadpassword-store-ff175677136f1fdfb40fbb3bf1efbb3248a9ea20.tar.gz
password-store-ff175677136f1fdfb40fbb3bf1efbb3248a9ea20.tar.xz
password-store-ff175677136f1fdfb40fbb3bf1efbb3248a9ea20.zip
feat: Add a fuzzy password retrieval method
You are no longer required to first type `pass find toto` in order to remember exactly where you put your toto password. If several password file match, the result of pass find is displayed. If only one match, its content are displayed.
-rwxr-xr-xsrc/password-store.sh51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/password-store.sh b/src/password-store.sh
index 7264ffc..9076381 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -262,7 +262,7 @@ cmd_usage() {
Selectively reencrypt existing passwords using new gpg-id.
$PROGRAM [ls] [subfolder]
List passwords.
- $PROGRAM find pass-names...
+ $PROGRAM find [--flat,-f] pass-names...
List passwords that match pass-names.
$PROGRAM [show] [--clip[=line-number],-c[line-number]] pass-name
Show existing password and optionally put it on the clipboard.
@@ -360,6 +360,17 @@ cmd_show() {
local pass
local path="$1"
+ if [[ -n "$1" ]] && [[ `expr index "$path" '/'` -eq 0 ]]; then
+ local allitems=($(find $PREFIX -type f -iname "*${path}*" | sed "s|$PREFIX/||"))
+ local last_match=${#allitems[@]}
+ if [[ $last_match -gt 1 ]]; then
+ cmd_find -f "$@"
+ exit
+ elif [[ -n $allitems ]]; then
+ path=${allitems:0: -4}
+ echo "Found one matching item: $path"
+ fi
+ fi
local passfile="$PREFIX/$path.gpg"
check_sneaky_paths "$path"
if [[ -f $passfile ]]; then
@@ -380,7 +391,7 @@ cmd_show() {
if [[ -z $path ]]; then
echo "Password Store"
else
- echo "${path%\/}"
+ echo -e "\e[1;34m${path%\/}\e[0m"
fi
tree -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of line, but keep colors
elif [[ -z $path ]]; then
@@ -391,10 +402,35 @@ cmd_show() {
}
cmd_find() {
- [[ $# -eq 0 ]] && die "Usage: $PROGRAM $COMMAND pass-names..."
+ local opts flat=0
+ opts="$($GETOPT -o f -l flat -n "$PROGRAM" -- "$@")"
+ local err=$?
+ eval set -- "$opts"
+ while true; do case $1 in
+ -f|--flat) flat=1; shift ;;
+ --) shift; break ;;
+ esac done
+ [[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--flat,-f] pass-names..."
+
IFS="," eval 'echo "Search Terms: $*"'
- local terms="*$(printf '%s*|*' "$@")"
- tree -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
+
+ if [[ $flat -eq 1 ]]; then
+ local terms=$(printf '%s[^/]*\|' "$@")
+ terms=${terms%"\|"}
+ local allitems=($(find $PREFIX -type f -iregex ".*/[^/]*\(${terms}\)" | sed "s|$PREFIX/||"))
+ local last_match=$((${#allitems[@]} - 1))
+ for item in $(seq 0 $last_match); do
+ local possible_pass=${allitems[item]}
+ if [[ $item -eq $last_match ]]; then
+ echo "└── "${possible_pass:0: -4}
+ else
+ echo "├── "${possible_pass:0: -4}
+ fi
+ done
+ else
+ local terms="*$(printf '%s*|*' "$@")"
+ tree -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
+ fi
}
cmd_grep() {
@@ -699,3 +735,8 @@ case "$1" in
*) cmd_extension_or_show "$@" ;;
esac
exit 0
+
+# Local Variables:
+# indent-tabs-mode: t
+# tab-width: 4
+# End: