Get field value for command show and option -c

Adrien Horgnies ah at fita.dev
Wed Feb 28 14:40:24 UTC 2024


Hi,

The website https://www.passwordstore.org/ suggests to organize data like this:

> Yw|ZSNH!}z"6{ym9pI
> URL: *.amazon.com/*
> Username: AmazonianChicken at example.com
> Secret Question 1: What is your childhood best friend's most bizarre superhero fantasy? Oh god, Amazon, it's too awful to say...
> Phone Support PIN #: 84719

However, it's impractical to use if you want to copy any information there,
as you only need the thing on the right side of the colon.

I'd like to suggest the following feature for the command "show" and
option "-c":
- An non-numerical value is treated as a field key to search for
- Instead of copy / qrencode the full line, it processes the value
next to the key

Here's how it would work:

```console
$ pass foo
foo
user: bar
pin: 1234
stuff: spam
$ pass -cuser foo && wl-paste
Copied foo to clipboard. Will clear in 45 seconds.
bar
$ pass -cpin foo && wl-paste
Copied foo to clipboard. Will clear in 45 seconds.
1234
```

Here's a simple implementation.
It's missing documentation and tests.
I'll add them if you deem the patch interesting.
Also, would it need anything else?
I guess making the separator ": " customizable would be of interest as well.

Best regards,

Adrien Horgnies

>From 58f4e1206dfabb61a0983b3514376d2893655f77 Mon Sep 17 00:00:00 2001
From: Adrien Horgnies <ah at fita.dev>
Date: Wed, 28 Feb 2024 15:00:05 +0100
Subject: [PATCH] Get field value for command show and option -c

Treat lines with colon and space ": " as a field with a key and a value.
The left side of the first ": " is considered a field key.
The right side of the first ": " is considered a field value.

Using the option -c with a non-numerical argument doesn't fail anymore.
Instead it treats the argument as a field key.
And it gets you the value without the key.
Or an error if the key doesn't match anything or the value is empty.
---
 src/password-store.sh | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 22e818f..e0f6e3a 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -387,9 +387,13 @@ cmd_show() {
             pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
             echo "$pass" | $BASE64 -d
         else
-            [[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location
'$selected_line' is not a number."
-            pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n
+${selected_line} | head -n 1)" || exit $?
-            [[ -n $pass ]] || die "There is no password to put on the
clipboard at line ${selected_line}."
+            if [[ $selected_line =~ ^[0-9]+$ ]]; then
+                pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail
-n +${selected_line} | head -n 1)" || exit $?
+                [[ -n $pass ]] || die "There is no password to put on
the clipboard at line ${selected_line}."
+            else
+                pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | grep
-Pom1 "${selected_line}: \K.*" /dev/stdin)"
+                [[ -n $pass ]] || die "There is no password to put on
the clipboard associated with field ${selected_line}."
+            fi
             if [[ $clip -eq 1 ]]; then
                 clip "$pass" "$path"
             elif [[ $qrcode -eq 1 ]]; then
-- 
2.34.1


More information about the Password-Store mailing list