<div dir="ltr"><div>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).<br>The feature requested was:<br><br><br>
> As someone who likes to change passwords quite often I really like the generate command.<br>> 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.<br>
> Otherwise when using generate everything else in the passfile would be deleted.<br><br></div>and one of the repiles was:<br><br>> I second that. I was thinking about it, but haven't got around coding<br>> anything yet.<br>
><br>> I would do it as an option to "generate" e.g., "-r/--replace".<br><br>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.<br>
Cheers!<br><br><br>---<br> src/password-store.sh | 21 +++++++++++++++------<br> 1 file changed, 15 insertions(+), 6 deletions(-)<br><br>diff --git a/src/password-store.sh b/src/password-store.sh<br>index ad22833..d35765e 100755<br>
--- a/src/password-store.sh<br>+++ b/src/password-store.sh<br>@@ -230,10 +230,11 @@ cmd_usage() {<br> overwriting existing password unless forced.<br> $PROGRAM edit pass-name<br> Insert a new password or edit an existing password using ${EDITOR:-vi}.<br>
- $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length<br>+ $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f] [--replace,-r] pass-name pass-length<br> Generate a new password of pass-length with optionally no symbols.<br>
Optionally put it on the clipboard and clear board after 45 seconds.<br> Prompt before overwriting existing password unless forced.<br>+ Only replace the first line, leaving the rest of the file untouched.<br>
$PROGRAM rm [--recursive,-r] [--force,-f] pass-name<br> Remove existing password or directory, optionally forcefully.<br> $PROGRAM mv [--force,-f] old-path new-path<br>@@ -430,18 +431,19 @@ cmd_edit() {<br>
}<br> <br> cmd_generate() {<br>- local opts clip=0 force=0 symbols="-y"<br>- opts="$($GETOPT -o ncf -l no-symbols,clip,force -n "$PROGRAM" -- "$@")"<br>+ local opts clip=0 force=0 replace=0 symbols="-y"<br>
+ opts="$($GETOPT -o ncfr -l no-symbols,clip,force,replace -n "$PROGRAM" -- "$@")"<br> local err=$?<br> eval set -- "$opts"<br> while true; do case $1 in<br> -n|--no-symbols) symbols=""; shift ;;<br>
-c|--clip) clip=1; shift ;;<br> -f|--force) force=1; shift ;;<br>+ -r|--replace) replace=1; shift;;<br> --) shift; break ;;<br> esac done<br> <br>- [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length"<br>
+ [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--force,-f] [--replace,-r] pass-name pass-length"<br> local path="$1"<br> local length="$2"<br>
check_sneaky_paths "$path"<br>@@ -450,11 +452,18 @@ cmd_generate() {<br> set_gpg_recipients "$(dirname "$path")"<br> local passfile="$PREFIX/$path.gpg"<br> <br>- [[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"<br>
+ [[ $force -eq 0 && $replace -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"<br> <br> local pass="$(pwgen -s $symbols $length 1)"<br>
[[ -n $pass ]] || exit 1<br>- $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$pass"<br>+ local output="$pass"<br>+<br>+ if [[ $replace -eq 1 ]]; then<br>
+ local file_metadata="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +2)"<br>+ output+="$(echo -e "\n$file_metadata")"<br>+ fi<br>+<br>+ $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$output"<br>
git_add_file "$passfile" "Add generated password for $path to store."<br> <br> if [[ $clip -eq 0 ]]; then<br>-- <br>1.9.2<br><br></div>