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