zsh: update zsh completion for docker command
| ... | ... |
@@ -5,7 +5,7 @@ |
| 5 | 5 |
# version: 0.3.0 |
| 6 | 6 |
# github: https://github.com/felixr/docker-zsh-completion |
| 7 | 7 |
# |
| 8 |
-# contributers: |
|
| 8 |
+# contributors: |
|
| 9 | 9 |
# - Felix Riedel |
| 10 | 10 |
# - Vincent Bernat |
| 11 | 11 |
# |
| ... | ... |
@@ -37,65 +37,86 @@ |
| 37 | 37 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | 38 |
# |
| 39 | 39 |
|
| 40 |
-__parse_docker_list() {
|
|
| 41 |
- awk ' |
|
| 42 |
-NR == 1 {
|
|
| 43 |
- idx=1;i=0;f[i]=0 |
|
| 44 |
- header=$0 |
|
| 45 |
- while ( match(header, / ([A-Z]+|[A-Z]+ [A-Z]+)/) ) {
|
|
| 46 |
- idx += RSTART+1 |
|
| 47 |
- f[++i]=idx |
|
| 48 |
- header = substr($0,idx) |
|
| 49 |
- } |
|
| 50 |
- f[++i]=999 |
|
| 51 |
-} |
|
| 40 |
+__docker_get_containers() {
|
|
| 41 |
+ local kind expl |
|
| 42 |
+ declare -a running stopped lines args |
|
| 52 | 43 |
|
| 53 |
-NR > 1 '"$1"' {
|
|
| 54 |
- for(j=0;j<i;j++) {
|
|
| 55 |
- x[j] = substr($0, f[j], f[j+1]-f[j]-1) |
|
| 56 |
- gsub(/[ ]+$/, "", x[j]) |
|
| 57 |
- } |
|
| 58 |
- printf("%s:%7s, %s\n", x[0], x[3], x[1])
|
|
| 59 |
- if (x[6] != "") {
|
|
| 60 |
- split(x[6], names, /,/) |
|
| 61 |
- for (name in names) printf("%s:%7s, %s\n", names[name], x[3], x[1])
|
|
| 44 |
+ kind=$1 |
|
| 45 |
+ shift |
|
| 46 |
+ [[ $kind = (stopped|all) ]] && args=($args -a) |
|
| 47 |
+ |
|
| 48 |
+ lines=(${(f)"$(_call_program commands docker ps ${args})"})
|
|
| 49 |
+ |
|
| 50 |
+ # Parse header line to find columns |
|
| 51 |
+ local i=1 j=1 k header=${lines[1]}
|
|
| 52 |
+ declare -A begin end |
|
| 53 |
+ while (( $j < ${#header} - 1 )) {
|
|
| 54 |
+ i=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 1))
|
|
| 55 |
+ j=$(( $i + ${${header[$i,-1]}[(i) ]} - 1))
|
|
| 56 |
+ k=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 2))
|
|
| 57 |
+ begin[${header[$i,$(($j-1))]}]=$i
|
|
| 58 |
+ end[${header[$i,$(($j-1))]}]=$k
|
|
| 62 | 59 |
} |
| 63 |
-} |
|
| 64 |
-'| sed -e 's/ \([hdwm]\)\(inutes\|ays\|ours\|eeks\)/\1/' |
|
| 60 |
+ lines=(${lines[2,-1]})
|
|
| 61 |
+ |
|
| 62 |
+ # Container ID |
|
| 63 |
+ local line |
|
| 64 |
+ local s |
|
| 65 |
+ for line in $lines; do |
|
| 66 |
+ s="${line[${begin[CONTAINER ID]},${end[CONTAINER ID]}]%% ##}"
|
|
| 67 |
+ s="$s:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
|
|
| 68 |
+ s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}"
|
|
| 69 |
+ if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then
|
|
| 70 |
+ stopped=($stopped $s) |
|
| 71 |
+ else |
|
| 72 |
+ running=($running $s) |
|
| 73 |
+ fi |
|
| 74 |
+ done |
|
| 75 |
+ |
|
| 76 |
+ # Names |
|
| 77 |
+ local name |
|
| 78 |
+ local -a names |
|
| 79 |
+ for line in $lines; do |
|
| 80 |
+ names=(${(ps:,:)${${line[${begin[NAMES]},-1]}%% *}})
|
|
| 81 |
+ for name in $names; do |
|
| 82 |
+ s="${name}:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
|
|
| 83 |
+ s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}"
|
|
| 84 |
+ if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then
|
|
| 85 |
+ stopped=($stopped $s) |
|
| 86 |
+ else |
|
| 87 |
+ running=($running $s) |
|
| 88 |
+ fi |
|
| 89 |
+ done |
|
| 90 |
+ done |
|
| 91 |
+ |
|
| 92 |
+ [[ $kind = (running|all) ]] && _describe -t containers-running "running containers" running |
|
| 93 |
+ [[ $kind = (stopped|all) ]] && _describe -t containers-stopped "stopped containers" stopped |
|
| 65 | 94 |
} |
| 66 | 95 |
|
| 67 | 96 |
__docker_stoppedcontainers() {
|
| 68 |
- local expl |
|
| 69 |
- declare -a stoppedcontainers |
|
| 70 |
- stoppedcontainers=(${(f)"$(_call_program commands docker ps -a | __parse_docker_list '&& / Exit/')"})
|
|
| 71 |
- _describe -t containers-stopped "Stopped Containers" stoppedcontainers "$@" |
|
| 97 |
+ __docker_get_containers stopped "$@" |
|
| 72 | 98 |
} |
| 73 | 99 |
|
| 74 | 100 |
__docker_runningcontainers() {
|
| 75 |
- local expl |
|
| 76 |
- declare -a containers |
|
| 77 |
- |
|
| 78 |
- containers=(${(f)"$(_call_program commands docker ps | __parse_docker_list)"})
|
|
| 79 |
- _describe -t containers-active "Running Containers" containers "$@" |
|
| 101 |
+ __docker_get_containers running "$@" |
|
| 80 | 102 |
} |
| 81 | 103 |
|
| 82 | 104 |
__docker_containers () {
|
| 83 |
- __docker_stoppedcontainers "$@" |
|
| 84 |
- __docker_runningcontainers "$@" |
|
| 105 |
+ __docker_get_containers all "$@" |
|
| 85 | 106 |
} |
| 86 | 107 |
|
| 87 | 108 |
__docker_images () {
|
| 88 | 109 |
local expl |
| 89 | 110 |
declare -a images |
| 90 |
- images=(${(f)"$(_call_program commands docker images | awk '(NR > 1 && $1 != "<none>"){printf("%s", $1);if ($2 != "<none>") printf("\\:%s", $2); printf("\n")}')"})
|
|
| 91 |
- images=($images ${(f)"$(_call_program commands docker images | awk '(NR > 1){printf("%s:%-15s in %s\n", $3,$2,$1)}')"})
|
|
| 92 |
- _describe -t docker-images "Images" images |
|
| 111 |
+ images=(${${${${(f)"$(_call_program commands docker images)"}[2,-1]}/ ##/\\:}%% *})
|
|
| 112 |
+ images=(${${images%\\:<none>}#<none>} ${${${(f)"$(_call_program commands docker images)"}[2,-1]}/(#b)([^ ]##) ##([^ ]##) ##([^ ]##)*/${match[3]}:${(r:15:: :::)match[2]} in ${match[1]}})
|
|
| 113 |
+ _describe -t docker-images "images" images |
|
| 93 | 114 |
} |
| 94 | 115 |
|
| 95 | 116 |
__docker_tags() {
|
| 96 | 117 |
local expl |
| 97 | 118 |
declare -a tags |
| 98 |
- tags=(${(f)"$(_call_program commands docker images | awk '(NR>1){print $2}'| sort | uniq)"})
|
|
| 119 |
+ tags=(${${${${${(f)"$(_call_program commands docker images)"}#* }## #}%% *}[2,-1]})
|
|
| 99 | 120 |
_describe -t docker-tags "tags" tags |
| 100 | 121 |
} |
| 101 | 122 |
|
| ... | ... |
@@ -124,16 +145,15 @@ __docker_search() {
|
| 124 | 124 |
if ( [[ ${(P)+cachename} -eq 0 ]] || _cache_invalid ${cachename#_} ) \
|
| 125 | 125 |
&& ! _retrieve_cache ${cachename#_}; then
|
| 126 | 126 |
_message "Searching for ${searchterm}..."
|
| 127 |
- result=(${(f)"$(_call_program commands docker search ${searchterm} | awk '(NR>2){print $1}')"})
|
|
| 127 |
+ result=(${${${(f)"$(_call_program commands docker search ${searchterm})"}%% *}[2,-1]})
|
|
| 128 | 128 |
_store_cache ${cachename#_} result
|
| 129 | 129 |
fi |
| 130 |
- _wanted dockersearch expl 'Available images' compadd -a result |
|
| 130 |
+ _wanted dockersearch expl 'available images' compadd -a result |
|
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 | 133 |
__docker_caching_policy() |
| 134 | 134 |
{
|
| 135 |
- # oldp=( "$1"(Nmh+24) ) # 24 hour |
|
| 136 |
- oldp=( "$1"(Nmh+1) ) # 24 hour |
|
| 135 |
+ oldp=( "$1"(Nmh+1) ) # 1 hour |
|
| 137 | 136 |
(( $#oldp )) |
| 138 | 137 |
} |
| 139 | 138 |
|
| ... | ... |
@@ -141,8 +161,8 @@ __docker_caching_policy() |
| 141 | 141 |
__docker_repositories () {
|
| 142 | 142 |
local expl |
| 143 | 143 |
declare -a repos |
| 144 |
- repos=(${(f)"$(_call_program commands docker images | sed -e '1d' -e 's/[ ].*//' | sort | uniq)"})
|
|
| 145 |
- _describe -t docker-repos "Repositories" repos "$@" |
|
| 144 |
+ repos=(${${${(f)"$(_call_program commands docker images)"}%% *}[2,-1]})
|
|
| 145 |
+ _describe -t docker-repos "repositories" repos "$@" |
|
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
__docker_commands () {
|
| ... | ... |
@@ -157,8 +177,7 @@ __docker_commands () {
|
| 157 | 157 |
if ( [[ ${+_docker_subcommands} -eq 0 ]] || _cache_invalid docker_subcommands) \
|
| 158 | 158 |
&& ! _retrieve_cache docker_subcommands; |
| 159 | 159 |
then |
| 160 |
- _docker_subcommands=(${${(f)"$(_call_program commands
|
|
| 161 |
- docker 2>&1 | sed -e '1,6d' -e '/^[ ]*$/d' -e 's/[ ]*\([^ ]\+\)\s*\([^ ].*\)/\1:\2/' )"}}) |
|
| 160 |
+ _docker_subcommands=(${${${${(f)"$(_call_program commands docker 2>&1)"}[5,-1]}## #}/ ##/:})
|
|
| 162 | 161 |
_docker_subcommands=($_docker_subcommands 'help:Show help for a command') |
| 163 | 162 |
_store_cache docker_subcommands _docker_subcommands |
| 164 | 163 |
fi |
| ... | ... |
@@ -180,13 +199,13 @@ __docker_subcommand () {
|
| 180 | 180 |
'--no-cache[Do not use cache when building the image]' \ |
| 181 | 181 |
'-q[Suppress verbose build output]' \ |
| 182 | 182 |
'--rm[Remove intermediate containers after a successful build]' \ |
| 183 |
- '-t=-:repository:__docker_repositories_with_tags' \ |
|
| 183 |
+ '-t:repository:__docker_repositories_with_tags' \ |
|
| 184 | 184 |
':path or URL:_directories' |
| 185 | 185 |
;; |
| 186 | 186 |
(commit) |
| 187 | 187 |
_arguments \ |
| 188 | 188 |
'--author=-[Author]:author: ' \ |
| 189 |
- '-m=-[Commit message]:message: ' \ |
|
| 189 |
+ '-m[Commit message]:message: ' \ |
|
| 190 | 190 |
'--run=-[Configuration automatically applied when the image is run]:configuration: ' \ |
| 191 | 191 |
':container:__docker_containers' \ |
| 192 | 192 |
':repository:__docker_repositories_with_tags' |
| ... | ... |
@@ -252,9 +271,9 @@ __docker_subcommand () {
|
| 252 | 252 |
;; |
| 253 | 253 |
(login) |
| 254 | 254 |
_arguments \ |
| 255 |
- '-e=-[Email]:email: ' \ |
|
| 256 |
- '-p=-[Password]:password: ' \ |
|
| 257 |
- '-u=-[Username]:username: ' \ |
|
| 255 |
+ '-e[Email]:email: ' \ |
|
| 256 |
+ '-p[Password]:password: ' \ |
|
| 257 |
+ '-u[Username]:username: ' \ |
|
| 258 | 258 |
':server: ' |
| 259 | 259 |
;; |
| 260 | 260 |
(logs) |
| ... | ... |
@@ -284,7 +303,7 @@ __docker_subcommand () {
|
| 284 | 284 |
'*:images:__docker_images' |
| 285 | 285 |
;; |
| 286 | 286 |
(restart|stop) |
| 287 |
- _arguments '-t=-[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \ |
|
| 287 |
+ _arguments '-t[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \ |
|
| 288 | 288 |
'*:containers:__docker_runningcontainers' |
| 289 | 289 |
;; |
| 290 | 290 |
(top) |
| ... | ... |
@@ -303,7 +322,7 @@ __docker_subcommand () {
|
| 303 | 303 |
'-a[Show all containers]' \ |
| 304 | 304 |
'--before=-[Show only container created before...]:containers:__docker_containers' \ |
| 305 | 305 |
'-l[Show only the latest created container]' \ |
| 306 |
- '-n=-[Show n last created containers, include non-running one]:n:(1 5 10 25 50)' \ |
|
| 306 |
+ '-n[Show n last created containers, include non-running one]:n:(1 5 10 25 50)' \ |
|
| 307 | 307 |
'--no-trunc[Do not truncate output]' \ |
| 308 | 308 |
'-q[Only show numeric IDs]' \ |
| 309 | 309 |
'-s[Display sizes]' \ |
| ... | ... |
@@ -319,28 +338,28 @@ __docker_subcommand () {
|
| 319 | 319 |
_arguments \ |
| 320 | 320 |
'-P[Publish all exposed ports to the host]' \ |
| 321 | 321 |
'-a[Attach to stdin, stdout or stderr]' \ |
| 322 |
- '-c=-[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)' \ |
|
| 322 |
+ '-c[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)' \ |
|
| 323 | 323 |
'--cidfile=-[Write the container ID to the file]:CID file:_files' \ |
| 324 | 324 |
'-d[Detached mode: leave the container running in the background]' \ |
| 325 | 325 |
'*--dns=-[Set custom dns servers]:dns server: ' \ |
| 326 |
- '*-e=-[Set environment variables]:environment variable: ' \ |
|
| 326 |
+ '*-e[Set environment variables]:environment variable: ' \ |
|
| 327 | 327 |
'--entrypoint=-[Overwrite the default entrypoint of the image]:entry point: ' \ |
| 328 | 328 |
'*--expose=-[Expose a port from the container without publishing it]: ' \ |
| 329 |
- '-h=-[Container host name]:hostname:_hosts' \ |
|
| 329 |
+ '-h[Container host name]:hostname:_hosts' \ |
|
| 330 | 330 |
'-i[Keep stdin open even if not attached]' \ |
| 331 | 331 |
'--link=-[Add link to another container]:link:->link' \ |
| 332 | 332 |
'--lxc-conf=-[Add custom lxc options]:lxc options: ' \ |
| 333 |
- '-m=-[Memory limit (in bytes)]:limit: ' \ |
|
| 333 |
+ '-m[Memory limit (in bytes)]:limit: ' \ |
|
| 334 | 334 |
'--name=-[Container name]:name: ' \ |
| 335 |
- '*-p=-[Expose a container'"'"'s port to the host]:port:_ports' \ |
|
| 335 |
+ '*-p[Expose a container'"'"'s port to the host]:port:_ports' \ |
|
| 336 | 336 |
'--privileged[Give extended privileges to this container]' \ |
| 337 | 337 |
'--rm[Remove intermediate containers when it exits]' \ |
| 338 | 338 |
'--sig-proxy[Proxify all received signal]' \ |
| 339 | 339 |
'-t[Allocate a pseudo-tty]' \ |
| 340 |
- '-u=-[Username or UID]:user:_users' \ |
|
| 341 |
- '*-v=-[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]:volume: '\ |
|
| 340 |
+ '-u[Username or UID]:user:_users' \ |
|
| 341 |
+ '*-v[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]:volume: '\ |
|
| 342 | 342 |
'--volumes-from=-[Mount volumes from the specified container]:volume: ' \ |
| 343 |
- '-w=-[Working directory inside the container]:directory:_directories' \ |
|
| 343 |
+ '-w[Working directory inside the container]:directory:_directories' \ |
|
| 344 | 344 |
'(-):images:__docker_images' \ |
| 345 | 345 |
'(-):command: _command_names -e' \ |
| 346 | 346 |
'*::arguments: _normal' |
| ... | ... |
@@ -360,7 +379,7 @@ __docker_subcommand () {
|
| 360 | 360 |
_arguments ':name:__docker_search' |
| 361 | 361 |
;; |
| 362 | 362 |
(push) |
| 363 |
- _arguments ':repository:__docker_repositories_with_tags' |
|
| 363 |
+ _arguments ':images:__docker_images' |
|
| 364 | 364 |
;; |
| 365 | 365 |
(save) |
| 366 | 366 |
_arguments \ |
| ... | ... |
@@ -390,7 +409,7 @@ _docker () {
|
| 390 | 390 |
typeset -A opt_args |
| 391 | 391 |
|
| 392 | 392 |
_arguments -C \ |
| 393 |
- '-H=-[tcp://host:port to bind/connect to]:socket: ' \ |
|
| 393 |
+ '-H[tcp://host:port to bind/connect to]:socket: ' \ |
|
| 394 | 394 |
'(-): :->command' \ |
| 395 | 395 |
'(-)*:: :->option-or-argument' |
| 396 | 396 |
|
| ... | ... |
@@ -409,3 +428,11 @@ _docker () {
|
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
_docker "$@" |
| 412 |
+ |
|
| 413 |
+# Local Variables: |
|
| 414 |
+# mode: Shell-Script |
|
| 415 |
+# sh-indentation: 4 |
|
| 416 |
+# indent-tabs-mode: nil |
|
| 417 |
+# sh-basic-offset: 4 |
|
| 418 |
+# End: |
|
| 419 |
+# vim: ft=zsh sw=4 ts=4 et |