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

Lars Flitter password-store at larsflitter.de
Sat Feb 2 23:44:30 CET 2019


---
 Makefile                            | 14 +++++--
 src/completion/pass.bash-completion | 61 +++++++++++++++++++++++------
 2 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index eac2291..5e1be54 100644
--- a/Makefile
+++ b/Makefile
@@ -36,9 +36,17 @@ 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_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"
+	@[ "$(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"
 
 
 ifneq ($(strip $(wildcard $(PLATFORMFILE))),)
diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index 95d3e1e..3b1fb94 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -4,6 +4,10 @@
 # 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 () {
 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
@@ -80,11 +84,51 @@ _pass_complete_keys () {
 	COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
 }
 
+_pass_extension_commands() {
+	local file
+	if [[ -d "$SYSTEM_EXTENSION_DIR" ]]; then
+		for file in "$SYSTEM_EXTENSION_DIR"/*.bash; do
+			if [[ -f "$file" ]]; then
+				file="${file##*/}"
+				echo "${file%.bash}"
+			fi
+		done
+	fi
+
+	if [[ $PASSWORD_STORE_ENABLE_EXTENSIONS == true ]]; then
+		for file in "$EXTENSIONS"/*.bash; do
+			if [[ -f "$file" ]]; then
+				file="${file##*/}"
+				echo "${file%.bash}"
+			fi
+		 done
+	fi
+}
+
+# To add completion for an extension command create <COMMAND>.bash-completion in the extension folder.
+# <COMMAND>.bash-completion should contains the completion code like this:
+#
+# COMPREPLY+=($(compgen -W "-o --option" -- "${cur}"))
+# _pass_complete_entries 1
+#
+_source_extension_completion() {
+	local extension_command=$1
+	local user_extension_completion system_extension_commpletion
+
+	[[ -n $SYSTEM_EXTENSION_DIR ]] && system_extension_commpletion="$SYSTEM_EXTENSION_DIR/$extension_command.bash-completion"
+	[[ $PASSWORD_STORE_ENABLE_EXTENSIONS == true ]] && user_extension_completion="$EXTENSIONS/$extension_command.bash-completion"
+
+	if [[ -n $user_extension_completion && -f $user_extension_completion ]]; then
+		source "$user_extension_completion"
+	elif [[ -n $system_extension_commpletion && -f $system_extension_commpletion ]]; then
+		source "$system_extension_commpletion"
+	fi
+}
+
 _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[*]}"
 	if [[ $COMP_CWORD -gt 1 ]]; then
 		local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
 		case "${COMP_WORDS[1]}" in
@@ -123,19 +167,12 @@ _pass()
 			git)
 				COMPREPLY+=($(compgen -W "init push pull config log reflog rebase" -- ${cur}))
 				;;
+			*)
+				_source_extension_completion "${COMP_WORDS[1]}"
+				;;
 		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
-		# }
-		#
-		# 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]}"
-		fi
 	else
+		local commands="init ls find grep show insert generate edit rm mv cp git help version $(_pass_extension_commands)"
 		COMPREPLY+=($(compgen -W "${commands}" -- ${cur}))
 		_pass_complete_entries 1
 	fi
-- 
2.20.1



More information about the Password-Store mailing list