[PATCH 1/1] bash completion without relying on /etc/bash-completion.d

Lars Flitter password-store at larsflitter.de
Sun Oct 7 22:34:02 CEST 2018


---
 Makefile                            |  6 +++-
 src/completion/pass.bash-completion | 46 +++++++++++++++++++++++------
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index eac2291..23aa5f4 100644
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,11 @@ all:
 
 install-common:
 	@install -v -d "$(DESTDIR)$(MANDIR)/man1" && install -m 0644 -v man/pass.1 "$(DESTDIR)$(MANDIR)/man1/pass.1"
-	@[ "$(WITH_BASHCOMP)" = "yes" ] || exit 0; install -v -d "$(DESTDIR)$(BASHCOMPDIR)" && install -m 0644 -v src/completion/pass.bash-completion "$(DESTDIR)$(BASHCOMPDIR)/pass"
+	@[ "$(WITH_BASHCOMP)" = "yes" ] || exit 0;  \
+		install -v -d "$(DESTDIR)$(BASHCOMPDIR)" && \
+		trap 'rm -f src/completion/.bash' EXIT; \
+		sed 's:^SYSTEM_EXTENSION_DIR=.*:SYSTEM_EXTENSION_DIR="$(LIBDIR)/password-store/extensions":' src/completion/pass.bash-completion > src/completion/.bash && \
+		install -m 0644 -v src/completion/.bash "$(DESTDIR)$(BASHCOMPDIR)/pass"
 	@[ "$(WITH_ZSHCOMP)" = "yes" ] || exit 0; install -v -d "$(DESTDIR)$(ZSHCOMPDIR)" && install -m 0644 -v src/completion/pass.zsh-completion "$(DESTDIR)$(ZSHCOMPDIR)/_pass"
 	@[ "$(WITH_FISHCOMP)" = "yes" ] || exit 0; install -v -d "$(DESTDIR)$(FISHCOMPDIR)" && install -m 0644 -v src/completion/pass.fish-completion "$(DESTDIR)$(FISHCOMPDIR)/pass.fish"
 
diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index bbe9d0c..0a8d843 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -4,6 +4,11 @@
 # Brian Mattern <rephorm at rephorm.com>. All Rights Reserved.
 # This file is licensed under the GPLv2+. Please see COPYING for more information.
 
+SYSTEM_EXTENSION_DIR=""
+PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
+EXTENSIONS="${PASSWORD_STORE_EXTENSIONS_DIR:-$PREFIX/.extensions}"
+
+
 _pass_complete_entries () {
 	prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
@@ -77,11 +82,36 @@ _pass_complete_keys () {
 	COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
 }
 
+_pass_extension_commands() {
+	if [[ -n $SYSTEM_EXTENSION_DIR ]]; then
+		find $SYSTEM_EXTENSION_DIR -name \*.bash  -printf "%f " | sed 's/.bash//g'
+	fi
+
+	if [[ $PASSWORD_STORE_ENABLE_EXTENSIONS == true ]]; then
+		find $EXTENSIONS -name \*.bash  -printf "%f " | sed 's/.bash//g'
+	fi
+}
+
+_pass_extension_completion_file() {
+	local extension_command=$1
+	if [[ $PASSWORD_STORE_ENABLE_EXTENSIONS == true ]] && [ -f $EXTENSIONS/$extension_command.bash.completion ]; then
+		echo $EXTENSIONS/$extension_command.bash.completion
+		return 0
+	fi
+
+	if [[ -n $SYSTEM_EXTENSION_DIR ]] && [ -f $SYSTEM_EXTENSION_DIR/$extension_command.bash.completion ]; then
+		echo $SYSTEM_EXTENSION_DIR/$extension_command.bash.completion
+		return 0
+	fi
+
+	return 1
+}
+
 _pass()
 {
 	COMPREPLY=()
 	local cur="${COMP_WORDS[COMP_CWORD]}"
-	local commands="init ls find grep show insert generate edit rm mv cp git help version ${PASSWORD_STORE_EXTENSION_COMMANDS[*]}"
+	local commands="init ls find grep show insert generate edit rm mv cp git help version $(_pass_extension_commands)"
 	if [[ $COMP_CWORD -gt 1 ]]; then
 		local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
 		case "${COMP_WORDS[1]}" in
@@ -122,15 +152,13 @@ _pass()
 				;;
 		esac
 
-		# To add completion for an extension command define a function like this:
-		# __password_store_extension_complete_<COMMAND>() {
-		#     COMPREPLY+=($(compgen -W "-o --option" -- ${cur}))
-		#     _pass_complete_entries 1
-		# }
+		# To add completion for an extension command create <COMMAND>.bash.completion in the extension folder that contains the completion code like this:
+		#
+		# COMPREPLY+=($(compgen -W "-o --option" -- ${cur}))
+		# _pass_complete_entries 1
 		#
-		# and add the command to the $PASSWORD_STORE_EXTENSION_COMMANDS array
-		if [[ " ${PASSWORD_STORE_EXTENSION_COMMANDS[*]} " == *" ${COMP_WORDS[1]} "* ]] && type "__password_store_extension_complete_${COMP_WORDS[1]}" &> /dev/null; then
-			"__password_store_extension_complete_${COMP_WORDS[1]}"
+		if [[ " $(_pass_extension_commands)" == *" ${COMP_WORDS[1]} "* ]] && [ -n "$(_pass_extension_completion_file ${COMP_WORDS[1]})" ] ; then
+			source $(_pass_extension_completion_file ${COMP_WORDS[1]})
 		fi
 	else
 		COMPREPLY+=($(compgen -W "${commands}" -- ${cur}))
-- 
2.19.1



More information about the Password-Store mailing list