[pass] [PATCH] Add --fistline (-l) option to show only the first line

Daniele Pizzolli daniele.pizzolli at create-net.org
Thu Dec 24 16:39:36 CET 2015


Date: Thu, 24 Dec 2015 16:25:17 +0100
User-agent: mu4e 0.9.15; emacs 24.4.1
Address the asymmetry between the output that is by default multiples
line and the input that is by default single line.

Add the option -l or --fistline to to show only the first line.

The asymmetry is still here, but at least now is possible to have a
single line output.

The test suite was extended to check the use cases.

The completion were updated, but not tested.
---
 man/pass.1                          |  5 +++--
 src/completion/pass.bash-completion |  2 +-
 src/completion/pass.fish-completion |  1 +
 src/completion/pass.zsh-completion  |  2 ++
 src/password-store.sh               | 17 +++++++++++++----
 tests/t0020-show-tests.sh           | 18 ++++++++++++++++++
 6 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index e1fe605..9cd1dbb 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -86,12 +86,13 @@ List names of passwords inside the tree that match \fIpass-names\fP by using the
 .BR tree (1)
 program. This command is alternatively named \fBsearch\fP.
 .TP
-\fBshow\fP [ \fI--clip\fP, \fI-c\fP ] \fIpass-name\fP
-Decrypt and print a password named \fIpass-name\fP. If \fI--clip\fP or \fI-c\fP
+\fBshow\fP [ \fI--clip\fP, \fI-c\fP ] [ \fI--firstline\fP, \fI-l\fP ] \fIpass-name\fP
+Decrypt and print a password named \fIpass-name\fP.  If \fI--clip\fP or \fI-c\fP
 is specified, do not print the password but instead copy the first line to the
 clipboard using
 .BR xclip (1)
 and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP) seconds.
+If \fI--firstline\fP or \fI-l\fP is specified, print only the first line.
 .TP
 \fBinsert\fP [ \fI--echo\fP, \fI-e\fP | \fI--multiline\fP, \fI-m\fP ] [ \fI--force\fP, \fI-f\fP ] \fIpass-name\fP
 Insert a new password into the password store called \fIpass-name\fP. This will
diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index efd4b70..fd281cb 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -80,7 +80,7 @@ _pass()
 				_pass_complete_entries
 				;;
 			show|-*)
-				COMPREPLY+=($(compgen -W "-c --clip" -- ${cur}))
+				COMPREPLY+=($(compgen -W "-c --clip -l --firstline" -- ${cur}))
 				_pass_complete_entries 1
 				;;
 			insert)
diff --git a/src/completion/pass.fish-completion b/src/completion/pass.fish-completion
index c32a42c..85fd09a 100644
--- a/src/completion/pass.fish-completion
+++ b/src/completion/pass.fish-completion
@@ -101,6 +101,7 @@ complete -c $PROG -f -A -n '__fish_pass_uses_command show' -s c -l clip -d 'Put
 complete -c $PROG -f -A -n '__fish_pass_uses_command show' -a "(__fish_pass_print_entries)"
 # When no command is given, `show` is defaulted.
 complete -c $PROG -f -A -n '__fish_pass_needs_command' -s c -l clip -d 'Put password in clipboard'
+complete -c $PROG -f -A -n '__fish_pass_needs_command' -s l -l firstline -d 'Show only first line'
 complete -c $PROG -f -A -n '__fish_pass_needs_command' -a "(__fish_pass_print_entries)"
 complete -c $PROG -f -A -n '__fish_pass_uses_command -c' -a "(__fish_pass_print_entries)"
 complete -c $PROG -f -A -n '__fish_pass_uses_command --clip' -a "(__fish_pass_print_entries)"
diff --git a/src/completion/pass.zsh-completion b/src/completion/pass.zsh-completion
index 9bb3f97..af57bee 100644
--- a/src/completion/pass.zsh-completion
+++ b/src/completion/pass.zsh-completion
@@ -43,6 +43,8 @@ _pass () {
 					"--no-symbols[don't include symbols in password]" \
 					"-c[copy password to the clipboard]" \
 					"--clip[copy password to the clipboard]" \
+					"-l[show only first line]" \
+					"--firstline[show only first line]" \
 					"-f[force overwrite]" \
 					"--force[force overwrite]" \
 					"-i[replace first line]" \
diff --git a/src/password-store.sh b/src/password-store.sh
index d535a74..e49dd92 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -223,9 +223,10 @@ cmd_usage() {
 	        List passwords.
 	    $PROGRAM find pass-names...
 	    	List passwords that match pass-names.
-	    $PROGRAM [show] [--clip,-c] pass-name
+	    $PROGRAM [show] [--clip,-c] [--firstline,-l] 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.
+	        Optionally show only the first line
 	    $PROGRAM grep search-string
 	        Search for password files containing search-string when decrypted.
 	    $PROGRAM insert [--echo,-e | --multiline,-m] [--force,-f] pass-name
@@ -295,22 +296,30 @@ cmd_init() {
 
 cmd_show() {
 	local opts clip=0
-	opts="$($GETOPT -o c -l clip -n "$PROGRAM" -- "$@")"
+	opts="$($GETOPT -o cl -l clip,firstline -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
 		-c|--clip) clip=1; shift ;;
+		-l|--firstline) firstline=1; shift ;;
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [pass-name]"
+	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [--firstline,-1] [pass-name]"
+
+	[[ $clip -eq 1 ]] && [[ $firstline -eq 1 ]] \
+		&& die "$PROGRAM $COMMAND: Error: Options --clip and --firstline are mutually exclusive"
 
 	local path="$1"
 	local passfile="$PREFIX/$path.gpg"
 	check_sneaky_paths "$path"
 	if [[ -f $passfile ]]; then
 		if [[ $clip -eq 0 ]]; then
-			$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
+			if [[ $firstline -eq 1 ]]; then
+				$GPG -d "${GPG_OPTS[@]}" "$passfile" | head -n 1 || exit $?
+			else
+				$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
+			fi
 		else
 			local pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | head -n 1)"
 			[[ -n $pass ]] || exit 1
diff --git a/tests/t0020-show-tests.sh b/tests/t0020-show-tests.sh
index a4b782f..ed54d62 100755
--- a/tests/t0020-show-tests.sh
+++ b/tests/t0020-show-tests.sh
@@ -19,4 +19,22 @@ test_expect_success 'Test "show" of nonexistant password' '
 	test_must_fail "$PASS" show cred2
 '
 
+test_expect_success 'Test "show default multiline"' '
+	"$PASS" init $KEY1 &&
+	printf firstline\\nsecondline\\n | "$PASS" insert --multiline multiline &&
+	"$PASS" show multiline &&
+	lines=$("$PASS" show multiline | wc -l) && [ "$lines" -eq 2 ]
+'
+
+test_expect_success 'Test "show oneline"' '
+	"$PASS" init $KEY1 &&
+	printf firstline\\nsecondline\\n | "$PASS" insert --multiline multiline2 &&
+        line=$("$PASS" show -l multiline2) && [ "$line" == "firstline" ]
+        line=$("$PASS" show --firstline multiline2) && [ "$line" == "firstline" ]
+'
+
+test_expect_failure 'Test option clip and firstline xor' '
+	"$PASS" show -l -c
+'
+
 test_done
-- 
2.1.4



More information about the Password-Store mailing list