Hello list,

I tried to do some scripting with ``pass grep'' and was unhappy with the
output format.  I suggest the attached patch.  It will simplify the logic of
the cmd_grep function and remove some redirection to shell variables of
(potentially big) matches.  At the same time it will change the output format
of the grep subcommand and thereby change the "public interface" of the API of
pass (if such a thing is of concern).  

The commit message could be:
> grep: Change output format and update man page.
>
> The grep option --color=auto is used to color the output if and only if output
> is to a terminal.  This makes it more suitable for scripts.
> 
> The grep option --label=$label is used (together with -H) to print any
> string to appear as the filename if grep searches stdin.  This makes it
> possible to display the password name on the same line as the match. This
> makes it more suitable for further line wise processing in scripts.
> 
> The man page falsely stated that the filename would be displayed with the
> matches.  Actually the password name is displayed.

Note about portability: The --color option was already used, the --label
option is present in GNU grep and according to
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/grep.1.html
http://www.unix.com/man-page/FreeBSD/1/grep/ also on Mac OS X and FreeBSD.
According to http://nixdoc.net/man-pages/OpenBSD/man1/grep.1.html it is not
present in OpenBSD (and -H has a different semantics) but --color doesn't seem
to be present either (so this patch does not break portability any further).
I do not know about Cygwin, as I did not find a man page online.

When I updated the man page my editor automatically deleted a lot of trailing
white space.  I'm sorry for this but I ran ``diff <(man 1 pass) <(man
man/pass.1)'' and checked that it does not change the formatted man page.

I'm happy to update the patch if there are some concerns.  Thanks for
developing pass and kind regards
  Lucas

---
 man/pass.1            | 164 +++++++++++++++++++++++++-------------------------
 src/password-store.sh |  14 ++---
 2 files changed, 87 insertions(+), 91 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index 0dd6952..11fa792 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -5,20 +5,20 @@ pass - stores, retrieves, generates, and synchronizes passwords securely
 
 .SH SYNOPSIS
 .B pass
-[ 
+[
 .I COMMAND
-] [ 
+] [
 .I OPTIONS
-]... [ 
+]... [
 .I ARGS
 ]...
 
 .SH DESCRIPTION
 
-.B pass 
-is a very simple password store that keeps passwords inside 
+.B pass
+is a very simple password store that keeps passwords inside
 .BR gpg2 (1)
-encrypted files inside a simple directory tree residing at 
+encrypted files inside a simple directory tree residing at
 .IR ~/.password-store .
 The
 .B pass
@@ -75,8 +75,8 @@ by using the
 program. This command is alternatively named \fBlist\fP.
 .TP
 \fBgrep\fP \fIsearch-string\fP
-Searches inside each decrypted password file for \fIsearch-string\fP, and displays line
-containing matched string along with filename. Uses
+Searches inside each decrypted password file for \fIsearch-string\fP, and displays matching
+lines along with password names. Uses
 .BR grep (1)
 for matching. Make use of the \fIGREP_OPTIONS\fP environment variable to set particular
 options.
@@ -165,36 +165,36 @@ Show version information.
 
 .TP
 Initialize password store
-.B zx2c4@laptop ~ $ pass init [email protected] 
+.B zx2c4@laptop ~ $ pass init [email protected]
 .br
-mkdir: created directory \[u2018]/home/zx2c4/.password-store\[u2019] 
+mkdir: created directory \[u2018]/home/zx2c4/.password-store\[u2019]
 .br
 Password store initialized for [email protected].
 .TP
 List existing passwords in store
-.B zx2c4@laptop ~ $ pass 
+.B zx2c4@laptop ~ $ pass
 .br
 Password Store
 .br
-\[u251C]\[u2500]\[u2500] Business 
+\[u251C]\[u2500]\[u2500] Business
 .br
-\[u2502]   \[u251C]\[u2500]\[u2500] some-silly-business-site.com 
+\[u2502]   \[u251C]\[u2500]\[u2500] some-silly-business-site.com
 .br
-\[u2502]   \[u2514]\[u2500]\[u2500] another-business-site.net 
+\[u2502]   \[u2514]\[u2500]\[u2500] another-business-site.net
 .br
-\[u251C]\[u2500]\[u2500] Email 
+\[u251C]\[u2500]\[u2500] Email
 .br
-\[u2502]   \[u251C]\[u2500]\[u2500] donenfeld.com 
+\[u2502]   \[u251C]\[u2500]\[u2500] donenfeld.com
 .br
-\[u2502]   \[u2514]\[u2500]\[u2500] zx2c4.com 
+\[u2502]   \[u2514]\[u2500]\[u2500] zx2c4.com
 .br
-\[u2514]\[u2500]\[u2500] France 
+\[u2514]\[u2500]\[u2500] France
 .br
-    \[u251C]\[u2500]\[u2500] bank 
+    \[u251C]\[u2500]\[u2500] bank
 .br
-    \[u251C]\[u2500]\[u2500] freebox 
+    \[u251C]\[u2500]\[u2500] freebox
 .br
-    \[u2514]\[u2500]\[u2500] mobilephone  
+    \[u2514]\[u2500]\[u2500] mobilephone
 .br
 
 .br
@@ -205,65 +205,65 @@ Find existing passwords in store that match .com
 .br
 Search Terms: .com
 .br
-\[u251C]\[u2500]\[u2500] Business 
+\[u251C]\[u2500]\[u2500] Business
 .br
-\[u2502]   \[u251C]\[u2500]\[u2500] some-silly-business-site.com 
+\[u2502]   \[u251C]\[u2500]\[u2500] some-silly-business-site.com
 .br
-\[u2514]\[u2500]\[u2500] Email 
+\[u2514]\[u2500]\[u2500] Email
 .br
-    \[u251C]\[u2500]\[u2500] donenfeld.com 
+    \[u251C]\[u2500]\[u2500] donenfeld.com
 .br
-    \[u2514]\[u2500]\[u2500] zx2c4.com 
+    \[u2514]\[u2500]\[u2500] zx2c4.com
 .br
 
 .br
 Alternatively, "\fBpass search .com\fP".
 .TP
 Show existing password
-.B zx2c4@laptop ~ $ pass Email/zx2c4.com 
+.B zx2c4@laptop ~ $ pass Email/zx2c4.com
 .br
-sup3rh4x3rizmynam3 
+sup3rh4x3rizmynam3
 .TP
 Copy existing password to clipboard
-.B zx2c4@laptop ~ $ pass -c Email/zx2c4.com 
+.B zx2c4@laptop ~ $ pass -c Email/zx2c4.com
 .br
 Copied Email/[email protected] to clipboard. Will clear in 45 seconds.
 .TP
 Add password to store
-.B zx2c4@laptop ~ $ pass insert Business/cheese-whiz-factory 
+.B zx2c4@laptop ~ $ pass insert Business/cheese-whiz-factory
 .br
 Enter password for Business/cheese-whiz-factory: omg so much cheese what am i gonna do
 .TP
-Add multiline password to store 
-.B zx2c4@laptop ~ $ pass insert -m Business/cheese-whiz-factory 
+Add multiline password to store
+.B zx2c4@laptop ~ $ pass insert -m Business/cheese-whiz-factory
 .br
-Enter contents of Business/cheese-whiz-factory and press Ctrl+D when finished: 
+Enter contents of Business/cheese-whiz-factory and press Ctrl+D when finished:
 .br
- 
+
 .br
-Hey this is my 
+Hey this is my
 .br
-awesome 
+awesome
 .br
-multi 
+multi
 .br
-line 
+line
 .br
-passworrrrrrrrd. 
+passworrrrrrrrd.
 .br
-^D 
+^D
 .TP
 Generate new password
-.B zx2c4@laptop ~ $ pass generate Email/jasondonenfeld.com 15 
+.B zx2c4@laptop ~ $ pass generate Email/jasondonenfeld.com 15
 .br
-The generated password to Email/jasondonenfeld.com is: 
+The generated password to Email/jasondonenfeld.com is:
 .br
 $(-QF&Q=IN2nFBx
 .TP
 Generate new alphanumeric password
 .B zx2c4@laptop ~ $ pass generate -n Email/jasondonenfeld.com 12
 .br
-The generated password to Email/jasondonenfeld.com is: 
+The generated password to Email/jasondonenfeld.com is:
 .br
 YqFsMkBeO6di
 .TP
@@ -273,9 +273,9 @@ Generate new password and copy it to the clipboard
 Copied Email/jasondonenfeld.com to clipboard. Will clear in 45 seconds.
 .TP
 Remove password from store
-.B zx2c4@laptop ~ $ pass remove Business/cheese-whiz-factory 
+.B zx2c4@laptop ~ $ pass remove Business/cheese-whiz-factory
 .br
-rm: remove regular file \[u2018]/home/zx2c4/.password-store/Business/cheese-whiz-factory.gpg\[u2019]? y 
+rm: remove regular file \[u2018]/home/zx2c4/.password-store/Business/cheese-whiz-factory.gpg\[u2019]? y
 .br
 removed \[u2018]/home/zx2c4/.password-store/Business/cheese-whiz-factory.gpg\[u2019]
 
@@ -284,13 +284,13 @@ Here, we initialize new password store, create a git repository, and then manipu
 .BR git-push (1)
 for more information.
 
-.B zx2c4@laptop ~ $ pass init [email protected] 
+.B zx2c4@laptop ~ $ pass init [email protected]
 .br
-mkdir: created directory \[u2018]/home/zx2c4/.password-store\[u2019] 
+mkdir: created directory \[u2018]/home/zx2c4/.password-store\[u2019]
 .br
-Password store initialized for [email protected]. 
+Password store initialized for [email protected].
 
-.B zx2c4@laptop ~ $ pass git init 
+.B zx2c4@laptop ~ $ pass git init
 .br
 Initialized empty Git repository in /home/zx2c4/.password-store/.git/
 .br
@@ -300,75 +300,75 @@ Initialized empty Git repository in /home/zx2c4/.password-store/.git/
 .br
  create mode 100644 .gpg-id
 
-.B zx2c4@laptop ~ $ pass git remote add origin kexec.com:pass-store 
+.B zx2c4@laptop ~ $ pass git remote add origin kexec.com:pass-store
 
-.B zx2c4@laptop ~ $ pass generate Amazon/[email protected] 21 
+.B zx2c4@laptop ~ $ pass generate Amazon/[email protected] 21
 .br
-mkdir: created directory \[u2018]/home/zx2c4/.password-store/Amazon\[u2019] 
+mkdir: created directory \[u2018]/home/zx2c4/.password-store/Amazon\[u2019]
 .br
 [master 30fdc1e] Added generated password for Amazon/[email protected] to store.
 .br
-1 file changed, 0 insertions(+), 0 deletions(-) 
+1 file changed, 0 insertions(+), 0 deletions(-)
 .br
-create mode 100644 Amazon/[email protected] 
+create mode 100644 Amazon/[email protected]
 .br
-The generated password to Amazon/[email protected] is: 
+The generated password to Amazon/[email protected] is:
 .br
-<5m,_BrZY`antNDxKN<0A 
+<5m,_BrZY`antNDxKN<0A
 
 .B zx2c4@laptop ~ $ pass git push -u --all
 .br
-Counting objects: 4, done. 
+Counting objects: 4, done.
 .br
-Delta compression using up to 2 threads. 
+Delta compression using up to 2 threads.
 .br
-Compressing objects: 100% (3/3), done. 
+Compressing objects: 100% (3/3), done.
 .br
-Writing objects: 100% (4/4), 921 bytes, done. 
+Writing objects: 100% (4/4), 921 bytes, done.
 .br
-Total 4 (delta 0), reused 0 (delta 0) 
+Total 4 (delta 0), reused 0 (delta 0)
 .br
-To kexec.com:pass-store 
+To kexec.com:pass-store
 .br
-* [new branch]      master -> master 
+* [new branch]      master -> master
 .br
-Branch master set up to track remote branch master from origin. 
+Branch master set up to track remote branch master from origin.
 
-.B zx2c4@laptop ~ $ pass insert Amazon/[email protected] 
+.B zx2c4@laptop ~ $ pass insert Amazon/[email protected]
 .br
-Enter password for Amazon/[email protected]: som3r3a11yb1gp4ssw0rd!!88** 
+Enter password for Amazon/[email protected]: som3r3a11yb1gp4ssw0rd!!88**
 .br
-[master b9b6746] Added given password for Amazon/[email protected] to store. 
+[master b9b6746] Added given password for Amazon/[email protected] to store.
 .br
-1 file changed, 0 insertions(+), 0 deletions(-) 
+1 file changed, 0 insertions(+), 0 deletions(-)
 .br
-create mode 100644 Amazon/[email protected] 
+create mode 100644 Amazon/[email protected]
 
-.B zx2c4@laptop ~ $ pass rm Amazon/[email protected] 
+.B zx2c4@laptop ~ $ pass rm Amazon/[email protected]
 .br
-rm: remove regular file \[u2018]/home/zx2c4/.password-store/Amazon/[email protected]\[u2019]? y 
+rm: remove regular file \[u2018]/home/zx2c4/.password-store/Amazon/[email protected]\[u2019]? y
 .br
-removed \[u2018]/home/zx2c4/.password-store/Amazon/[email protected]\[u2019] 
+removed \[u2018]/home/zx2c4/.password-store/Amazon/[email protected]\[u2019]
 .br
-rm 'Amazon/[email protected]' 
+rm 'Amazon/[email protected]'
 .br
-[master 288b379] Removed Amazon/[email protected] from store. 
+[master 288b379] Removed Amazon/[email protected] from store.
 .br
-1 file changed, 0 insertions(+), 0 deletions(-) 
+1 file changed, 0 insertions(+), 0 deletions(-)
 .br
-delete mode 100644 Amazon/[email protected] 
+delete mode 100644 Amazon/[email protected]
 
 .B zx2c4@laptop ~ $ pass git push
 .br
-Counting objects: 9, done. 
+Counting objects: 9, done.
 .br
-Delta compression using up to 2 threads. 
+Delta compression using up to 2 threads.
 .br
-Compressing objects: 100% (5/5), done. 
+Compressing objects: 100% (5/5), done.
 .br
-Writing objects: 100% (7/7), 1.25 KiB, done. 
+Writing objects: 100% (7/7), 1.25 KiB, done.
 .br
-Total 7 (delta 0), reused 0 (delta 0) 
+Total 7 (delta 0), reused 0 (delta 0)
 .br
 To kexec.com:pass-store
 
@@ -393,7 +393,7 @@ Overrides the default password storage directory.
 .I PASSWORD_STORE_KEY
 Overrides the default gpg key identification set by \fBinit\fP. Keys must not
 contain spaces and thus use of the hexidecimal key signature is recommended.
-Multiple keys may be specified separated by spaces. 
+Multiple keys may be specified separated by spaces.
 .TP
 .I PASSWORD_STORE_GIT
 Overrides the default root of the git repository, which is helpful if
diff --git a/src/password-store.sh b/src/password-store.sh
index c85cc33..24d3126 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -350,16 +350,12 @@ cmd_find() {
 cmd_grep() {
 	[[ $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND search-string"
 	agent_check
-	local search="$1" passfile grepresults
+	local search="$1" passfile label
 	while read -r -d "" passfile; do
-		grepresults="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | grep --color=always "$search")"
-		[ $? -ne 0 ] && continue
-		passfile="${passfile%.gpg}"
-		passfile="${passfile#$PREFIX/}"
-		local passfile_dir="${passfile%/*}"
-		passfile="${passfile##*/}"
-		printf "\e[94m%s/\e[1m%s\e[0m:\n" "$passfile_dir" "$passfile"
-		echo "$grepresults"
+		label="${passfile%.gpg}"
+		label="${label#"$PREFIX"}"
+		label="${label#/}"
+		$GPG -d "${GPG_OPTS[@]}" "$passfile" | grep --color=auto --label="$label" -H "$search"
 	done < <(find -L "$PREFIX" -iname '*.gpg' -print0)
 }
 
_______________________________________________
Password-Store mailing list
[email protected]
http://lists.zx2c4.com/mailman/listinfo/password-store

Reply via email to