[RFC PATCH] Add option to print the first line of an entry

Johannes Altmanninger aclopte at gmail.com
Sat Dec 25 09:19:59 UTC 2021


On Mon, Dec 20, 2021 at 01:40:26PM +0100, Rene Kita wrote:
> This commit enables the use of '-1' to print only the first line of an
> entry. A typical use-case would be: 'password=$(pass -1 example.org)' or
> 'pass -1 example.org | tmux loadb -'. Before this change one had to use
> 'sed 1q' or similar when using multi-line entries.

I like this feature, thanks. It adds convenience, and is consistent with
--clip=<line-number> and --qrcode=<line-number>

Of course -1 is probably way less common than  --clip, so maybe it's not
worth it, I'm not sure.

(Regarding the other thread, I also don't think this should be pushed to
extensions, since it's a pretty common/core feature, and making the user
type the extension name kind of defeats the point.)

The synopsis on error (like "pass -h") also needs an update:

diff --git a/src/password-store.sh b/src/password-store.sh
index 77f7ad5..bb50431 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -378,7 +378,7 @@ cmd_show() {
 		--) 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 [-1] [--clip[=line-number],-c[line-number]] [--qrcode[=line-number],-q[line-number]] [pass-name]"
 
 	local pass
 	local path="$1"

> 
> Signed-off-by: Rene Kita <mail at rkta.de>
> ---
>  man/pass.1            | 9 +++++----
>  src/password-store.sh | 7 +++++--
>  2 files changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/man/pass.1 b/man/pass.1
> index a555dcb..cd0ebf0 100644
> --- a/man/pass.1
> +++ b/man/pass.1
> @@ -94,10 +94,11 @@ 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
> -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
> +\fBshow\fP [ \fI-1\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
> +Decrypt and print a password named \fIpass-name\fP. If \fI-1\fP is specified,
> +print only the first line. 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)
>  or
>  .BR wl-clipboard(1)
> diff --git a/src/password-store.sh b/src/password-store.sh
> index aef8d72..f496131 100755
> --- a/src/password-store.sh
> +++ b/src/password-store.sh
> @@ -367,12 +367,13 @@ cmd_init() {
>  
>  cmd_show() {
>  	local opts selected_line clip=0 qrcode=0
> -	opts="$($GETOPT -o q::c:: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
> +	opts="$($GETOPT -o :1q::c::: -l qrcode::,clip:: -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 ;;
> +		-1) selected_line=1; shift ;;
>  		--) shift; break ;;
>  	esac done
>  
> @@ -383,7 +384,7 @@ cmd_show() {
>  	local passfile="$PREFIX/$path.gpg"
>  	check_sneaky_paths "$path"
>  	if [[ -f $passfile ]]; then
> -		if [[ $clip -eq 0 && $qrcode -eq 0 ]]; then
> +		if [[ $clip -eq 0 && $qrcode -eq 0 && -z $selected_line ]]; then
>  			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
>  			echo "$pass" | $BASE64 -d
>  		else
> @@ -394,6 +395,8 @@ cmd_show() {
>  				clip "$pass" "$path"
>  			elif [[ $qrcode -eq 1 ]]; then
>  				qrcode "$pass" "$path"
> +			else
> +				echo "$pass"
>  			fi
>  		fi
>  	elif [[ -d $PREFIX/$path ]]; then
> -- 
> 2.30.2
> 


More information about the Password-Store mailing list