From 682fbe2f03113f49384adcad5d503ca885b69dc9 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Sat, 10 Aug 2024 02:11:45 -0700 Subject: [PATCH] client: completion: add `plugins` support --- client/src/docker/completion.bash | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/client/src/docker/completion.bash b/client/src/docker/completion.bash index 6e6d18a..b5477ed 100644 --- a/client/src/docker/completion.bash +++ b/client/src/docker/completion.bash @@ -22,6 +22,9 @@ if ! hash docker &>/dev/null; then exit 0 fi +# TODO: HACK: needed for `plugins` completion +declare -gx instance + function docker-finance::completion() { local _cur="${COMP_WORDS[COMP_CWORD]}" @@ -31,7 +34,7 @@ function docker-finance::completion() mapfile -t _images < <(docker image ls | grep ^docker-finance | cut -d'/' -f2- | cut -d' ' -f1-4 | sed -e 's/ /:/') declare -r _images - local -r _commands=("gen" "edit" "build" "backup" "rm" "up" "down" "start" "stop" "run" "shell" "version" "license" "linter" "doxygen") + local -r _commands=("gen" "edit" "build" "backup" "rm" "up" "down" "start" "stop" "run" "shell" "version" "license" "linter" "doxygen" "plugins") local _reply @@ -43,6 +46,7 @@ function docker-finance::completion() for _r in "${_reply[@]}"; do COMPREPLY+=("'${_r}'") done + instance="${COMPREPLY[*]}" ;; 2) mapfile -t _reply < <(compgen -W "${_commands[*]}" -- "$_cur") @@ -85,6 +89,31 @@ function docker-finance::completion() doxygen) mapfile -t _reply < <(compgen -W "help gen${global_arg_delim_2} rm${global_arg_delim_2}" -- "$_cur") ;; + plugins) + # TODO: HACK: since this is completion, lib_docker is never initialized, prior to completing + # (environment variables are needed). + if [ -z "$DOCKER_FINANCE_CLIENT_REPO" ]; then + _aliases=~/.bash_aliases + ! test -f "$_aliases" && _aliases=~/.bashrc + + declare -g DOCKER_FINANCE_CLIENT_REPO + DOCKER_FINANCE_CLIENT_REPO="$(dirname "$(grep '^alias docker-finance=' $_aliases | cut -d'=' -f2 | sed -e "s:'::g" -e 's:"::g')")/../" + source "${DOCKER_FINANCE_CLIENT_REPO}/client/src/docker/lib/lib_docker.bash" || echo $? + + local _instance + declare -r _instance="$(echo $instance | sed "s:'::g")" # TODO: remove after hack for colon tag is resolved + lib_docker::docker "$_instance" || lib_utils::print_fatal "$_instance not initialized" + fi + [ -z "$DOCKER_FINANCE_CLIENT_PLUGINS" ] && lib_utils::print_fatal "DOCKER_FINANCE_CLIENT_PLUGINS not set" + + local _plugins + mapfile -t _plugins < <({ + find "${DOCKER_FINANCE_CLIENT_PLUGINS}"/client/docker -type f -executable -printf 'custom/%P\n' 2>/dev/null + find "${DOCKER_FINANCE_CLIENT_REPO}"/client/plugins/docker -type f -executable -printf 'repo/%P\n' 2>/dev/null + }) + declare -r _plugins + mapfile -t _reply < <(compgen -W "${_plugins[*]}" -- "$_cur") + ;; esac declare -r _reply COMPREPLY=("${_reply[@]}")