[pass] [PATCH] generate only first line (correction)

Facundo Tuesca facutuesca at gmail.com
Mon May 5 23:34:33 CEST 2014


Well it seems I messed up with the formatting of the last email I tried to send, so here it goes again:

Hey everyone, here's a patch for the feature requested last month (I just suscribed to the list, so I can't send this as a reply to the original email).
The feature requested was:


> As someone who likes to change passwords quite often I really like the generate command.
> But as I prefer to have metadata alongside the password it would be really useful if pass would only replace the first line of a passfile if it regenerates an already existing password.
> Otherwise when using generate everything else in the passfile would be deleted.

and one of the repiles was:

> I second that. I was thinking about it, but haven't got around coding
> anything yet.
>
> I would do it as an option to "generate" e.g., "-r/--replace".

So here's a little patch that implements the option "-r" for "generate", allowing to replace the first line of the password file, and leaving the rest untouched.
Cheers!

---
 src/password-store.sh | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index ad22833..ccc073d 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -230,10 +230,11 @@ cmd_usage() {
 	        overwriting existing password unless forced.
 	    $PROGRAM edit pass-name
 	        Insert a new password or edit an existing password using ${EDITOR:-vi}.
-	    $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length
+	    $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f] [--replace,-r] pass-name pass-length
 	        Generate a new password of pass-length with optionally no symbols.
 	        Optionally put it on the clipboard and clear board after 45 seconds.
 	        Prompt before overwriting existing password unless forced.
+	        Only replace the first line, leaving the rest of the file untouched.
 	    $PROGRAM rm [--recursive,-r] [--force,-f] pass-name
 	        Remove existing password or directory, optionally forcefully.
 	    $PROGRAM mv [--force,-f] old-path new-path
@@ -430,18 +431,19 @@ cmd_edit() {
 }
 
 cmd_generate() {
-	local opts clip=0 force=0 symbols="-y"
-	opts="$($GETOPT -o ncf -l no-symbols,clip,force -n "$PROGRAM" -- "$@")"
+	local opts clip=0 force=0 replace=0 symbols="-y"
+	opts="$($GETOPT -o ncfr -l no-symbols,clip,force,replace -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
 		-n|--no-symbols) symbols=""; shift ;;
 		-c|--clip) clip=1; shift ;;
 		-f|--force) force=1; shift ;;
+		-r|--replace) replace=1; shift;;
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length"
+	[[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--force,-f] [--replace,-r]  pass-name pass-length"
 	local path="$1"
 	local length="$2"
 	check_sneaky_paths "$path"
@@ -454,7 +456,14 @@ cmd_generate() {
 
 	local pass="$(pwgen -s $symbols $length 1)"
 	[[ -n $pass ]] || exit 1
-	$GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$pass"
+	local output="$pass"
+
+	if [[ $replace -eq 1 ]]; then
+		local file_metadata="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +2)"
+		output+="$(echo -e "\n$file_metadata")"
+	fi
+
+	$GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$output"
 	git_add_file "$passfile" "Add generated password for $path to store."
 
 	if [[ $clip -eq 0 ]]; then
-- 
1.9.2



More information about the Password-Store mailing list