From d7c9ae60e646e827e741c2be4646d0b867cdc207 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sun, 15 Nov 2015 13:31:13 -0800 Subject: [PATCH] Improve bash completion for `docker network disconnect` Signed-off-by: Harald Albers --- contrib/completion/bash/docker | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index 1d3b986e04..7210e42cb3 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -157,6 +157,11 @@ __docker_networks() { COMPREPLY=( $(compgen -W "$networks" -- "$cur") ) } +__docker_containers_in_network() { + local containers=$(__docker_q network inspect -f '{{range $i, $c := .Containers}}{{$i}} {{$c.Name}} {{end}}' "$1") + COMPREPLY=( $(compgen -W "$containers" -- "$cur") ) +} + __docker_volumes() { COMPREPLY=( $(compgen -W "$(__docker_q volume ls -q)" -- "$cur") ) } @@ -1092,7 +1097,7 @@ _docker_network_connect() { COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) ;; *) - local counter=$(__docker_pos_first_nonflag '--tail') + local counter=$(__docker_pos_first_nonflag) if [ $cword -eq $counter ]; then __docker_networks elif [ $cword -eq $(($counter + 1)) ]; then @@ -1127,9 +1132,19 @@ _docker_network_create() { } _docker_network_disconnect() { - # TODO disconnect should only complete running containers connected - # to the specified network. - _docker_network_connect + case "$cur" in + -*) + COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) + ;; + *) + local counter=$(__docker_pos_first_nonflag) + if [ $cword -eq $counter ]; then + __docker_networks + elif [ $cword -eq $(($counter + 1)) ]; then + __docker_containers_in_network "$prev" + fi + ;; + esac } _docker_network_inspect() {