[PATCH 2/2] pass ls: Add --find flag

Alejandro Colomar alx at kernel.org
Sun Sep 24 22:47:29 UTC 2023


Similarly to `pass find --find`, this flag changes the `pass ls` command
to use find(1) instead of tree(1).

With this flag, the output is directly usable in a subsequent call to
`pass show`.

Examples of use:

	$ pass find --find kernel \
	  | tail -n -1 \
	  | xargs ./password-store.sh ls --find;
	www/foss/kernel/
	www/foss/kernel/smtp/
	www/foss/kernel/smtp/pass
	www/foss/kernel/bugzilla/
	www/foss/kernel/bugzilla/url
	www/foss/kernel/bugzilla/alx/
	www/foss/kernel/bugzilla/alx/id
	www/foss/kernel/bugzilla/alx/pass

	$ pass find --find kernel \
	  | tail -n -1 \
	  | xargs pass ls --find \
	  | tail -n1 \
	  | xargs pass show;
	pw

Signed-off-by: Alejandro Colomar <alx at kernel.org>
---

And here's a patch for pass ls.  These patches apply on top of the
formatting fixes for the manual page that I sent earlier.

Cheers,
Alex

 man/pass.1            | 15 +++++++++++++++
 src/password-store.sh | 20 ++++++++++++++------
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index b9764c5..2f4d858 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -92,6 +92,21 @@ .SH COMMANDS
 .BR tree (1)
 program.
 This command is alternatively named \fBlist\fP.
+If the
+.I \-\-find
+flag is used,
+.BR find (1)
+is used instead;
+this is useful to be able to reuse the output as an input to
+.IR "pass show" .
+Here's an example:
+.IP
+.in +4n
+.EX
+$ pass ls \-\-find foo/bar/ | tail \-n1 | xargs pass show
+.EE
+.in
+.TP
 .TP
 \fBgrep\fP [\fIGREPOPTIONS\fP] \fIsearch-string\fP
 Searches inside each decrypted password file for \fIsearch-string\fP, and displays line
diff --git a/src/password-store.sh b/src/password-store.sh
index bf0e446..966c245 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -366,11 +366,12 @@ cmd_init() {
 }
 
 cmd_show() {
-	local opts selected_line clip=0 qrcode=0
-	opts="$($GETOPT -o q::c:: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
+	local opts selected_line clip=0 find=0 qrcode=0
+	opts="$($GETOPT -o q::c:: -l qrcode::,clip::,find -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
+		--find) find=1; shift ;;
 		-q|--qrcode) qrcode=1; selected_line="${2:-1}"; shift 2 ;;
 		-c|--clip) clip=1; selected_line="${2:-1}"; shift 2 ;;
 		--) shift; break ;;
@@ -397,12 +398,19 @@ cmd_show() {
 			fi
 		fi
 	elif [[ -d $PREFIX/$path ]]; then
-		if [[ -z $path ]]; then
-			echo "Password Store"
+		if [[ $find -eq 1 ]]; then
+			local n="$(echo "$PREFIX" | wc -c)"
+
+			find -L "$PREFIX/$path" | sed '/[^\/]$/s,$,/,' | sed "s/^.\{$n\}//" | sed 's,\.gpg/$,,'
 		else
-			echo "${path%\/}"
+			if [[ -z $path ]]; then
+				echo "Password Store"
+			else
+				echo "${path%\/}"
+			fi
+
+			tree -N -C -l --noreport "$PREFIX/$path" 3>&- | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of line, but keep colors
 		fi
-		tree -N -C -l --noreport "$PREFIX/$path" 3>&- | 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
 		die "Error: password store is empty. Try \"pass init\"."
 	else
-- 
2.40.1



More information about the Password-Store mailing list