-
Notifications
You must be signed in to change notification settings - Fork 5
/
docker-compose-auto-complete
679 lines (584 loc) · 13.2 KB
/
docker-compose-auto-complete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#!/bin/bash
#
# bash completion for docker-compose
#
# This work is based on the completion for the docker command.
#
# This script provides completion of:
# - commands and their options
# - service names
# - filepaths
#
# To enable the completions either:
# - place this file in /etc/bash_completion.d
# or
# - copy this file to e.g. ~/.docker-compose-completion.sh and add the line
# below to your .bashrc after bash completion features are loaded
# . ~/.docker-compose-completion.sh
# this file should be placed in /usr/share/bash-completion/completions name: docker-compose
__docker_compose_previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob
__docker_compose_q() {
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 IFS='|'
echo "${parts[*]}"
}
# 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" )
echo "@($extglob)"
}
# Determines whether the option passed as the first argument exist on
# 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
return 0
fi
done
return 1
}
# Returns `key` if we are currently completing the value of a map option (`key=value`)
# 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 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) ]] && echo "$key"
}
# suppress trailing whitespace
__docker_compose_nospace() {
# compopt is not available in ancient bash versions
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`.
__docker_compose_services() {
__docker_compose_q ps --services "$@"
}
# Applies completion of services based on the current value of `$cur`.
# 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") )
}
# 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") )
}
_docker_compose_build() {
case "$prev" in
--build-arg)
COMPREPLY=( $( compgen -e -- "$cur" ) )
__docker_compose_nospace
return
;;
--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" ) )
;;
*)
__docker_compose_complete_services --filter source=build
;;
esac
}
_docker_compose_bundle() {
case "$prev" in
--output|-o)
_filedir
return
;;
esac
COMPREPLY=( $( compgen -W "--push-images --help --output -o" -- "$cur" ) )
}
_docker_compose_config() {
case "$prev" in
--hash)
if [[ $cur == \\* ]] ; then
COMPREPLY=( '\*' )
else
COMPREPLY=( $(compgen -W "$(__docker_compose_services) \\\* " -- "$cur") )
fi
return
;;
esac
COMPREPLY=( $( compgen -W "--hash --help --no-interpolate --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" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_docker_compose() {
case "$prev" in
--tlscacert|--tlscert|--tlskey)
_filedir
return
;;
--file|-f)
_filedir "y?(a)ml"
return
;;
--log-level)
COMPREPLY=( $( compgen -W "debug info warning error critical" -- "$cur" ) )
return
;;
--project-directory)
_filedir -d
return
;;
--env-file)
_filedir
return
;;
$(__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 "${commands[*]}" -- "$cur" ) )
;;
esac
}
_docker_compose_down() {
case "$prev" in
--rmi)
COMPREPLY=( $( compgen -W "all local" -- "$cur" ) )
return
;;
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --rmi --timeout -t --volumes -v --remove-orphans" -- "$cur" ) )
;;
esac
}
_docker_compose_events() {
case "$prev" in
--json)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --json" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_exec() {
case "$prev" in
--index|--user|-u|--workdir|-w)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "-d --detach --help --index --privileged -T --user -u --workdir -w" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_help() {
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
}
_docker_compose_images() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --quiet -q" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_kill() {
case "$prev" in
-s)
COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help -s" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_logs() {
case "$prev" in
--tail)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--follow -f --help --no-color --tail --timestamps -t" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_pause() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_port() {
case "$prev" in
--protocol)
COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
return;
;;
--index)
return;
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --index --protocol" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_ps() {
local key=$(__docker_compose_map_key_of_current_option '--filter')
case "$key" in
source)
COMPREPLY=( $( compgen -W "build image" -- "${cur##*=}" ) )
return
;;
status)
COMPREPLY=( $( compgen -W "paused restarting running stopped" -- "${cur##*=}" ) )
return
;;
esac
case "$prev" in
--filter)
COMPREPLY=( $( compgen -W "source status" -S "=" -- "$cur" ) )
__docker_compose_nospace
return;
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--all -a --filter --help --quiet -q --services" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_pull() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --ignore-pull-failures --include-deps --no-parallel --quiet -q" -- "$cur" ) )
;;
*)
__docker_compose_complete_services --filter source=image
;;
esac
}
_docker_compose_push() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --ignore-push-failures" -- "$cur" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_restart() {
case "$prev" in
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_rm() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--force -f --help --stop -s -v" -- "$cur" ) )
;;
*)
if __docker_compose_has_option "--stop|-s" ; then
__docker_compose_complete_services
else
__docker_compose_complete_services --filter status=stopped
fi
;;
esac
}
_docker_compose_run() {
case "$prev" in
-e)
COMPREPLY=( $( compgen -e -- "$cur" ) )
__docker_compose_nospace
return
;;
--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" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_scale() {
case "$prev" in
=)
COMPREPLY=("$cur")
return
;;
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
COMPREPLY=( $(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur") )
__docker_compose_nospace
;;
esac
}
_docker_compose_start() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_complete_services --filter status=stopped
;;
esac
}
_docker_compose_stop() {
case "$prev" in
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_top() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_complete_running_services
;;
esac
}
_docker_compose_unpause() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_complete_services --filter status=paused
;;
esac
}
_docker_compose_up() {
case "$prev" in
=)
COMPREPLY=("$cur")
return
;;
--exit-code-from)
__docker_compose_complete_services
return
;;
--scale)
COMPREPLY=( $(compgen -S "=" -W "$(__docker_compose_services)" -- "$cur") )
__docker_compose_nospace
return
;;
--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" ) )
;;
*)
__docker_compose_complete_services
;;
esac
}
_docker_compose_version() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--short" -- "$cur" ) )
;;
esac
}
_docker_compose() {
local previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob
local commands=(
build
bundle
config
create
down
events
exec
help
images
kill
logs
pause
port
ps
pull
push
restart
rm
run
scale
start
stop
top
unpause
up
version
)
# Options for the docker daemon that have to be passed to secondary calls to
# docker-compose executed by this script.
local daemon_boolean_options="
--skip-hostname-check
--tls
--tlsverify
"
local daemon_options_with_args="
--env-file
--file -f
--host -H
--project-directory
--project-name -p
--tlscacert
--tlscert
--tlskey
"
# These options are require special treatment when searching the command.
local top_level_options_with_args="
--log-level
"
COMPREPLY=()
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
# search subcommand and invoke its handler.
# special treatment of some top-level options
local command='docker_compose'
local top_level_options=()
local counter=1
while [ $counter -lt $cword ]; do
case "${words[$counter]}" in
$(__docker_compose_to_extglob "$daemon_boolean_options") )
local opt=${words[counter]}
top_level_options+=($opt)
;;
$(__docker_compose_to_extglob "$daemon_options_with_args") )
local opt=${words[counter]}
local arg=${words[++counter]}
top_level_options+=($opt $arg)
;;
$(__docker_compose_to_extglob "$top_level_options_with_args") )
(( counter++ ))
;;
-*)
;;
*)
command="${words[$counter]}"
break
;;
esac
(( counter++ ))
done
local completions_func=_docker_compose_${command//-/_}
declare -F $completions_func >/dev/null && $completions_func
eval "$previous_extglob_setting"
return 0
}
eval "$__docker_compose_previous_extglob_setting"
unset __docker_compose_previous_extglob_setting
complete -F _docker_compose docker-compose docker-compose.exe