[pass] [PATCH] Consolidate command paths to bash variables

milki milki at rescomp.berkeley.edu
Mon Sep 17 10:00:40 CEST 2012


gpg, getopt, base64. xclip may be different for non-linux
machines
---
 src/password-store.sh | 55 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 0a12d67..533d426 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -7,10 +7,15 @@ umask 077
 
 PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
 ID="$PREFIX/.gpg-id"
-GIT="$PREFIX/.git"
+GITDIR="$PREFIX/.git"
+
+GPG=gpg
 GPG_OPTS="--quiet --yes --batch"
+GETOPT=getopt
+XCLIP="xclip -o -selection clipboard"
+BASE64=base64
 
-export GIT_DIR="$GIT"
+export GIT_DIR="$GITDIR"
 export GIT_WORK_TREE="$PREFIX"
 
 version() {
@@ -72,12 +77,12 @@ clip() {
 	# in shell. There must be a better way to deal with this, but because I'm a dolt,
 	# we're going with this for now.
 
-	before="$(xclip -o -selection clipboard | base64)"
-	echo -n "$1" | xclip -selection clipboard
+	before="$($XCLIP | $BASE64)"
+	echo -n "$1" | $XCLIP
 	(
 		sleep 45
-		now="$(xclip -o -selection clipboard | base64)"
-		if [[ $now != $(echo -n "$1" | base64) ]]; then
+		now="$($XCLIP | $BASE64)"
+		if [[ $now != $(echo -n "$1" | $BASE64) ]]; then
 			before="$now"
 		fi
 		# It might be nice to programatically check to see if klipper exists,
@@ -85,7 +90,7 @@ clip() {
 		# this works fine. Clipboard managers frequently write their history
 		# out in plaintext, so we axe it here.
 		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory >/dev/null 2>&1
-		echo "$before" | base64 -d | xclip -selection clipboard
+		echo "$before" | $BASE64 -d | $XCLIP
 	) & disown
 	echo "Copied $2 to clipboard. Will clear in 45 seconds."
 }
@@ -134,7 +139,7 @@ case "$command" in
 	show|ls|list)
 		clip=0
 
-		opts="$(getopt -o c -l clip -n $program -- "$@")"
+		opts="$($GETOPT -o c -l clip -n $program -- "$@")"
 		err=$?
 		eval set -- "$opts"
 		while true; do case $1 in
@@ -162,9 +167,9 @@ case "$command" in
 				exit 1
 			fi
 			if [ $clip -eq 0 ]; then
-				exec gpg -d $GPG_OPTS "$passfile"
+				exec $GPG -d $GPG_OPTS "$passfile"
 			else
-				clip "$(gpg -d $GPG_OPTS "$passfile" | head -n 1)" "$path"
+				clip "$($GPG -d $GPG_OPTS "$passfile" | head -n 1)" "$path"
 			fi
 		fi
 		;;
@@ -173,7 +178,7 @@ case "$command" in
 		noecho=0
 		force=0
 
-		opts="$(getopt -o mnf -l multiline,no-echo,force -n $program -- "$@")"
+		opts="$($GETOPT -o mnf -l multiline,no-echo,force -n $program -- "$@")"
 		err=$?
 		eval set -- "$opts"
 		while true; do case $1 in
@@ -201,7 +206,7 @@ case "$command" in
 		if [[ $multiline -eq 1 ]]; then
 			echo "Enter contents of $path and press Ctrl+D when finished:"
 			echo
-			cat | gpg -e -r "$ID" -o "$passfile" $GPG_OPTS
+			cat | $GPG -e -r "$ID" -o "$passfile" $GPG_OPTS
 		elif [[ $noecho -eq 1 ]]; then
 			while true; do
 				read -p "Enter password for $path: " -s password
@@ -209,7 +214,7 @@ case "$command" in
 				read -p "Retype password for $path: " -s password_again
 				echo
 				if [[ $password == $password_again ]]; then
-					gpg -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$password"
+					$GPG -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$password"
 					break
 				else
 					echo "Error: the entered passwords do not match."
@@ -217,9 +222,9 @@ case "$command" in
 			done
 		else
 			read -p "Enter password for $path: " -e password
-			gpg -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$password"
+			$GPG -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$password"
 		fi
-		if [[ -d $GIT ]]; then
+		if [[ -d $GITDIR ]]; then
 			git add "$passfile"
 			git commit -m "Added given password for $path to store."
 		fi
@@ -252,16 +257,16 @@ case "$command" in
 
 		action="Added"
 		if [[ -f $passfile ]]; then
-			gpg -d -o "$tmp_file" $GPG_OPTS "$passfile" || exit 1
+			$GPG -d -o "$tmp_file" $GPG_OPTS "$passfile" || exit 1
 			action="Edited"
 		fi
 		${EDITOR:-vi} "$tmp_file"
-		while ! gpg -e -r "$ID" -o "$passfile" $GPG_OPTS "$tmp_file"; do
+		while ! $GPG -e -r "$ID" -o "$passfile" $GPG_OPTS "$tmp_file"; do
 			echo "GPG encryption failed. Retrying."
 			sleep 1
 		done
 
-		if [[ -d $GIT ]]; then
+		if [[ -d $GITDIR ]]; then
 			git add "$passfile"
 			git commit -m "$action password for $path using ${EDITOR:-vi}."
 		fi
@@ -270,7 +275,7 @@ case "$command" in
 		clip=0
 		symbols="-y"
 
-		opts="$(getopt -o nc -l no-symbols,clip -n $program -- "$@")"
+		opts="$($GETOPT -o nc -l no-symbols,clip -n $program -- "$@")"
 		err=$?
 		eval set -- "$opts"
 		while true; do case $1 in
@@ -292,8 +297,8 @@ case "$command" in
 		mkdir -p -v "$PREFIX/$(dirname "$path")"
 		pass="$(pwgen -s $symbols $length 1)"
 		passfile="$PREFIX/$path.gpg"
-		gpg -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$pass"
-		if [[ -d $GIT ]]; then
+		$GPG -e -r "$ID" -o "$passfile" $GPG_OPTS <<<"$pass"
+		if [[ -d $GITDIR ]]; then
 			git add "$passfile"
 			git commit -m "Added generated password for $path to store."
 		fi
@@ -331,13 +336,13 @@ case "$command" in
 			fi
 		fi
 		rm $recursive $force -v "$passfile"
-		if [[ -d $GIT && ! -e $passfile ]]; then
-			git rm -r "$passfile"
+		if [[ -d $GITDIR ]] && ! [[ -f $passfile ]]; then
+			git rm -f "$passfile"
 			git commit -m "Removed $path from store."
 		fi
 		;;
 	push|pull)
-		if [[ -d $GIT ]]; then
+		if [[ -d $GITDIR ]]; then
 			exec git $command "$@"
 		else
 			echo "Error: the password store is not a git repository."
@@ -345,7 +350,7 @@ case "$command" in
 		fi
 		;;
 	git)
-		if [[ $1 == "init" || -d $GIT ]]; then
+		if [[ $1 == "init" || -d $GITDIR ]]; then
 			exec git "$@"
 		else
 			echo "Error: the password store is not a git repository."
-- 
1.7.11.5




More information about the Password-Store mailing list