Skip to content

Commit 12834ee

Browse files
authored
Merge pull request #1542 from thaJeztah/18.09_backport_completion_cli_experimental
[18.09 backport] Add bash completion for experimental CLI commands (manifest)
2 parents bb46da9 + 2ac475c commit 12834ee

File tree

2 files changed

+156
-29
lines changed

2 files changed

+156
-29
lines changed

cli/command/manifest/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type osArch struct {
1818
// list of valid os/arch values (see "Optional Environment Variables" section
1919
// of https://golang.org/doc/install/source
2020
// Added linux/s390x as we know System z support already exists
21+
// Keep in sync with _docker_manifest_annotate in contrib/completion/bash/docker
2122
var validOSArches = map[osArch]bool{
2223
{os: "darwin", arch: "386"}: true,
2324
{os: "darwin", arch: "amd64"}: true,

contrib/completion/bash/docker

Lines changed: 155 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -563,23 +563,39 @@ __docker_append_to_completions() {
563563
COMPREPLY=( ${COMPREPLY[@]/%/"$1"} )
564564
}
565565

566-
# __docker_daemon_is_experimental tests whether the currently configured Docker
567-
# daemon runs in experimental mode. If so, the function exits with 0 (true).
566+
# __docker_fetch_info fetches information about the configured Docker server and updates
567+
# several variables with the results.
568+
# The result is cached for the duration of one invocation of bash completion.
569+
__docker_fetch_info() {
570+
if [ -z "$info_fetched" ] ; then
571+
read -r client_experimental server_experimental server_os < <(__docker_q version -f '{{.Client.Experimental}} {{.Server.Experimental}} {{.Server.Os}}')
572+
info_fetched=true
573+
fi
574+
}
575+
576+
# __docker_client_is_experimental tests whether the Docker cli is configured to support
577+
# experimental features. If so, the function exits with 0 (true).
578+
# Otherwise, or if the result cannot be determined, the exit value is 1 (false).
579+
__docker_client_is_experimental() {
580+
__docker_fetch_info
581+
[ "$client_experimental" = "true" ]
582+
}
583+
584+
# __docker_server_is_experimental tests whether the currently configured Docker
585+
# server runs in experimental mode. If so, the function exits with 0 (true).
568586
# Otherwise, or if the result cannot be determined, the exit value is 1 (false).
569-
__docker_daemon_is_experimental() {
570-
[ "$(__docker_q version -f '{{.Server.Experimental}}')" = "true" ]
587+
__docker_server_is_experimental() {
588+
__docker_fetch_info
589+
[ "$server_experimental" = "true" ]
571590
}
572591

573-
# __docker_daemon_os_is tests whether the currently configured Docker daemon runs
592+
# __docker_server_os_is tests whether the currently configured Docker server runs
574593
# on the operating system passed in as the first argument.
575-
# It does so by querying the daemon for its OS. The result is cached for the duration
576-
# of one invocation of bash completion so that this function can be used to test for
577-
# several different operating systems without additional costs.
578594
# Known operating systems: linux, windows.
579-
__docker_daemon_os_is() {
595+
__docker_server_os_is() {
580596
local expected_os="$1"
581-
local actual_os=${daemon_os=$(__docker_q version -f '{{.Server.Os}}')}
582-
[ "$actual_os" = "$expected_os" ]
597+
__docker_fetch_info
598+
[ "$server_os" = "$expected_os" ]
583599
}
584600

585601
# __docker_stack_orchestrator_is tests whether the client is configured to use
@@ -1128,7 +1144,8 @@ _docker_docker() {
11281144
*)
11291145
local counter=$( __docker_pos_first_nonflag "$(__docker_to_extglob "$global_options_with_args")" )
11301146
if [ "$cword" -eq "$counter" ]; then
1131-
__docker_daemon_is_experimental && commands+=(${experimental_commands[*]})
1147+
__docker_client_is_experimental && commands+=(${experimental_client_commands[*]})
1148+
__docker_server_is_experimental && commands+=(${experimental_server_commands[*]})
11321149
COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
11331150
fi
11341151
;;
@@ -1837,14 +1854,14 @@ _docker_container_run_and_create() {
18371854
--volume -v
18381855
--workdir -w
18391856
"
1840-
__docker_daemon_os_is windows && options_with_args+="
1857+
__docker_server_os_is windows && options_with_args+="
18411858
--cpu-count
18421859
--cpu-percent
18431860
--io-maxbandwidth
18441861
--io-maxiops
18451862
--isolation
18461863
"
1847-
__docker_daemon_is_experimental && options_with_args+="
1864+
__docker_server_is_experimental && options_with_args+="
18481865
--platform
18491866
"
18501867

@@ -1960,7 +1977,7 @@ _docker_container_run_and_create() {
19601977
return
19611978
;;
19621979
--isolation)
1963-
if __docker_daemon_os_is windows ; then
1980+
if __docker_server_os_is windows ; then
19641981
__docker_complete_isolation
19651982
return
19661983
fi
@@ -2071,12 +2088,12 @@ _docker_container_start() {
20712088
__docker_complete_detach_keys && return
20722089
case "$prev" in
20732090
--checkpoint)
2074-
if __docker_daemon_is_experimental ; then
2091+
if __docker_server_is_experimental ; then
20752092
return
20762093
fi
20772094
;;
20782095
--checkpoint-dir)
2079-
if __docker_daemon_is_experimental ; then
2096+
if __docker_server_is_experimental ; then
20802097
_filedir -d
20812098
return
20822099
fi
@@ -2086,7 +2103,7 @@ _docker_container_start() {
20862103
case "$cur" in
20872104
-*)
20882105
local options="--attach -a --detach-keys --help --interactive -i"
2089-
__docker_daemon_is_experimental && options+=" --checkpoint --checkpoint-dir"
2106+
__docker_server_is_experimental && options+=" --checkpoint --checkpoint-dir"
20902107
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
20912108
;;
20922109
*)
@@ -2449,7 +2466,7 @@ _docker_daemon() {
24492466
}
24502467

24512468
_docker_deploy() {
2452-
__docker_daemon_is_experimental && _docker_stack_deploy
2469+
__docker_server_is_experimental && _docker_stack_deploy
24532470
}
24542471

24552472
_docker_diff() {
@@ -2535,7 +2552,7 @@ _docker_image_build() {
25352552
--target
25362553
--ulimit
25372554
"
2538-
__docker_daemon_os_is windows && options_with_args+="
2555+
__docker_server_os_is windows && options_with_args+="
25392556
--isolation
25402557
"
25412558

@@ -2549,7 +2566,7 @@ _docker_image_build() {
25492566
--quiet -q
25502567
--rm
25512568
"
2552-
if __docker_daemon_is_experimental ; then
2569+
if __docker_server_is_experimental ; then
25532570
options_with_args+="
25542571
--platform
25552572
"
@@ -2584,7 +2601,7 @@ _docker_image_build() {
25842601
return
25852602
;;
25862603
--isolation)
2587-
if __docker_daemon_os_is windows ; then
2604+
if __docker_server_os_is windows ; then
25882605
__docker_complete_isolation
25892606
return
25902607
fi
@@ -2779,7 +2796,7 @@ _docker_image_pull() {
27792796
case "$cur" in
27802797
-*)
27812798
local options="--all-tags -a --disable-content-trust=false --help"
2782-
__docker_daemon_is_experimental && options+=" --platform"
2799+
__docker_server_is_experimental && options+=" --platform"
27832800

27842801
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
27852802
;;
@@ -3430,7 +3447,7 @@ _docker_service_update_and_create() {
34303447
--user -u
34313448
--workdir -w
34323449
"
3433-
__docker_daemon_os_is windows && options_with_args+="
3450+
__docker_server_os_is windows && options_with_args+="
34343451
--credential-spec
34353452
"
34363453

@@ -3820,6 +3837,109 @@ _docker_swarm_update() {
38203837
esac
38213838
}
38223839

3840+
_docker_manifest() {
3841+
local subcommands="
3842+
annotate
3843+
create
3844+
inspect
3845+
push
3846+
"
3847+
__docker_subcommands "$subcommands" && return
3848+
3849+
case "$cur" in
3850+
-*)
3851+
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
3852+
;;
3853+
*)
3854+
COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
3855+
;;
3856+
esac
3857+
}
3858+
3859+
_docker_manifest_annotate() {
3860+
case "$prev" in
3861+
--arch)
3862+
COMPREPLY=( $( compgen -W "
3863+
386
3864+
amd64
3865+
arm
3866+
arm64
3867+
mips64
3868+
mips64le
3869+
ppc64le
3870+
s390x" -- "$cur" ) )
3871+
return
3872+
;;
3873+
--os)
3874+
COMPREPLY=( $( compgen -W "
3875+
darwin
3876+
dragonfly
3877+
freebsd
3878+
linux
3879+
netbsd
3880+
openbsd
3881+
plan9
3882+
solaris
3883+
windows" -- "$cur" ) )
3884+
return
3885+
;;
3886+
--os-features|--variant)
3887+
return
3888+
;;
3889+
esac
3890+
3891+
case "$cur" in
3892+
-*)
3893+
COMPREPLY=( $( compgen -W "--arch --help --os --os-features --variant" -- "$cur" ) )
3894+
;;
3895+
*)
3896+
local counter=$( __docker_pos_first_nonflag "--arch|--os|--os-features|--variant" )
3897+
if [ "$cword" -eq "$counter" ] || [ "$cword" -eq "$((counter + 1))" ]; then
3898+
__docker_complete_images --force-tag --id
3899+
fi
3900+
;;
3901+
esac
3902+
}
3903+
3904+
_docker_manifest_create() {
3905+
case "$cur" in
3906+
-*)
3907+
COMPREPLY=( $( compgen -W "--amend -a --help --insecure" -- "$cur" ) )
3908+
;;
3909+
*)
3910+
__docker_complete_images --force-tag --id
3911+
;;
3912+
esac
3913+
}
3914+
3915+
_docker_manifest_inspect() {
3916+
case "$cur" in
3917+
-*)
3918+
COMPREPLY=( $( compgen -W "--help --insecure --verbose -v" -- "$cur" ) )
3919+
;;
3920+
*)
3921+
local counter=$( __docker_pos_first_nonflag )
3922+
if [ "$cword" -eq "$counter" ] || [ "$cword" -eq "$((counter + 1))" ]; then
3923+
__docker_complete_images --force-tag --id
3924+
fi
3925+
;;
3926+
esac
3927+
}
3928+
3929+
_docker_manifest_push() {
3930+
case "$cur" in
3931+
-*)
3932+
COMPREPLY=( $( compgen -W "--help --insecure --purge -p" -- "$cur" ) )
3933+
;;
3934+
*)
3935+
local counter=$( __docker_pos_first_nonflag )
3936+
if [ "$cword" -eq "$counter" ]; then
3937+
__docker_complete_images --force-tag --id
3938+
fi
3939+
;;
3940+
esac
3941+
}
3942+
38233943
_docker_node() {
38243944
local subcommands="
38253945
demote
@@ -4454,7 +4574,7 @@ _docker_stack_deploy() {
44544574
case "$cur" in
44554575
-*)
44564576
local options="--compose-file -c --help --orchestrator"
4457-
__docker_daemon_is_experimental && __docker_stack_orchestrator_is swarm && options+=" --bundle-file"
4577+
__docker_server_is_experimental && __docker_stack_orchestrator_is swarm && options+=" --bundle-file"
44584578
__docker_stack_orchestrator_is kubernetes && options+=" --kubeconfig --namespace"
44594579
__docker_stack_orchestrator_is swarm && options+=" --prune --resolve-image --with-registry-auth"
44604580
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
@@ -5077,7 +5197,11 @@ _docker() {
50775197
wait
50785198
)
50795199

5080-
local experimental_commands=(
5200+
local experimental_client_commands=(
5201+
manifest
5202+
)
5203+
5204+
local experimental_server_commands=(
50815205
checkpoint
50825206
deploy
50835207
)
@@ -5101,10 +5225,12 @@ _docker() {
51015225
--tlskey
51025226
"
51035227

5104-
local host config daemon_os
5105-
5228+
# variables to cache server info, populated on demand for performance reasons
5229+
local info_fetched server_experimental server_os
51065230
# variables to cache client info, populated on demand for performance reasons
5107-
local stack_orchestrator_is_kubernetes stack_orchestrator_is_swarm
5231+
local client_experimental stack_orchestrator_is_kubernetes stack_orchestrator_is_swarm
5232+
5233+
local host config
51085234

51095235
COMPREPLY=()
51105236
local cur prev words cword

0 commit comments

Comments
 (0)