[pass] Use find instead of tree to search for cmd_find

Hugo Osvaldo Barrera hugo at barrera.io
Fri Oct 3 02:26:03 CEST 2014


On 2014-10-02 20:56, Serpent7776 wrote:
> On Thu, 25 Sep 2014 21:04:51 -0300
> Hugo Osvaldo Barrera <hugo at barrera.io> wrote:
> 
> > On 2014-09-25 22:58, Serpent7776 wrote:
> > > Hello,
> > > 
> > > Some time ago I've sent a patch that add switch -1 (number one, not letter
> > > ell) to command ls that lists passwords one per line using find. I attach
> > > modified version that also add switch -1 to search command. Unfortunately
> > > it only accept one term when searching, so
> > > 
> > > pass search -1 foo
> > > 
> > > works, but
> > > 
> > > pass search -1 foo bar
> > > 
> > > will search only for foo
> > > This could be fixed by using 
> > > 
> > > find -iregex
> > > 
> > > and I made it to work on FreeBSD with find -E, but GNU find does not
> > > understand -E option
> > > 
> > 
> > `find ... | grep -e` would work similar and is really portable POSIX. It's
> > slightly less efficient, but: How many passwords can one have?
> 
> I somehow didn't think about using grep here.
> I attach a diff with code modified to use grep -e.
> 
> -- 
> //Serpent7776

> diff --git a/src/password-store.sh b/src/password-store.sh
> index c85cc33..daea65f 100755
> --- a/src/password-store.sh
> +++ b/src/password-store.sh
> @@ -228,9 +228,9 @@ cmd_usage() {
>  	    $PROGRAM init [--path=subfolder,-p subfolder] gpg-id...
>  	        Initialize new password storage and use gpg-id for encryption.
>  	        Selectively reencrypt existing passwords using new gpg-id.
> -	    $PROGRAM [ls] [subfolder]
> +	    $PROGRAM [ls] [-1] [subfolder]
>  	        List passwords.
> -	    $PROGRAM find pass-names...
> +	    $PROGRAM find [-1] pass-names...
>  	    	List passwords that match pass-names.
>  	    $PROGRAM [show] [--clip,-c] pass-name
>  	        Show existing password and optionally put it on the clipboard.
> @@ -304,16 +304,17 @@ cmd_init() {
>  }
>  
>  cmd_show() {
> -	local opts clip=0
> -	opts="$($GETOPT -o c -l clip -n "$PROGRAM" -- "$@")"
> +	local opts clip=0 one_per_row=0
> +	opts="$($GETOPT -o c -l clip -o 1 -n "$PROGRAM" -- "$@")"
>  	local err=$?
>  	eval set -- "$opts"
>  	while true; do case $1 in
>  		-c|--clip) clip=1; shift ;;
> +		-1) one_per_row=1; shift ;;
>  		--) shift; break ;;
>  	esac done
>  
> -	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] [pass-name]"
> +	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c,-1] [pass-name]"
>  
>  	local path="$1"
>  	local passfile="$PREFIX/$path.gpg"
> @@ -327,12 +328,19 @@ cmd_show() {
>  			clip "$pass" "$path"
>  		fi
>  	elif [[ -d $PREFIX/$path ]]; then
> -		if [[ -z $path ]]; then
> -			echo "Password Store"
> +		if [[ $one_per_row -eq 0 ]]; then
> +			if [[ -z $path ]]; then
> +				echo "Password Store"
> +			else
> +				echo "${path%\/}"
> +			fi
> +			tree -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed 's/\.gpg$//'
>  		else
> -			echo "${path%\/}"
> +			if [[ -z "$path" ]]; then
> +				path="."
> +			fi
> +			( cd $PREFIX && find $path/ -type f -iname \*.gpg | sed -e 's/\.gpg$//' -e 's/\.\///' )
>  		fi
> -		tree -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed 's/\.gpg$//'
>  	elif [[ -z $path ]]; then
>  		die "Error: password store is empty. Try \"pass init\"."
>  	else
> @@ -341,10 +349,24 @@ cmd_show() {
>  }
>  
>  cmd_find() {
> -	[[ -z "$@" ]] && die "Usage: $PROGRAM $COMMAND pass-names..."
> -	IFS="," eval 'echo "Search Terms: $*"'
> -	local terms="*$(printf '%s*|*' "$@")"
> -	tree -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" | tail -n +2 | sed 's/\.gpg$//'
> +	local opts one_per_row=0
> +	opts="$($GETOPT -o 1 -n "$PROGRAM" -- "$@")"
> +	local err=$?
> +	eval set -- "$opts"
> +	while true; do case $1 in
> +		-1) one_per_row=1; shift ;;
> +		--) shift; break ;;
> +	esac done
> +
> +	[[ -z "$@" ]] && die "Usage: $PROGRAM $COMMAND [-1] pass-names..."
> +	if [[ $one_per_row -eq 0 ]]; then
> +		IFS="," eval 'echo "Search Terms: $*"'
> +		local terms="*$(printf '%s*|*' "$@")"
> +		tree -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$PREFIX" | tail -n +2 | sed 's/\.gpg$//'
> +	else
> +		local terms="$(printf '%s\\|' "$@")"
> +		( cd $PREFIX && find ./ -type f | grep -e ".*\\(${terms%\\|}\\).*" | sed -e 's/\.gpg$//' -e 's/\.\///' )
> +	fi
>  }
>  
>  cmd_grep() {

I'm not much of a shell programmer, but this looks good to me.

Thanks, and cheers!

-- 
Hugo Osvaldo Barrera
A: Because we read from top to bottom, left to right.
Q: Why should I start my reply below the quoted text?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <http://lists.zx2c4.com/pipermail/password-store/attachments/20141002/586244e0/attachment.asc>


More information about the Password-Store mailing list