[PATCH] feat: Allow to show selected line

Eddie Barraco contact at eddiebarraco.fr
Fri Mar 1 21:19:05 CET 2019


An annoying problem was that pass didn't allow to show only some
specific line (generaly the first). In some program configuration, we
had to trick to get the password prompted:

Example:

```
~/.msmtprc
passwordeval   "pass show db/Email/Contact/contact at eddiebarraco.fr | head -1"
```

The problem can be tricked only if el famoso program allow to use
redirections and pipes. In some cases the trick become really annoying...

Example:

```
~/.config/vdirsyncer/config
password.fetch = ["command", "/home/eddie/bin/passfirst", "db/Internet/Nextcloud"]
```

```
~/bin/passfirst
pass show "$1" | head -1
```

I thinked about this problem from some times and I think I got a point.

The qrcode and clip logic seems to be too coupled to the line selection
logic and this generate two problems :
-> We cannot clip and qrcode all the lines
-> we cannot show specific lines

So I tried to seperate both logics with a dedicated parameter (--selected-line|-s)

-c and -q will both use selected lines (all the lines if unspecified)
-s will allow to select a specific line (the first line if no line number is used)

Here is a proof of concept and I need reviews to make it better.

The changes will breaks old logics ! If you previously used '-c 1' or '-q 1', it
will no longer works. You'll have to '-c -s 1' or '-q -s 1'. But the
price is worth IMHO cause we will be able to do more things.

TY dudes, good night.
---
 src/password-store.sh | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 284eabf..1c6c786 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -281,7 +281,7 @@ cmd_usage() {
 	        List passwords.
 	    $PROGRAM find pass-names...
 	    	List passwords that match pass-names.
-	    $PROGRAM [show] [--clip[=line-number],-c[line-number]] pass-name
+	    $PROGRAM [show] [--clip,-c | --qrcode,-q] [--selected-line[=line-number],-s[line-number]] pass-name
 	        Show existing password and optionally put it on the clipboard.
 	        If put on the clipboard, it will be cleared in $CLIP_TIME seconds.
 	    $PROGRAM grep [GREPOPTIONS] search-string
@@ -363,17 +363,18 @@ 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=0 clip=0 qrcode=0
+	opts="$($GETOPT -o q::c::s:: -l qrcode::,clip::,selected-line:: -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
-		-q|--qrcode) qrcode=1; selected_line="${2:-1}"; shift 2 ;;
-		-c|--clip) clip=1; selected_line="${2:-1}"; shift 2 ;;
+		-q|--qrcode) qrcode=1; shift 2 ;;
+		-c|--clip) clip=1; shift 2 ;;
+		-s|--selected-line) selected_line="${2:-1}"; shift 2 ;;
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--clip[=line-number],-c[line-number]] [--qrcode[=line-number],-q[line-number]] [pass-name]"
+	[[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c | --qrcode,-q] [--selected-line[=line-number],-s[line-number]] [pass-name]"
 
 	local pass
 	local path="$1"
@@ -381,11 +382,20 @@ cmd_show() {
 	check_sneaky_paths "$path"
 	if [[ -f $passfile ]]; then
 		if [[ $clip -eq 0 && $qrcode -eq 0 ]]; then
-			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
+			if [[ $selected_line -eq 0 ]]; then
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
+			else
+				[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1 | $BASE64)" || exit $?
+			fi
 			echo "$pass" | $BASE64 -d
 		else
-			[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
-			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)" || exit $?
+			if [[ $selected_line -eq 0 ]]; then
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile")" || exit $?
+			else
+				[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)" || exit $?
+			fi
 			[[ -n $pass ]] || die "There is no password to put on the clipboard at line ${selected_line}."
 			if [[ $clip -eq 1 ]]; then
 				clip "$pass" "$path"
-- 
2.21.0



More information about the Password-Store mailing list