Refactor usage of `docker version` in bash completion

This preapares bash completion for more context sensitivity:

- experimental cli features
- orchestrator specific features

Also renames _daemon_ to _server_ where used in context of `docker version`
because the fields there are grouped unter _Server_.

Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
Harald Albers 2018-07-31 13:22:20 +02:00
parent deb84a9e4e
commit 564d4da06e
1 changed files with 38 additions and 28 deletions

View File

@ -563,23 +563,31 @@ __docker_append_to_completions() {
COMPREPLY=( ${COMPREPLY[@]/%/"$1"} )
}
# __docker_daemon_is_experimental tests whether the currently configured Docker
# daemon runs in experimental mode. If so, the function exits with 0 (true).
# Otherwise, or if the result cannot be determined, the exit value is 1 (false).
__docker_daemon_is_experimental() {
[ "$(__docker_q version -f '{{.Server.Experimental}}')" = "true" ]
# __docker_fetch_info fetches information about the configured Docker server and updates
# several variables with the results.
# The result is cached for the duration of one invocation of bash completion.
__docker_fetch_info() {
if [ -z "$info_fetched" ] ; then
read -r server_experimental server_os < <(__docker_q version -f '{{.Server.Experimental}} {{.Server.Os}}')
info_fetched=true
fi
}
# __docker_daemon_os_is tests whether the currently configured Docker daemon runs
# __docker_server_is_experimental tests whether the currently configured Docker
# server runs in experimental mode. If so, the function exits with 0 (true).
# Otherwise, or if the result cannot be determined, the exit value is 1 (false).
__docker_server_is_experimental() {
__docker_fetch_info
[ "$server_experimental" = "true" ]
}
# __docker_server_os_is tests whether the currently configured Docker server runs
# on the operating system passed in as the first argument.
# It does so by querying the daemon for its OS. The result is cached for the duration
# of one invocation of bash completion so that this function can be used to test for
# several different operating systems without additional costs.
# Known operating systems: linux, windows.
__docker_daemon_os_is() {
__docker_server_os_is() {
local expected_os="$1"
local actual_os=${daemon_os=$(__docker_q version -f '{{.Server.Os}}')}
[ "$actual_os" = "$expected_os" ]
__docker_fetch_info
[ "$server_os" = "$expected_os" ]
}
# __docker_stack_orchestrator_is tests whether the client is configured to use
@ -1128,7 +1136,7 @@ _docker_docker() {
*)
local counter=$( __docker_pos_first_nonflag "$(__docker_to_extglob "$global_options_with_args")" )
if [ "$cword" -eq "$counter" ]; then
__docker_daemon_is_experimental && commands+=(${experimental_commands[*]})
__docker_server_is_experimental && commands+=(${experimental_commands[*]})
COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
fi
;;
@ -1837,14 +1845,14 @@ _docker_container_run_and_create() {
--volume -v
--workdir -w
"
__docker_daemon_os_is windows && options_with_args+="
__docker_server_os_is windows && options_with_args+="
--cpu-count
--cpu-percent
--io-maxbandwidth
--io-maxiops
--isolation
"
__docker_daemon_is_experimental && options_with_args+="
__docker_server_is_experimental && options_with_args+="
--platform
"
@ -1960,7 +1968,7 @@ _docker_container_run_and_create() {
return
;;
--isolation)
if __docker_daemon_os_is windows ; then
if __docker_server_os_is windows ; then
__docker_complete_isolation
return
fi
@ -2071,12 +2079,12 @@ _docker_container_start() {
__docker_complete_detach_keys && return
case "$prev" in
--checkpoint)
if __docker_daemon_is_experimental ; then
if __docker_server_is_experimental ; then
return
fi
;;
--checkpoint-dir)
if __docker_daemon_is_experimental ; then
if __docker_server_is_experimental ; then
_filedir -d
return
fi
@ -2086,7 +2094,7 @@ _docker_container_start() {
case "$cur" in
-*)
local options="--attach -a --detach-keys --help --interactive -i"
__docker_daemon_is_experimental && options+=" --checkpoint --checkpoint-dir"
__docker_server_is_experimental && options+=" --checkpoint --checkpoint-dir"
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
;;
*)
@ -2449,7 +2457,7 @@ _docker_daemon() {
}
_docker_deploy() {
__docker_daemon_is_experimental && _docker_stack_deploy
__docker_server_is_experimental && _docker_stack_deploy
}
_docker_diff() {
@ -2535,7 +2543,7 @@ _docker_image_build() {
--target
--ulimit
"
__docker_daemon_os_is windows && options_with_args+="
__docker_server_os_is windows && options_with_args+="
--isolation
"
@ -2549,7 +2557,7 @@ _docker_image_build() {
--quiet -q
--rm
"
if __docker_daemon_is_experimental ; then
if __docker_server_is_experimental ; then
options_with_args+="
--platform
"
@ -2584,7 +2592,7 @@ _docker_image_build() {
return
;;
--isolation)
if __docker_daemon_os_is windows ; then
if __docker_server_os_is windows ; then
__docker_complete_isolation
return
fi
@ -2779,7 +2787,7 @@ _docker_image_pull() {
case "$cur" in
-*)
local options="--all-tags -a --disable-content-trust=false --help"
__docker_daemon_is_experimental && options+=" --platform"
__docker_server_is_experimental && options+=" --platform"
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
;;
@ -3431,7 +3439,7 @@ _docker_service_update_and_create() {
--user -u
--workdir -w
"
__docker_daemon_os_is windows && options_with_args+="
__docker_server_os_is windows && options_with_args+="
--credential-spec
"
@ -4451,7 +4459,7 @@ _docker_stack_deploy() {
case "$cur" in
-*)
local options="--compose-file -c --help --orchestrator"
__docker_daemon_is_experimental && __docker_stack_orchestrator_is swarm && options+=" --bundle-file"
__docker_server_is_experimental && __docker_stack_orchestrator_is swarm && options+=" --bundle-file"
__docker_stack_orchestrator_is kubernetes && options+=" --kubeconfig --namespace"
__docker_stack_orchestrator_is swarm && options+=" --prune --resolve-image --with-registry-auth"
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
@ -5098,11 +5106,13 @@ _docker() {
--tlskey
"
local host config daemon_os
# variables to cache server info, populated on demand for performance reasons
local info_fetched server_experimental server_os
# variables to cache client info, populated on demand for performance reasons
local stack_orchestrator_is_kubernetes stack_orchestrator_is_swarm
local host config
COMPREPLY=()
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword