password-store

Simple password manager using gpg and ordinary unix directories
git clone https://git.zx2c4.com/password-store
Log | Files | Refs | README | LICENSE

pass.bash-completion (4215B)


      1 # completion file for bash
      2 
      3 # Copyright (C) 2012 - 2014 Jason A. Donenfeld <Jason@zx2c4.com> and
      4 # Brian Mattern <rephorm@rephorm.com>. All Rights Reserved.
      5 # This file is licensed under the GPLv2+. Please see COPYING for more information.
      6 
      7 _pass_complete_entries () {
      8 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
      9 	prefix="${prefix%/}/"
     10 	local suffix=".gpg"
     11 	local autoexpand=${1:-0}
     12 
     13 	local IFS=$'\n'
     14 	local items=($(compgen -f $prefix$cur))
     15 
     16 	# Remember the value of the first item, to see if it is a directory. If
     17 	# it is a directory, then don't add a space to the completion
     18 	local firstitem=""
     19 	# Use counter, can't use ${#items[@]} as we skip hidden directories
     20 	local i=0 item
     21 
     22 	for item in ${items[@]}; do
     23 		[[ $item =~ /\.[^/]*$ ]] && continue
     24 
     25 		# if there is a unique match, and it is a directory with one entry
     26 		# autocomplete the subentry as well (recursively)
     27 		if [[ ${#items[@]} -eq 1 && $autoexpand -eq 1 ]]; then
     28 			while [[ -d $item ]]; do
     29 				local subitems=($(compgen -f "$item/"))
     30 				local filtereditems=( ) item2
     31 				for item2 in "${subitems[@]}"; do
     32 					[[ $item2 =~ /\.[^/]*$ ]] && continue
     33 					filtereditems+=( "$item2" )
     34 				done
     35 				if [[ ${#filtereditems[@]} -eq 1 ]]; then
     36 					item="${filtereditems[0]}"
     37 				else
     38 					break
     39 				fi
     40 			done
     41 		fi
     42 
     43 		# append / to directories
     44 		[[ -d $item ]] && item="$item/"
     45 
     46 		item="${item%$suffix}"
     47 		COMPREPLY+=("${item#$prefix}")
     48 		if [[ $i -eq 0 ]]; then
     49 			firstitem=$item
     50 		fi
     51 		let i+=1
     52 	done
     53 
     54 	# The only time we want to add a space to the end is if there is only
     55 	# one match, and it is not a directory
     56 	if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then
     57 		compopt -o nospace
     58 	fi
     59 }
     60 
     61 _pass_complete_folders () {
     62 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
     63 	prefix="${prefix%/}/"
     64 
     65 	local IFS=$'\n'
     66 	local items=($(compgen -d $prefix$cur))
     67 	for item in ${items[@]}; do
     68 		[[ $item == $prefix.* ]] && continue
     69 		COMPREPLY+=("${item#$prefix}/")
     70 	done
     71 }
     72 
     73 _pass_complete_keys () {
     74 	local GPG="gpg"
     75 	command -v gpg2 &>/dev/null && GPG="gpg2"
     76 
     77 	local IFS=$'\n'
     78 	# Extract names and email addresses from gpg --list-keys
     79 	local keys="$($GPG --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d')"
     80 	COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
     81 }
     82 
     83 _pass()
     84 {
     85 	COMPREPLY=()
     86 	local cur="${COMP_WORDS[COMP_CWORD]}"
     87 	local commands="init ls find grep show insert generate edit rm mv cp git help version ${PASSWORD_STORE_EXTENSION_COMMANDS[*]}"
     88 	if [[ $COMP_CWORD -gt 1 ]]; then
     89 		local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
     90 		case "${COMP_WORDS[1]}" in
     91 			init)
     92 				if [[ $lastarg == "-p" || $lastarg == "--path" ]]; then
     93 					_pass_complete_folders
     94 					compopt -o nospace
     95 				else
     96 					COMPREPLY+=($(compgen -W "-p --path" -- ${cur}))
     97 					_pass_complete_keys
     98 				fi
     99 				;;
    100 			ls|list|edit)
    101 				_pass_complete_entries
    102 				;;
    103 			show|-*)
    104 				COMPREPLY+=($(compgen -W "-c --clip" -- ${cur}))
    105 				_pass_complete_entries 1
    106 				;;
    107 			insert)
    108 				COMPREPLY+=($(compgen -W "-e --echo -m --multiline -f --force" -- ${cur}))
    109 				_pass_complete_entries
    110 				;;
    111 			generate)
    112 				COMPREPLY+=($(compgen -W "-n --no-symbols -c --clip -f --force -i --in-place" -- ${cur}))
    113 				_pass_complete_entries
    114 				;;
    115 			cp|copy|mv|rename)
    116 				COMPREPLY+=($(compgen -W "-f --force" -- ${cur}))
    117 				_pass_complete_entries
    118 				;;
    119 			rm|remove|delete)
    120 				COMPREPLY+=($(compgen -W "-r --recursive -f --force" -- ${cur}))
    121 				_pass_complete_entries
    122 				;;
    123 			git)
    124 				COMPREPLY+=($(compgen -W "init push pull config log reflog rebase" -- ${cur}))
    125 				;;
    126 		esac
    127 
    128 		# To add completion for an extension command define a function like this:
    129 		# __password_store_extension_complete_<COMMAND>() {
    130 		#     COMPREPLY+=($(compgen -W "-o --option" -- ${cur}))
    131 		#     _pass_complete_entries 1
    132 		# }
    133 		#
    134 		# and add the command to the $PASSWORD_STORE_EXTENSION_COMMANDS array
    135 		if [[ " ${PASSWORD_STORE_EXTENSION_COMMANDS[*]} " == *" ${COMP_WORDS[1]} "* ]] && type "__password_store_extension_complete_${COMP_WORDS[1]}" &> /dev/null; then
    136 			"__password_store_extension_complete_${COMP_WORDS[1]}"
    137 		fi
    138 	else
    139 		COMPREPLY+=($(compgen -W "${commands}" -- ${cur}))
    140 		_pass_complete_entries 1
    141 	fi
    142 }
    143 
    144 complete -o filenames -F _pass pass