[pass] [PATCH] Add --only option to command show

Olivier Mehani shtrom+zx2c4 at ssji.net
Sun May 15 08:17:05 CEST 2016


From: Olivier Mehani <shtrom at ssji.net>

This allows to output only the selected line, useful for piping.

Also introduce the '-1' shorthand, which is the same as --only=1.

Signed-off-by: Olivier Mehani <shtrom at ssji.net>
---
 src/password-store.sh | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 63be840..eace832 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -224,7 +224,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[=line-number],-c[line-number] | -1,--only[=line-number],-o[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 search-string
@@ -295,28 +295,34 @@ cmd_init() {
 }
 
 cmd_show() {
-	local opts clip_location clip=0
-	opts="$($GETOPT -o c:: -l clip:: -n "$PROGRAM" -- "$@")"
+	local opts clip_location clip=0 only=0
+	opts="$($GETOPT -o c::,o::,1 -l clip::,only:: -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
 		-c|--clip) clip=1; clip_location="${2:-1}"; shift 2 ;;
+		-o|--only) only=1; clip_location="${2:-1}"; shift 2 ;;
+		-1) only=1; clip_location="1"; shift 1 ;;
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip[=line-number],-c[line-number]] [pass-name]"
+	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip[=line-number],-c[line-number] | -1,--only[=line-number],-o[line-number]] pass-name"
 
 	local path="$1"
 	local passfile="$PREFIX/$path.gpg"
 	check_sneaky_paths "$path"
 	if [[ -f $passfile ]]; then
-		if [[ $clip -eq 0 ]]; then
+		if [[ $clip -eq 0 && $only -eq 0 ]]; then
 			$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
 		else
-			[[ $clip_location =~ ^[0-9]+$ ]] || die "Clip location '$clip_location' is not a number."
+			[[ $clip_location =~ ^[0-9]+$ ]] || die "Password location '$clip_location' is not a number."
 			local pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${clip_location} | head -n 1)"
-			[[ -n $pass ]] || die "There is no password to put on the clipboard at line ${clip_location}."
-			clip "$pass" "$path"
+			[[ -n $pass ]] || die "There is no password at line ${clip_location}."
+			if [[ $clip -eq 0 ]]; then
+				echo "$pass"
+			else # default to clipping
+				clip "$pass" "$path"
+			fi
 		fi
 	elif [[ -d $PREFIX/$path ]]; then
 		if [[ -z $path ]]; then
-- 
2.8.2



More information about the Password-Store mailing list