Browse code

Add zsh completion for 'docker network ls -f {id,name,type}'

Signed-off-by: Steve Durrheimer <s.durrheimer@gmail.com>

Steve Durrheimer authored on 2016/04/30 05:04:50
Showing 1 changed files
... ...
@@ -311,11 +311,42 @@ __docker_complete_ps_filters() {
311 311
     return ret
312 312
 }
313 313
 
314
-__docker_networks() {
314
+__docker_network_complete_ls_filters() {
315
+    [[ $PREFIX = -* ]] && return 1
316
+    integer ret=1
317
+
318
+    if compset -P '*='; then
319
+        case "${${words[-1]%=*}#*=}" in
320
+            (id)
321
+                __docker_networks_ids && ret=0
322
+                ;;
323
+            (name)
324
+                __docker_networks_names && ret=0
325
+                ;;
326
+            (type)
327
+                type_opts=('builtin' 'custom')
328
+                _describe -t type-filter-opts "Type Filter Options" type_opts && ret=0
329
+                ;;
330
+            *)
331
+                _message 'value' && ret=0
332
+                ;;
333
+        esac
334
+    else
335
+        opts=('id' 'name' 'type')
336
+        _describe -t filter-opts "Filter Options" opts -qS "=" && ret=0
337
+    fi
338
+
339
+    return ret
340
+}
341
+
342
+__docker_get_networks() {
315 343
     [[ $PREFIX = -* ]] && return 1
316 344
     integer ret=1
345
+    local line s
317 346
     declare -a lines networks
318 347
 
348
+    type=$1; shift
349
+
319 350
     lines=(${(f)"$(_call_program commands docker $docker_options network ls)"})
320 351
 
321 352
     # Parse header line to find columns
... ...
@@ -332,24 +363,42 @@ __docker_networks() {
332 332
     lines=(${lines[2,-1]})
333 333
 
334 334
     # Network ID
335
-    local line s
336
-    for line in $lines; do
337
-        s="${line[${begin[NETWORK ID]},${end[NETWORK ID]}]%% ##}"
338
-        s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
339
-        networks=($networks $s)
340
-    done
335
+    if [[ $type = (ids|all) ]]; then
336
+        for line in $lines; do
337
+            s="${line[${begin[NETWORK ID]},${end[NETWORK ID]}]%% ##}"
338
+            s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
339
+            networks=($networks $s)
340
+        done
341
+    fi
341 342
 
342 343
     # Names
343
-    for line in $lines; do
344
-        s="${line[${begin[NAME]},${end[NAME]}]%% ##}"
345
-        s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
346
-        networks=($networks $s)
347
-    done
344
+    if [[ $type = (names|all) ]]; then
345
+        for line in $lines; do
346
+            s="${line[${begin[NAME]},${end[NAME]}]%% ##}"
347
+            s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
348
+            networks=($networks $s)
349
+        done
350
+    fi
348 351
 
349
-    _describe -t networks-list "networks" networks && ret=0
352
+    _describe -t networks-list "networks" networks "$@" && ret=0
350 353
     return ret
351 354
 }
352 355
 
356
+__docker_networks() {
357
+    [[ $PREFIX = -* ]] && return 1
358
+    __docker_get_networks all "$@"
359
+}
360
+
361
+__docker_networks_ids() {
362
+    [[ $PREFIX = -* ]] && return 1
363
+    __docker_get_networks ids "$@"
364
+}
365
+
366
+__docker_networks_names() {
367
+    [[ $PREFIX = -* ]] && return 1
368
+    __docker_get_networks names "$@"
369
+}
370
+
353 371
 __docker_network_commands() {
354 372
     local -a _docker_network_subcommands
355 373
     _docker_network_subcommands=(
... ...
@@ -423,7 +472,13 @@ __docker_network_subcommand() {
423 423
             _arguments $(__docker_arguments) \
424 424
                 $opts_help \
425 425
                 "($help)--no-trunc[Do not truncate the output]" \
426
+                "($help)*"{-f=,--filter=}"[Provide filter values]:filter:->filter-options" \
426 427
                 "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
428
+            case $state in
429
+                (filter-options)
430
+                    __docker_network_complete_ls_filters && ret=0
431
+                    ;;
432
+            esac
427 433
             ;;
428 434
         (rm)
429 435
             _arguments $(__docker_arguments) \