Sfoglia il codice sorgente

Merge pull request #16934 from sdurrheimer/zsh-completion-docker-network

Add zsh completion for 'docker network'
Jess Frazelle 9 anni fa
parent
commit
6bacb32755
1 ha cambiato i file con 113 aggiunte e 3 eliminazioni
  1. 113 3
      contrib/completion/zsh/_docker

+ 113 - 3
contrib/completion/zsh/_docker

@@ -183,6 +183,99 @@ __docker_search() {
     _wanted dockersearch expl 'available images' compadd -a result
 }
 
+__docker_networks() {
+    [[ $PREFIX = -* ]] && return 1
+    integer ret=1
+    declare -a lines networks
+
+    lines=(${(f)"$(_call_program commands docker $docker_options network ls)"})
+
+    # Parse header line to find columns
+    local i=1 j=1 k header=${lines[1]}
+    declare -A begin end
+    while (( j < ${#header} - 1 )); do
+        i=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 1 ))
+        j=$(( i + ${${header[$i,-1]}[(i)  ]} - 1 ))
+        k=$(( j + ${${header[$j,-1]}[(i)[^ ]]} - 2 ))
+        begin[${header[$i,$((j-1))]}]=$i
+        end[${header[$i,$((j-1))]}]=$k
+    done
+    end[${header[$i,$((j-1))]}]=-1
+    lines=(${lines[2,-1]})
+
+    # Network ID
+    local line s
+    for line in $lines; do
+        s="${line[${begin[NETWORK ID]},${end[NETWORK ID]}]%% ##}"
+        s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
+        networks=($networks $s)
+    done
+
+    # Names
+    for line in $lines; do
+        s="${line[${begin[NAME]},${end[NAME]}]%% ##}"
+        s="$s:${(l:7:: :::)${${line[${begin[DRIVER]},${end[DRIVER]}]}%% ##}}"
+        networks=($networks $s)
+    done
+
+    _describe -t networks-list "networks" networks && ret=0
+    return ret
+}
+
+__docker_network_commands() {
+    local -a _docker_network_subcommands
+    _docker_network_subcommands=(
+        "connect:onnects a container to a network"
+        "create:Creates a new network with a name specified by the user"
+        "disconnect:Disconnects a container from a network"
+        "inspect:Displays detailed information on a network"
+        "ls:Lists all the networks created by the user"
+        "rm:Deletes a network"
+    )
+    _describe -t docker-network-commands "docker network command" _docker_network_subcommands
+}
+
+__docker_network_subcommand() {
+    local -a _command_args opts_help
+    local expl help="--help"
+    integer ret=1
+
+    opts_help=("(: -)--help[Print usage]")
+
+    case "$words[1]" in
+        (connect|disconnect)
+            _arguments \
+                $opts_help \
+                "($help -)1:network:__docker_networks" \
+                "($help -)2:containers:__docker_runningcontainers" && ret=0
+            ;;
+        (create)
+            _arguments -A '-*' \
+                $opts_help \
+                "($help -d --driver)"{-d,--driver=}"[Driver to manage the Network]:driver:(null host bridge overlay)" \
+                "($help -)1:Network Name: " && ret=0
+            ;;
+        (inspect|rm)
+            _arguments \
+                $opts_help \
+                "($help -):network:__docker_networks" && ret=0
+            ;;
+        (ls)
+            _arguments \
+                $opts_help \
+                "($help -l --latest)"{-l,--latest}"[Show the latest network created]" \
+                "($help)-n=-[Show n last created networks]:Number of networks: " \
+                "($help)--no-trunc[Do not truncate the output]" \
+                "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
+            ;;
+        (help)
+            _arguments ":subcommand:__docker_network_commands" && ret=0
+            ;;
+    esac
+
+    return ret
+}
+
 __docker_volumes() {
     [[ $PREFIX = -* ]] && return 1
     integer ret=1
@@ -244,7 +337,7 @@ __docker_volume_subcommand() {
         (inspect)
             _arguments \
                 $opts_help \
-                "($help -f --format=-)"{-f,--format=-}"[Format the output using the given go template]:template: " \
+                "($help -f --format)"{-f,--format=-}"[Format the output using the given go template]:template: " \
                 "($help -)1:volume:__docker_volumes" && ret=0
             ;;
         (ls)
@@ -335,7 +428,7 @@ __docker_subcommand() {
         "($help)*--lxc-conf=-[Add custom lxc options]:lxc options: "
         "($help)--mac-address=-[Container MAC address]:MAC address: "
         "($help)--name=-[Container name]:name: "
-        "($help)--net=-[Network mode]:network mode:(bridge none container host)"
+        "($help)--net=-[Connect a container to a network]:network mode:(bridge none container host)"
         "($help)--oom-kill-disable[Disable OOM Killer]"
         "($help -P --publish-all)"{-P,--publish-all}"[Publish all exposed ports]"
         "($help)*"{-p,--publish=-}"[Expose a container's port to the host]:port:_ports"
@@ -605,6 +698,23 @@ __docker_subcommand() {
                 "($help)--tail=-[Output the last K lines]:lines:(1 10 20 50 all)" \
                 "($help -)*:containers:__docker_containers" && ret=0
             ;;
+        (network)
+            local curcontext="$curcontext" state
+            _arguments \
+                $opts_help \
+                "($help -): :->command" \
+                "($help -)*:: :->option-or-argument" && ret=0
+
+            case $state in
+                (command)
+                    __docker_network_commands && ret=0
+                    ;;
+                (option-or-argument)
+                    curcontext=${curcontext%:*:*}:docker-${words[-1]}:
+                    __docker_network_subcommand && ret=0
+                    ;;
+            esac
+            ;;
         (pause|unpause)
             _arguments \
                 $opts_help \
@@ -750,7 +860,7 @@ __docker_subcommand() {
                     __docker_volume_commands && ret=0
                     ;;
                 (option-or-argument)
-                    curcontext=${curcontext%:*:*}:docker-$words[-1]:
+                    curcontext=${curcontext%:*:*}:docker-${words[-1]}:
                     __docker_volume_subcommand && ret=0
                     ;;
             esac