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.

Signed-off-by: Rene Kita <[email protected]>
---
Changes since the RFC:
 - Add option to Usage: (shown on error and with -h)
 - Add option to the completions for bash, fish and zsh

 man/pass.1                          | 9 +++++----
 src/completion/pass.bash-completion | 2 +-
 src/completion/pass.fish-completion | 3 +++
 src/completion/pass.zsh-completion  | 1 +
 src/password-store.sh               | 9 ++++++---
 5 files changed, 16 insertions(+), 8 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/completion/pass.bash-completion 
b/src/completion/pass.bash-completion
index 2d23cbf..266c26b 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -101,7 +101,7 @@ _pass()
                                _pass_complete_entries
                                ;;
                        show|-*)
-                               COMPREPLY+=($(compgen -W "-c --clip" -- ${cur}))
+                               COMPREPLY+=($(compgen -W "-1 -c --clip" -- 
${cur}))
                                _pass_complete_entries 1
                                ;;
                        insert)
diff --git a/src/completion/pass.fish-completion 
b/src/completion/pass.fish-completion
index 0f57dd2..ea804d9 100644
--- a/src/completion/pass.fish-completion
+++ b/src/completion/pass.fish-completion
@@ -96,11 +96,14 @@ complete -c $PROG -f -n '__fish_pass_needs_command' -a edit 
-d 'Command: edit pa
 complete -c $PROG -f -n '__fish_pass_uses_command edit' -a 
"(__fish_pass_print_entries)"
 
 complete -c $PROG -f -n '__fish_pass_needs_command' -a show -d 'Command: show 
existing password'
+complete -c $PROG -f -n '__fish_pass_uses_command show' -s 1 -d 'Show password 
only'
 complete -c $PROG -f -n '__fish_pass_uses_command show' -s c -l clip -d 'Put 
password in clipboard'
 complete -c $PROG -f -n '__fish_pass_uses_command show' -a 
"(__fish_pass_print_entries)"
 # When no command is given, `show` is defaulted.
+complete -c $PROG -f -n '__fish_pass_needs_command' -s 1 -d 'Show password 
only'
 complete -c $PROG -f -n '__fish_pass_needs_command' -s c -l clip -d 'Put 
password in clipboard'
 complete -c $PROG -f -n '__fish_pass_needs_command' -a 
"(__fish_pass_print_entries)"
+complete -c $PROG -f -n '__fish_pass_uses_command -1' -a 
"(__fish_pass_print_entries)"
 complete -c $PROG -f -n '__fish_pass_uses_command -c' -a 
"(__fish_pass_print_entries)"
 complete -c $PROG -f -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 d911e12..768744a 100644
--- a/src/completion/pass.zsh-completion
+++ b/src/completion/pass.zsh-completion
@@ -116,6 +116,7 @@ _pass () {
 
 _pass_cmd_show () {
        _arguments : \
+               "-1[show password only]" \
                "-c[put it on the clipboard]" \
                "--clip[put it on the clipboard]"
        _pass_complete_entries
diff --git a/src/password-store.sh b/src/password-store.sh
index aef8d72..87f6d07 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -367,23 +367,24 @@ 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
 
-       [[ $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"
        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

Reply via email to