Hi,

Several of the browser integrations expect user names and similar information
in multiline entries to be formatted as "key: value", e.g. "login: myusername",
so this is the format I use for all my entries.

This patch allows easy access to those fields from the cli, by letting --clip
(and --qrcode) also accept such a key instead of a line number.
E.g. "pass show foo --clip=login".

Alexander Gehrke
>From d2a368b3741d60d044b197d5df74a4588af38a63 Mon Sep 17 00:00:00 2001
From: Alexander Gehrke <[email protected]>
Date: Tue, 5 Jun 2018 13:41:00 +0200
Subject: [PATCH] show: allow specifying keys instead of line numbers for --clip

Several of the browser integrations expect user names and similar information
in multiline entries to be formatted as "key: value", e.g. "login: myusername".
This patch allows to copy a value with such a key by specifying it instead of a
line number, e.g. "show --clip=login".

---
 man/pass.1            | 7 ++++---
 src/password-store.sh | 7 ++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index e842178..74518fa 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -93,15 +93,16 @@ 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[=\fIline-number\fP], \fI-c\fP[\fIline-number\fP] ] [ \fI--qrcode\fP[=\fIline-number\fP], \fI-q\fP[\fIline-number\fP] ] \fIpass-name\fP
+\fBshow\fP [ \fI--clip\fP[=\fIline-number-or-key\fP], \fI-c\fP[\fIline-number-or-key\fP] ] [ \fI--qrcode\fP[=\fIline-number-or-key\fP], \fI-q\fP[\fIline-number-or-key\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 (or otherwise specified)
 line to the clipboard using
 .BR xclip (1)
-and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP) seconds. If \fI--qrcode\fP
+and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP) seconds.  If \fI--qrcode\fP
 or \fI-q\fP is specified, do not print the password but instead display a QR code using
 .BR qrencode (1)
-either to the terminal or graphically if supported.
+either to the terminal or graphically if supported. If the line specification is not a number, the first line
+starting with the given string followed by a colon is selected and everything after the colon is copied / displayed.
 .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/password-store.sh b/src/password-store.sh
index 19b3124..5f40bb2 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -364,7 +364,12 @@ cmd_show() {
 		if [[ $clip -eq 0 && $qrcode -eq 0 ]]; then
 			$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
 		else
-			[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
+			if [[ $selected_line =~ ^[0-9]+$ ]]; then
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)"
+			else
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" \
+					| awk -F': ' "/^${selected_line}/{for (i=2; i<NF; i++) printf \$i \" \"; print \$NF; exit}")"
+			fi
 			local pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)"
 			[[ -n $pass ]] || die "There is no password to put on the clipboard at line ${selected_line}."
 			if [[ $clip -eq 1 ]]; then
-- 
2.17.0

_______________________________________________
Password-Store mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/password-store

Reply via email to