-
Notifications
You must be signed in to change notification settings - Fork 119
/
completion.sh
728 lines (610 loc) · 16.9 KB
/
completion.sh
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/bin/bash
# __dcos_<helper func>
# _dcos_<cli subcommand>
__dcos_debug() {
if [[ -n ${DCOS_COMP_DEBUG_FILE} ]]; then
echo "${FUNCNAME[1]}: $*" >> "${DCOS_COMP_DEBUG_FILE}"
fi
}
# Checks if an array of subcommands contains a subcommand.
# sage: __dcos_contains_subcommand <cmd> <arrays_of_subcommands>
__dcos_contains_subcommand () {
local seeking=$1; shift
local res=1
for cmd; do
if [[ $cmd == "$seeking" ]]; then
res=0
break
fi
done
return $res
}
# Since _get_comp_words_by_ref is not available in zsh
# we need a ported version that can be sourced when using zsh.
if [[ -n ${ZSH_VERSION:+set} ]] && ! type _get_comp_words_by_ref >/dev/null 2>&1; then
_get_comp_words_by_ref () {
while [ $# -gt 0 ]; do
case "$1" in
cur)
cur=${COMP_WORDS[COMP_CWORD]}
;;
prev)
prev=${COMP_WORDS[COMP_CWORD-1]}
;;
words)
words=("${COMP_WORDS[@]}")
;;
cword)
cword=$COMP_CWORD
;;
-n)
# assume COMP_WORDBREAKS is already set sanely
shift
;;
esac
shift
done
}
fi
# Default behavior to get the next command from the current command. In the case of --help and maybe other situations
# in the future that should end completion, this returns 1, otherwise 0
#
# Assumes a $command variable exists in the calling context that will contain the found subcommand
# Assumes a $c variable exists in the calling context that is the current
# word being evaluated from the input string (this is defined in __dcos_main so shouldn't be a problem)
__dcos_default_command_parse() {
while [ "$c" -lt "$cword" ]; do
i="${words[c]}"
case "$i" in
--help|-h)
# if help is a flag, cobra stops parsing immediately so we return here
return 1
;;
-*) ;;
*)
command="$i";
# $c is effectively global so that way it can crawl through the arguments as the program
# navigates the function tree and so $c will always be the next unread word
# after finding the next command, we increment $c before breaking out of the loop
((c++))
break
;;
esac
((c++))
done
return 0
}
__dcos_handle_compreply() {
__dcos_debug "completions: $*"
local i=0
for completion in "$@"; do
# filter out completions which don't match the current word
if [[ $completion == "$cur"* ]]; then
# check if the completion offered is a flag that accepts an argument which is marked by an =
# if so, a space is not manually added since the user is expected to add further input
case "$completion" in
--*=*)
COMPREPLY[i++]="$completion"
;;
*)
COMPREPLY[i++]="$completion "
;;
esac
fi
done
}
# Calls next subcommand by our calling conventions.
# TODO: argument handling can probably be done more explicitly
# This assumes the following variables exist in the calling scope and are correct:
# commands: array of subcommands this function holds
# command: the command to call into
__dcos_handle_subcommand() {
# if a command was given, check that it is an actual command and call the associated function
for cmd in "${commands[@]}"; do
if [[ $cmd == "$command" ]]; then
__dcos_debug "found subcommand: $cmd"
local subcommand next_command
# swap - in commands names for _
subcommand=${command//-/_}
__dcos_debug "calling into function ${last_command}_${subcommand}"
next_command=${last_command}_${subcommand}
last_command=$next_command
if declare -f "$next_command" > /dev/null; then
$next_command
else
__dcos_debug "${next_command} does not exist"
fi
return
fi
done
}
__dcos_source_plugin_completions() {
for dir in "$@"; do
# skip if plugin doesn't have a completion directory
if [[ -d $dir ]]; then
# shellcheck disable=SC2044
# disables warning about using find in for loops
for file in $(find "$dir/bash" -type f -iname "*.sh"); do
__dcos_debug "sourcing completions from $file"
# shellcheck disable=SC1090
# disables shellcheck warning that it can't follow this source
. "$file"
done
fi
done
}
_dcos_auth() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local commands=("list-providers" "login" "logout")
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_handle_compreply "${commands[@]}"
;;
esac
return
fi
__dcos_handle_subcommand
}
_dcos_auth_login() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=( "--help"
"--password="
"--password-file="
"--private-key="
"--provider="
"--username="
)
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_auth_list_providers() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help" "--json")
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_auth_logout() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_cluster() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local commands=("attach" "help" "list" "open" "remove" "rename" "setup")
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_handle_compreply "${commands[@]}"
;;
esac
return
fi
__dcos_handle_subcommand
}
__dcos_complete_cluster_names() {
local names=()
while IFS=$'\n' read -r line; do cluster_names+=("$line"); done < <(dcos cluster list --names 2> /dev/null)
names+=("${cluster_names[@]}")
__dcos_debug "Found cluster names and IDs" "${cluster_names[@]}"
__dcos_handle_compreply "${names[@]}"
}
_dcos_cluster_attach() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_cluster_names
;;
esac
return
fi
}
_dcos_cluster_list() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help" "--attached" "--json")
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_cluster_open() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_cluster_names
;;
esac
return
fi
}
_dcos_cluster_remove() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help" "--all" "--unavailable")
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_cluster_names
;;
esac
return
fi
}
_dcos_cluster_rename() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_cluster_names
;;
esac
return
fi
}
_dcos_cluster_setup() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help"
"--ca-certs="
"--insecure"
"--name="
"--no-check"
"--no-timeout"
"--password="
"--password-file="
"--private-key="
"--provider="
"--username="
)
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# don't support flag argument completion yet
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*);;
esac
return
fi
}
__dcos_complete_config_keys() {
local keys=()
while IFS=$'\n' read -r line; do config_keys+=("$line"); done < <(dcos config keys --quiet 2> /dev/null)
keys+=("${config_keys[@]}")
__dcos_debug "Found config keys" "${config_keys[@]}"
__dcos_handle_compreply "${keys[@]}"
}
_dcos_config() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local commands=("set" "show" "unset")
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_config_keys
;;
esac
return
fi
__dcos_handle_subcommand
}
__dcos_complete_config_keys() {
local keys=(
"core.dcos_acs_token"
"core.dcos_url"
"core.mesos_master_url"
"core.pagination"
"core.ssl_verify"
"core.timeout"
"core.ssh_user"
"core.ssh_proxy_ip"
"core.reporting"
"core.prompt_login"
"cluster.name"
"job.url"
"job.service_name"
"marathon.url"
"package.cosmos_url"
)
__dcos_handle_compreply "${keys[@]}"
}
_dcos_config_set() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_config_keys
;;
esac
return
fi
}
_dcos_config_show() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_config_keys
;;
esac
return
fi
}
_dcos_config_unset() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_complete_config_keys
;;
esac
return
fi
}
_dcos_plugin() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local commands=("add" "list" "remove")
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
__dcos_handle_compreply "${commands[@]}"
;;
esac
return
fi
__dcos_handle_subcommand
}
_dcos_plugin_add() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help" "--update")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_plugin_list() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help" "--json")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*) ;;
esac
return
fi
}
_dcos_plugin_remove() {
local i command
if ! __dcos_default_command_parse; then
return
fi
local flags=("--help")
if [ -z "$command" ]; then
case "$cur" in
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
local plugins=()
while IFS=$'\n' read -r line; do plugin_ids+=("$line"); done < <(dcos plugin list --quiet 2> /dev/null)
plugins+=("${plugin_ids[@]}")
__dcos_handle_compreply "${plugins[@]}"
;;
esac
return
fi
}
_dcos() {
local i c=1 command
if ! __dcos_default_command_parse; then
return
fi
local commands=("auth" "cluster" "config" "help" "plugin")
local flags=("--help" "--version")
local plugin_commands=()
local completion_dirs=()
while IFS=$'\n' read -r line; do plugin_commands+=("$line"); done < <(dcos plugin list --commands 2> /dev/null)
commands+=("${plugin_commands[@]}")
__dcos_debug "Found plugin commands" "${plugin_commands[@]}"
while IFS=$'\n' read -r line; do completion_dirs+=("$line"); done < <(dcos plugin list --completion-dirs 2> /dev/null)
__dcos_debug "Plugin completion directories" "${completion_dirs[@]}"
__dcos_source_plugin_completions "${completion_dirs[@]}"
# no subcommand given, complete either flags or subcommands
if [ -z "$command" ]; then
case "$cur" in
--*=*)
# TODO: don't support flag completion right now
# this does leave out the potential for detecting flags that take values but are separated like
# --flag value instead of --flag=value
# we're not worrying about flag arg completion yet though so it's safe to ignore that
return
;;
help)
return
;;
--*)
__dcos_handle_compreply "${flags[@]}"
;;
*)
# no command was given so list out possible subcommands
# in real usage, $command will also end up being the argument given to the command, not sure yet
# how we want to handle that
__dcos_handle_compreply "${commands[@]}"
;;
esac
return
fi
__dcos_handle_subcommand
}
__dcos_main() {
__dcos_debug "$@"
# shellcheck disable=SC2034
# disable unused variable warning, prev is unused here
local cur prev words cword last_command
# give last_command the root prefix
last_command="_dcos"
COMPREPLY=()
# this function exists in the bash-completion package which I believe must
# be installed separately from bash
_get_comp_words_by_ref -n "=:" cur prev words cword
__dcos_debug "Starting completion on '$cur' from ${words[*]}"
_dcos
}
complete -o bashdefault -o default -o nospace -F __dcos_main dcos