From 0a92d76c4ca2b0a65c2e4d101c46718fb380d87e Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Tue, 24 Dec 2024 12:31:45 +0200 Subject: [PATCH] shfmt and shellcheck, docker-compose.completion is ready --- clean_files.txt | 1 + .../available/docker-compose.completion.bash | 226 ++++++++---------- 2 files changed, 103 insertions(+), 124 deletions(-) mode change 100755 => 100644 completion/available/docker-compose.completion.bash diff --git a/clean_files.txt b/clean_files.txt index 9f3802b46d..02bdfe588d 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -49,6 +49,7 @@ completion/available/dart.completion.bash completion/available/dirs.completion.bash completion/available/django.completion.bash completion/available/dmidecode.completion.bash +completion/available/docker-compose.completion.bash completion/available/docker-machine.completion.bash completion/available/docker.completion.bash completion/available/dotnet.completion.bash diff --git a/completion/available/docker-compose.completion.bash b/completion/available/docker-compose.completion.bash old mode 100755 new mode 100644 index 1102f5d97e..57d414fd5a --- a/completion/available/docker-compose.completion.bash +++ b/completion/available/docker-compose.completion.bash @@ -1,4 +1,5 @@ -#!/bin/bash +# shellcheck shell=bash +# shellcheck disable=SC2207 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,13 +36,13 @@ __docker_compose_previous_extglob_setting=$(shopt -p extglob) shopt -s extglob __docker_compose_q() { - docker-compose 2>/dev/null "${top_level_options[@]}" "$@" + docker-compose 2> /dev/null "${top_level_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 parts=("$1") local IFS='|' echo "${parts[*]}" } @@ -49,7 +50,8 @@ __docker_compose_to_alternatives() { # 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" ) + local extglob + extglob=$(__docker_compose_to_alternatives "$1") echo "@($extglob)" } @@ -57,8 +59,8 @@ __docker_compose_to_extglob() { # the commandline. The option may be a pattern, e.g. `--force|-f`. __docker_compose_has_option() { local pattern="$1" - for (( i=2; i < $cword; ++i)); do - if [[ ${words[$i]} =~ ^($pattern)$ ]] ; then + for ((i = 2; i < cword; ++i)); do + if [[ ${words[$i]} =~ ^($pattern)$ ]]; then return 0 fi done @@ -69,34 +71,33 @@ __docker_compose_has_option() { # which matches the extglob passed in as an argument. # This function is needed for key-specific completions. __docker_compose_map_key_of_current_option() { - local glob="$1" + local glob="$1" - local key glob_pos - if [ "$cur" = "=" ] ; then # key= case - key="$prev" - glob_pos=$((cword - 2)) - elif [[ $cur == *=* ]] ; then # key=value case (OSX) - key=${cur%=*} - glob_pos=$((cword - 1)) - elif [ "$prev" = "=" ] ; then - key=${words[$cword - 2]} # key=value case - glob_pos=$((cword - 3)) - else - return - fi + local key glob_pos + if [ "$cur" = "=" ]; then # key= case + key="$prev" + glob_pos=$((cword - 2)) + elif [[ $cur == *=* ]]; then # key=value case (OSX) + key=${cur%=*} + glob_pos=$((cword - 1)) + elif [ "$prev" = "=" ]; then + key=${words[$cword - 2]} # key=value case + glob_pos=$((cword - 3)) + else + return + fi - [ "${words[$glob_pos]}" = "=" ] && ((glob_pos--)) # --option=key=value syntax + [ "${words[$glob_pos]}" = "=" ] && ((glob_pos--)) # --option=key=value syntax - [[ ${words[$glob_pos]} == @($glob) ]] && echo "$key" + [[ ${words[$glob_pos]} == @($glob) ]] && echo "$key" } # suppress trailing whitespace __docker_compose_nospace() { # compopt is not available in ancient bash versions - type compopt &>/dev/null && compopt -o nospace + type compopt &> /dev/null && compopt -o nospace } - # Outputs a list of all defined services, regardless of their running state. # Arguments for `docker-compose ps` may be passed in order to filter the service list, # e.g. `status=running`. @@ -108,31 +109,31 @@ __docker_compose_services() { # Arguments for `docker-compose ps` may be passed in order to filter the service list, # see `__docker_compose_services`. __docker_compose_complete_services() { - COMPREPLY=( $(compgen -W "$(__docker_compose_services "$@")" -- "$cur") ) + COMPREPLY=($(compgen -W "$(__docker_compose_services "$@")" -- "$cur")) } # The services for which at least one running container exists __docker_compose_complete_running_services() { - local names=$(__docker_compose_services --filter status=running) - COMPREPLY=( $(compgen -W "$names" -- "$cur") ) + local names + names=$(__docker_compose_services --filter status=running) + COMPREPLY=($(compgen -W "$names" -- "$cur")) } - _docker_compose_build() { case "$prev" in --build-arg) - COMPREPLY=( $( compgen -e -- "$cur" ) ) + COMPREPLY=($(compgen -e -- "$cur")) __docker_compose_nospace return ;; - --memory|-m) + --memory | -m) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--build-arg --compress --force-rm --help --memory -m --no-cache --no-rm --pull --parallel -q --quiet" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--build-arg --compress --force-rm --help --memory -m --no-cache --no-rm --pull --parallel -q --quiet" -- "$cur")) ;; *) __docker_compose_complete_services --filter source=build @@ -140,39 +141,36 @@ _docker_compose_build() { esac } - _docker_compose_bundle() { case "$prev" in - --output|-o) + --output | -o) _filedir return ;; esac - COMPREPLY=( $( compgen -W "--push-images --help --output -o" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--push-images --help --output -o" -- "$cur")) } - _docker_compose_config() { case "$prev" in --hash) - if [[ $cur == \\* ]] ; then - COMPREPLY=( '\*' ) + if [[ $cur == \\* ]]; then + COMPREPLY=('\*') else - COMPREPLY=( $(compgen -W "$(__docker_compose_services) \\\* " -- "$cur") ) + COMPREPLY=($(compgen -W "$(__docker_compose_services) \\\* " -- "$cur")) fi return ;; esac - COMPREPLY=( $( compgen -W "--hash --help --quiet -q --resolve-image-digests --services --volumes" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--hash --help --quiet -q --resolve-image-digests --services --volumes" -- "$cur")) } - _docker_compose_create() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--build --force-recreate --help --no-build --no-recreate" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--build --force-recreate --help --no-build --no-recreate" -- "$cur")) ;; *) __docker_compose_complete_services @@ -180,60 +178,57 @@ _docker_compose_create() { esac } - _docker_compose_docker_compose() { case "$prev" in - --tlscacert|--tlscert|--tlskey) + --tlscacert | --tlscert | --tlskey) _filedir return ;; - --file|-f) + --file | -f) _filedir "y?(a)ml" return ;; --log-level) - COMPREPLY=( $( compgen -W "debug info warning error critical" -- "$cur" ) ) + COMPREPLY=($(compgen -W "debug info warning error critical" -- "$cur")) return ;; --project-directory) _filedir -d return ;; - $(__docker_compose_to_extglob "$daemon_options_with_args") ) + "$(__docker_compose_to_extglob "$daemon_options_with_args")") return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "$daemon_boolean_options $daemon_options_with_args $top_level_options_with_args --help -h --no-ansi --verbose --version -v" -- "$cur" ) ) + COMPREPLY=($(compgen -W "$daemon_boolean_options $daemon_options_with_args $top_level_options_with_args --help -h --no-ansi --verbose --version -v" -- "$cur")) ;; *) - COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) + COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur")) ;; esac } - _docker_compose_down() { case "$prev" in --rmi) - COMPREPLY=( $( compgen -W "all local" -- "$cur" ) ) + COMPREPLY=($(compgen -W "all local" -- "$cur")) return ;; - --timeout|-t) + --timeout | -t) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --rmi --timeout -t --volumes -v --remove-orphans" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --rmi --timeout -t --volumes -v --remove-orphans" -- "$cur")) ;; esac } - _docker_compose_events() { case "$prev" in --json) @@ -243,7 +238,7 @@ _docker_compose_events() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --json" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --json" -- "$cur")) ;; *) __docker_compose_complete_services @@ -251,17 +246,16 @@ _docker_compose_events() { esac } - _docker_compose_exec() { case "$prev" in - --index|--user|-u|--workdir|-w) + --index | --user | -u | --workdir | -w) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "-d --detach --help --index --privileged -T --user -u --workdir -w" -- "$cur" ) ) + COMPREPLY=($(compgen -W "-d --detach --help --index --privileged -T --user -u --workdir -w" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -269,15 +263,14 @@ _docker_compose_exec() { esac } - _docker_compose_help() { - COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) + COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur")) } _docker_compose_images() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --quiet -q" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --quiet -q" -- "$cur")) ;; *) __docker_compose_complete_services @@ -288,14 +281,14 @@ _docker_compose_images() { _docker_compose_kill() { case "$prev" in -s) - COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) ) + COMPREPLY=($(compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo "$cur" | tr '[:lower:]' '[:upper:]')")) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help -s" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help -s" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -303,7 +296,6 @@ _docker_compose_kill() { esac } - _docker_compose_logs() { case "$prev" in --tail) @@ -313,7 +305,7 @@ _docker_compose_logs() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--follow -f --help --no-color --tail --timestamps -t" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--follow -f --help --no-color --tail --timestamps -t" -- "$cur")) ;; *) __docker_compose_complete_services @@ -321,11 +313,10 @@ _docker_compose_logs() { esac } - _docker_compose_pause() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -333,21 +324,20 @@ _docker_compose_pause() { esac } - _docker_compose_port() { case "$prev" in --protocol) - COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) ) - return; + COMPREPLY=($(compgen -W "tcp udp" -- "$cur")) + return ;; --index) - return; + return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --index --protocol" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --index --protocol" -- "$cur")) ;; *) __docker_compose_complete_services @@ -355,31 +345,31 @@ _docker_compose_port() { esac } - _docker_compose_ps() { - local key=$(__docker_compose_map_key_of_current_option '--filter') + local key + key=$(__docker_compose_map_key_of_current_option '--filter') case "$key" in source) - COMPREPLY=( $( compgen -W "build image" -- "${cur##*=}" ) ) + COMPREPLY=($(compgen -W "build image" -- "${cur##*=}")) return ;; status) - COMPREPLY=( $( compgen -W "paused restarting running stopped" -- "${cur##*=}" ) ) + COMPREPLY=($(compgen -W "paused restarting running stopped" -- "${cur##*=}")) return ;; esac case "$prev" in --filter) - COMPREPLY=( $( compgen -W "source status" -S "=" -- "$cur" ) ) + COMPREPLY=($(compgen -W "source status" -S "=" -- "$cur")) __docker_compose_nospace - return; + return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--all -a --filter --help --quiet -q --services" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--all -a --filter --help --quiet -q --services" -- "$cur")) ;; *) __docker_compose_complete_services @@ -387,11 +377,10 @@ _docker_compose_ps() { esac } - _docker_compose_pull() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --ignore-pull-failures --include-deps --no-parallel --quiet -q" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --ignore-pull-failures --include-deps --no-parallel --quiet -q" -- "$cur")) ;; *) __docker_compose_complete_services --filter source=image @@ -399,11 +388,10 @@ _docker_compose_pull() { esac } - _docker_compose_push() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --ignore-push-failures" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --ignore-push-failures" -- "$cur")) ;; *) __docker_compose_complete_services @@ -411,17 +399,16 @@ _docker_compose_push() { esac } - _docker_compose_restart() { case "$prev" in - --timeout|-t) + --timeout | -t) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --timeout -t" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -429,14 +416,13 @@ _docker_compose_restart() { esac } - _docker_compose_rm() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--force -f --help --stop -s -v" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--force -f --help --stop -s -v" -- "$cur")) ;; *) - if __docker_compose_has_option "--stop|-s" ; then + if __docker_compose_has_option "--stop|-s"; then __docker_compose_complete_services else __docker_compose_complete_services --filter status=stopped @@ -445,22 +431,21 @@ _docker_compose_rm() { esac } - _docker_compose_run() { case "$prev" in -e) - COMPREPLY=( $( compgen -e -- "$cur" ) ) + COMPREPLY=($(compgen -e -- "$cur")) __docker_compose_nospace return ;; - --entrypoint|--label|-l|--name|--user|-u|--volume|-v|--workdir|-w) + --entrypoint | --label | -l | --name | --user | -u | --volume | -v | --workdir | -w) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--detach -d --entrypoint -e --help --label -l --name --no-deps --publish -p --rm --service-ports -T --use-aliases --user -u --volume -v --workdir -w" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--detach -d --entrypoint -e --help --label -l --name --no-deps --publish -p --rm --service-ports -T --use-aliases --user -u --volume -v --workdir -w" -- "$cur")) ;; *) __docker_compose_complete_services @@ -468,34 +453,32 @@ _docker_compose_run() { esac } - _docker_compose_scale() { case "$prev" in =) COMPREPLY=("$cur") return ;; - --timeout|-t) + --timeout | -t) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --timeout -t" -- "$cur")) ;; *) - COMPREPLY=( $(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur") ) + COMPREPLY=($(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur")) __docker_compose_nospace ;; esac } - _docker_compose_start() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; *) __docker_compose_complete_services --filter status=stopped @@ -503,17 +486,16 @@ _docker_compose_start() { esac } - _docker_compose_stop() { case "$prev" in - --timeout|-t) + --timeout | -t) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help --timeout -t" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -521,11 +503,10 @@ _docker_compose_stop() { esac } - _docker_compose_top() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; *) __docker_compose_complete_running_services @@ -533,11 +514,10 @@ _docker_compose_top() { esac } - _docker_compose_unpause() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; *) __docker_compose_complete_services --filter status=paused @@ -545,7 +525,6 @@ _docker_compose_unpause() { esac } - _docker_compose_up() { case "$prev" in =) @@ -557,18 +536,18 @@ _docker_compose_up() { return ;; --scale) - COMPREPLY=( $(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur") ) + COMPREPLY=($(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur")) __docker_compose_nospace return ;; - --timeout|-t) + --timeout | -t) return ;; esac case "$cur" in -*) - COMPREPLY=( $( compgen -W "--abort-on-container-exit --always-recreate-deps --build -d --detach --exit-code-from --force-recreate --help --no-build --no-color --no-deps --no-recreate --no-start --renew-anon-volumes -V --remove-orphans --scale --timeout -t" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--abort-on-container-exit --always-recreate-deps --build -d --detach --exit-code-from --force-recreate --help --no-build --no-color --no-deps --no-recreate --no-start --renew-anon-volumes -V --remove-orphans --scale --timeout -t" -- "$cur")) ;; *) __docker_compose_complete_services @@ -576,18 +555,17 @@ _docker_compose_up() { esac } - _docker_compose_version() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--short" -- "$cur" ) ) + COMPREPLY=($(compgen -W "--short" -- "$cur")) ;; esac } - _docker_compose() { - local previous_extglob_setting=$(shopt -p extglob) + local previous_extglob_setting + previous_extglob_setting=$(shopt -p extglob) shopt -s extglob local commands=( @@ -651,32 +629,32 @@ _docker_compose() { local top_level_options=() local counter=1 - while [ $counter -lt $cword ]; do + while [ $counter -lt "$cword" ]; do case "${words[$counter]}" in - $(__docker_compose_to_extglob "$daemon_boolean_options") ) + "$(__docker_compose_to_extglob "$daemon_boolean_options")") local opt=${words[counter]} - top_level_options+=($opt) + top_level_options+=("$opt") ;; - $(__docker_compose_to_extglob "$daemon_options_with_args") ) + "$(__docker_compose_to_extglob "$daemon_options_with_args")") local opt=${words[counter]} local arg=${words[++counter]} - top_level_options+=($opt $arg) + top_level_options+=("$opt" "$arg") ;; - $(__docker_compose_to_extglob "$top_level_options_with_args") ) - (( counter++ )) - ;; - -*) + "$(__docker_compose_to_extglob "$top_level_options_with_args")") + ((counter++)) ;; + -*) ;; + *) command="${words[$counter]}" break ;; esac - (( counter++ )) + ((counter++)) done local completions_func=_docker_compose_${command//-/_} - _is_function $completions_func && $completions_func + _is_function "$completions_func" && $completions_func eval "$previous_extglob_setting" return 0