Skip to content

Commit

Permalink
update bash completions
Browse files Browse the repository at this point in the history
  • Loading branch information
thatportugueseguy committed Jan 4, 2025
1 parent 8f2d6b0 commit 71fd6b6
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions passage-completion.sh → completions/passage-completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,48 @@ _passage_list_secrets() {
_passage_list_recipients() {
local CONF_DIR="${PASSAGE_DIR:-${HOME}/.config/passage}"
local KEYS_DIR="${PASSAGE_KEYS:-${CONF_DIR}/keys}"
# First list groups with @ prefix
find -L "${KEYS_DIR}/" -name '*.group' -type f -printf '@%P\n' | sed s/.group$//
# Then list regular recipients
find -L "${KEYS_DIR}/" -name '*.pub' -type f -printf '%P\n' | sed s/.pub$//
}

_passage_list_recipients_and_paths() {
local CONF_DIR="${PASSAGE_DIR:-${HOME}/.config/passage}"
local KEYS_DIR="${PASSAGE_KEYS:-${CONF_DIR}/keys}"
local SECR_DIR="${PASSAGE_SECRETS:-${CONF_DIR}/secrets}"

# If we're at the top level or no path is typed yet, show groups and top-level secrets
if [[ "${COMP_WORDS[$COMP_CWORD]}" != */* ]]; then
# First list groups with @ prefix
find -L "${KEYS_DIR}/" -name '*.group' -type f -printf '@%P\n' | sed s/.group$//
# Then list only top-level secrets and folders, excluding base dir
find -L "${SECR_DIR}/" -mindepth 1 -maxdepth 1 \( -name '*.age' -type f -o -type d \) -printf '%P\n' | \
while read -r entry; do
if [[ -d "${SECR_DIR}/$entry" ]]; then
echo "$entry/"
elif [[ $entry == *.age ]]; then
echo "${entry%.age}"
else
echo "$entry"
fi
done
else
# For deeper levels, only show secrets and folders in that path
local current_path="${SECR_DIR}/${COMP_WORDS[$COMP_CWORD]%/*}"
find -L "$current_path/" -mindepth 1 -maxdepth 1 \( -name '*.age' -type f -o -type d \) -printf "%P\n" | \
while read -r entry; do
if [[ -d "${current_path}/$entry" ]]; then
echo "${COMP_WORDS[$COMP_CWORD]%/*}/$entry/"
elif [[ $entry == *.age ]]; then
echo "${COMP_WORDS[$COMP_CWORD]%/*}/${entry%.age}"
else
echo "${COMP_WORDS[$COMP_CWORD]%/*}/$entry"
fi
done
fi
}

# Do completion from a passed list of paths
#
# Accepts 2 arguments
Expand Down Expand Up @@ -61,7 +100,7 @@ _passage_paths_completion() {
_passage_completions()
{
if [ "${#COMP_WORDS[@]}" -le 2 ]; then
local cmds="create delete edit edit-who get head list ls new realpath refresh replace search secret show template template-secrets what who"
local cmds="cat create delete edit edit-who get healthcheck init list ls new realpath refresh replace replace-comment rm search secret show subst template template-secrets what who"
COMPREPLY=($(compgen -W "$cmds" -- "${COMP_WORDS[1]}"))
else
case "${COMP_WORDS[1]}" in
Expand All @@ -84,8 +123,14 @@ _passage_completions()
if [[ $COMP_CWORD == 2 ]]; then
COMPREPLY=($(compgen -W "$(_passage_list_recipients)" -- "${COMP_WORDS[$COMP_CWORD]}"))
fi;;
*)
who)
# add autocomplete only for 2nd arg
if [[ $COMP_CWORD == 2 ]]; then
compopt -o nospace
COMPREPLY=($(compgen -W "$(_passage_list_recipients_and_paths)" -- "${COMP_WORDS[$COMP_CWORD]}"))
fi;;
*)
# add autocomplete only for 2nd arg for remaining commands
if [[ $COMP_CWORD == 2 ]]; then
_passage_paths_completion "$(_passage_list_secrets)" "${COMP_WORDS[$COMP_CWORD]}"
fi;;
Expand Down

0 comments on commit 71fd6b6

Please sign in to comment.