Skip to content
This repository was archived by the owner on Apr 7, 2020. It is now read-only.

Commit c40952b

Browse files
committed
Add bash completion for docker config command family
This adds bash completion for - docker/cli#45 - moby/moby#32336 Signed-off-by: Harald Albers <[email protected]>
1 parent dfdbdab commit c40952b

1 file changed

Lines changed: 160 additions & 0 deletions

File tree

contrib/completion/bash/docker

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# For several commands, the amount of completions can be configured by
2121
# setting environment variables.
2222
#
23+
# DOCKER_COMPLETION_SHOW_CONFIG_IDS
2324
# DOCKER_COMPLETION_SHOW_CONTAINER_IDS
2425
# DOCKER_COMPLETION_SHOW_NETWORK_IDS
2526
# DOCKER_COMPLETION_SHOW_NODE_IDS
@@ -61,6 +62,42 @@ __docker_q() {
6162
docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@"
6263
}
6364

65+
# __docker_configs returns a list of configs. Additional options to
66+
# `docker config ls` may be specified in order to filter the list, e.g.
67+
# `__docker_configs --filter label=stage=production`.
68+
# By default, only names are returned.
69+
# Set DOCKER_COMPLETION_SHOW_CONFIG_IDS=yes to also complete IDs.
70+
# An optional first option `--id|--name` may be used to limit the
71+
# output to the IDs or names of matching items. This setting takes
72+
# precedence over the environment setting.
73+
__docker_configs() {
74+
local format
75+
if [ "$1" = "--id" ] ; then
76+
format='{{.ID}}'
77+
shift
78+
elif [ "$1" = "--name" ] ; then
79+
format='{{.Name}}'
80+
shift
81+
elif [ "$DOCKER_COMPLETION_SHOW_CONFIG_IDS" = yes ] ; then
82+
format='{{.ID}} {{.Name}}'
83+
else
84+
format='{{.Name}}'
85+
fi
86+
87+
__docker_q config ls --format "$format" "$@"
88+
}
89+
90+
# __docker_complete_configs applies completion of configs based on the current value
91+
# of `$cur` or the value of the optional first option `--cur`, if given.
92+
__docker_complete_configs() {
93+
local current="$cur"
94+
if [ "$1" = "--cur" ] ; then
95+
current="$2"
96+
shift 2
97+
fi
98+
COMPREPLY=( $(compgen -W "$(__docker_configs "$@")" -- "$current") )
99+
}
100+
64101
# __docker_containers returns a list of containers. Additional options to
65102
# `docker ps` may be specified in order to filter the list, e.g.
66103
# `__docker_containers --filter status=running`
@@ -1096,6 +1133,117 @@ _docker_checkpoint_rm() {
10961133
}
10971134

10981135

1136+
_docker_config() {
1137+
local subcommands="
1138+
create
1139+
inspect
1140+
ls
1141+
rm
1142+
"
1143+
local aliases="
1144+
list
1145+
remove
1146+
"
1147+
__docker_subcommands "$subcommands $aliases" && return
1148+
1149+
case "$cur" in
1150+
-*)
1151+
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
1152+
;;
1153+
*)
1154+
COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
1155+
;;
1156+
esac
1157+
}
1158+
1159+
_docker_config_create() {
1160+
case "$prev" in
1161+
--label|-l)
1162+
return
1163+
;;
1164+
esac
1165+
1166+
case "$cur" in
1167+
-*)
1168+
COMPREPLY=( $( compgen -W "--help --label -l" -- "$cur" ) )
1169+
;;
1170+
*)
1171+
local counter=$(__docker_pos_first_nonflag '--label|-l')
1172+
if [ $cword -eq $((counter + 1)) ]; then
1173+
_filedir
1174+
fi
1175+
;;
1176+
esac
1177+
}
1178+
1179+
_docker_config_inspect() {
1180+
case "$prev" in
1181+
--format|-f)
1182+
return
1183+
;;
1184+
esac
1185+
1186+
case "$cur" in
1187+
-*)
1188+
COMPREPLY=( $( compgen -W "--format -f --help --pretty" -- "$cur" ) )
1189+
;;
1190+
*)
1191+
__docker_complete_configs
1192+
;;
1193+
esac
1194+
}
1195+
1196+
_docker_config_list() {
1197+
_docker_config_ls
1198+
}
1199+
1200+
_docker_config_ls() {
1201+
local key=$(__docker_map_key_of_current_option '--filter|-f')
1202+
case "$key" in
1203+
id)
1204+
__docker_complete_configs --cur "${cur##*=}" --id
1205+
return
1206+
;;
1207+
name)
1208+
__docker_complete_configs --cur "${cur##*=}" --name
1209+
return
1210+
;;
1211+
esac
1212+
1213+
case "$prev" in
1214+
--filter|-f)
1215+
COMPREPLY=( $( compgen -S = -W "id label name" -- "$cur" ) )
1216+
__docker_nospace
1217+
return
1218+
;;
1219+
--format)
1220+
return
1221+
;;
1222+
esac
1223+
1224+
case "$cur" in
1225+
-*)
1226+
COMPREPLY=( $( compgen -W "--format --filter -f --help --quiet -q" -- "$cur" ) )
1227+
;;
1228+
esac
1229+
}
1230+
1231+
_docker_config_remove() {
1232+
_docker_config_rm
1233+
}
1234+
1235+
_docker_config_rm() {
1236+
case "$cur" in
1237+
-*)
1238+
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
1239+
;;
1240+
*)
1241+
__docker_complete_configs
1242+
;;
1243+
esac
1244+
}
1245+
1246+
10991247
_docker_container() {
11001248
local subcommands="
11011249
attach
@@ -3146,6 +3294,7 @@ _docker_service_update_and_create() {
31463294

31473295
if [ "$subcommand" = "create" ] ; then
31483296
options_with_args="$options_with_args
3297+
--config
31493298
--constraint
31503299
--container-label
31513300
--dns
@@ -3162,6 +3311,10 @@ _docker_service_update_and_create() {
31623311
"
31633312

31643313
case "$prev" in
3314+
--config)
3315+
__docker_complete_configs
3316+
return
3317+
;;
31653318
--env-file)
31663319
_filedir
31673320
return
@@ -3196,6 +3349,8 @@ _docker_service_update_and_create() {
31963349
if [ "$subcommand" = "update" ] ; then
31973350
options_with_args="$options_with_args
31983351
--args
3352+
--config-add
3353+
--config-rm
31993354
--constraint-add
32003355
--constraint-rm
32013356
--container-label-add
@@ -3223,6 +3378,10 @@ _docker_service_update_and_create() {
32233378
"
32243379

32253380
case "$prev" in
3381+
--config-add|--config-rm)
3382+
__docker_complete_configs
3383+
return
3384+
;;
32263385
--group-add|--group-rm)
32273386
COMPREPLY=( $(compgen -g -- "$cur") )
32283387
return
@@ -4594,6 +4753,7 @@ _docker() {
45944753
shopt -s extglob
45954754

45964755
local management_commands=(
4756+
config
45974757
container
45984758
image
45994759
network

0 commit comments

Comments
 (0)