Created
November 20, 2021 16:09
-
-
Save jlisher/f07e8905eb6e896fc864a462efbbb307 to your computer and use it in GitHub Desktop.
Composer inside a container with completions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
composer() { | |
if ! docker volume exists composer_data; then | |
docker volume create composer_data | |
fi | |
docker run --rm \ | |
--interactive \ | |
--tty \ | |
--volume composer_data:/tmp \ | |
--user "$(id -u):$(id -g)" \ | |
--volume "$PWD:/app" \ | |
docker.io/library/composer "${@}" | |
return $? | |
} | |
_composer_completion() { | |
local tmp_filepath="/var/tmp/composer-list.json" | |
local cur_command="list" | |
local word_list="" | |
local options_list="" | |
if ! test -f "${tmp_filepath}"; then | |
composer --format=json list >"${tmp_filepath}" | |
fi | |
if test "${COMP_CWORD}" -eq 1; then | |
word_list="$(jq --raw-output '.namespaces[].commands | join(" ")' "${tmp_filepath}")" | |
else | |
cur_command="${COMP_WORDS[1]}" | |
fi | |
options_list="$(jq --arg cur_command "${cur_command}" --raw-output '.commands[] | select(.name==$cur_command) | [.definition.options[].name] | join(" ")' "${tmp_filepath}")" | |
word_list="${word_list} ${options_list}" | |
# shellcheck disable=SC2207 | |
COMPREPLY+=($(compgen -W "${word_list}" -- "${COMP_WORDS[COMP_CWORD]}")) | |
} | |
complete -o bashdefault -F _composer_completion composer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment