Skip to content

Commit

Permalink
prepare bash completion for new TLS options
Browse files Browse the repository at this point in the history
Up to now there were two special top-level options that required special
treatment: --project-name and --file (and their short forms).
For 1.7.0, several TLS related options were added that have to be passed
to secondary docker-compose invocations as well.
This commit introduces a scalable treatment of those options.

Signed-off-by: Harald Albers <[email protected]>
  • Loading branch information
albers committed Mar 24, 2016
1 parent 2a09fb0 commit 9094c4d
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions contrib/completion/bash/docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@


__docker_compose_q() {
local file_args
if [ ${#compose_files[@]} -ne 0 ] ; then
file_args="${compose_files[@]/#/-f }"
fi
docker-compose 2>/dev/null $file_args ${compose_project:+-p $compose_project} "$@"
docker-compose 2>/dev/null $daemon_options "$@"
}

# Transforms a multiline list of strings into a single line string
# with the words separated by "|".
__docker_compose_to_alternatives() {
local parts=( $1 )
local IFS='|'
echo "${parts[*]}"
}

# Transforms a multiline list of options into an extglob pattern
# suitable for use in case statements.
__docker_compose_to_extglob() {
local extglob=$( __docker_compose_to_alternatives "$1" )
echo "@($extglob)"
}

# suppress trailing whitespace
Expand All @@ -31,20 +42,6 @@ __docker_compose_nospace() {
type compopt &>/dev/null && compopt -o nospace
}

# For compatibility reasons, Compose and therefore its completion supports several
# stack compositon files as listed here, in descending priority.
# Support for these filenames might be dropped in some future version.
__docker_compose_compose_file() {
local file
for file in docker-compose.y{,a}ml ; do
[ -e $file ] && {
echo $file
return
}
done
echo docker-compose.yml
}

# Extracts all service names from the compose file.
___docker_compose_all_services_in_compose_file() {
__docker_compose_q config --services
Expand Down Expand Up @@ -142,7 +139,7 @@ _docker_compose_docker_compose() {

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--file -f --help -h --project-name -p --verbose --version -v" -- "$cur" ) )
COMPREPLY=( $( compgen -W "$daemon_options_with_args --help -h --verbose --version -v" -- "$cur" ) )
;;
*)
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
Expand Down Expand Up @@ -452,24 +449,29 @@ _docker_compose() {
version
)

# options for the docker daemon that have to be passed to secondary calls to
# docker-compose executed by this script
local daemon_options_with_args="
--file -f
--project-name -p
"

COMPREPLY=()
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword

# search subcommand and invoke its handler.
# special treatment of some top-level options
local command='docker_compose'
local daemon_options=()
local counter=1
local compose_files=() compose_project

while [ $counter -lt $cword ]; do
case "${words[$counter]}" in
--file|-f)
(( counter++ ))
compose_files+=(${words[$counter]})
;;
--project-name|-p)
(( counter++ ))
compose_project="${words[$counter]}"
$(__docker_compose_to_extglob "$daemon_options_with_args") )
local opt=${words[counter]}
local arg=${words[++counter]}
daemon_options+=($opt $arg)
;;
-*)
;;
Expand Down

0 comments on commit 9094c4d

Please sign in to comment.