_docker 6.44 KB
Newer Older
Ahmed Azaan's avatar
Ahmed Azaan committed
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
#compdef docker

# Docker autocompletion for oh-my-zsh
# Requires: Docker installed
# Author : Azaan (@aeonazaan)


# ----- Helper functions
# Output a selectable list of all running docker containers
__docker_containers() {
    declare -a cont_cmd
    cont_cmd=($(docker ps | awk 'NR>1{print $1":[CON("$1")"$2"("$3")]"}'))
    _describe 'containers' cont_cmd
}

# output a selectable list of all docker images
__docker_images() {
    declare -a img_cmd
    img_cmd=($(docker images | awk 'NR>1{print $1}'))
    _describe 'images' img_cmd
}

# ----- Commands
# Seperate function for each command, makes extension easier later
# ---------------------------
__attach() {
    __docker_containers
}

__build() {
    _arguments \
        '-q=false[Suppress verbose build output]' \
        '-t="[fuck to be applied to the resulting image in case of success]' \
Ahmed Azaan's avatar
Ahmed Azaan committed
34
        '*:files:_files'
Ahmed Azaan's avatar
Ahmed Azaan committed
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
}

__commit() {
    _arguments \
        '-author="[Author]' \
        '-m="[Commit message]' \
        '-run="[Config automatically applied when the image is run.\n]'
    __docker_containers
}

__diff() {
    __docker_containers
}

__export() {
   __docker_containers
}


__history() {
    __docker_images
}

__images() {
    _arguments \
        '-a[show all images]' \
        '-notrunc[dont truncate output]' \
        '-q[only show numeric IDs]' \
Ahmed Azaan's avatar
Ahmed Azaan committed
63
        '-viz[output graph in graphviz format]'
Ahmed Azaan's avatar
Ahmed Azaan committed
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
    __docker_images
}

__import() {
    _arguments '*:files:_files'
}

__info() {
    # no arguments
}

__insert() {
    __docker_images
    _arguments '*:files:_files'
}

__inspect() {
    __docker_images
    __docker_containers
}

__kill() {
    __docker_containers
}

__login() {
    _arguments \
        '-e="[email]' \
        '-p="[password]' \
        '-u="[username]' \
}

__logs() {
    __docker_containers
}

__port() {
    __docker_containers
}

__top() {
    __docker_containers
}

__ps() {
    _arguments \
        '-a[Show all containers. Only running containers are shown by default.]' \
        '-beforeId="[Show only container created before Id, include non-running ones.]' \
        '-l[Show only the latest created container, include non-running ones.]' \
        '-n=[Show n last created containers, include non-running ones.]' \
        '-notrurrrrnc[Dont truncate output]' \
        '-q[Only display numeric IDs]' \
        '-s[Display sizes]' \
        '-sinceId="[Show only containers created since Id, include non-running ones.]'
}

__pull() {
    _arguments '-t="[Download tagged image in repository]'
}

__push() {

}

__restart() {
    _arguments '-t=[number of seconds to try to stop before killing]'
    __docker_containers
}

__rm() {
    _arguments '-v[Remove the volumes associated to the container]'
    __docker_containers
}

__rmi() {
    __docker_images
}

__run() {
    _arguments \
        '-a=[Attach to stdin, stdout or stderr.]' \
        '-c=[CPU shares (relative weight)]' \
        '-d[Detached mode: leave the container running in the background]' \
        '-dns=[Set custom dns servers]' \
        '-e=[Set environment variables]' \
        '-entrypoint="[Overwrite the default entrypoint of the image]' \
        '-h="[Container host name]' \
        '-i[Keep stdin open even if not attached]' \
        '-m=[Memory limit (in bytes)]' \
        '-p=[Expose a containers port to the host (use docker port to see the actual mapping)]' \
        '-t[Allocate a pseudo-tty]' \
        '-u="[Username or UID]' \
        '-v=[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]' \
        '-volumes-from="[Mount volumes from the specified container]'
    __docker_images
}

__search() {
Ahmed Azaan's avatar
Ahmed Azaan committed
162
    _arguments '-notrunc[Dont truncate output]'
Ahmed Azaan's avatar
Ahmed Azaan committed
163
164
165
}

__start() {
Ahmed Azaan's avatar
Ahmed Azaan committed
166
    __docker_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
167
168
169
}

__stop() {
Ahmed Azaan's avatar
Ahmed Azaan committed
170
    _arguments '-t=[number of seconds to try to stop before killing]'
Ahmed Azaan's avatar
Ahmed Azaan committed
171
172
173
174
    __docker_containers
}

__tag() {
Ahmed Azaan's avatar
Ahmed Azaan committed
175
    _arguments '-f[Force]'
Ahmed Azaan's avatar
Ahmed Azaan committed
176
177
178
179
180
181
182
183
    __docker_images
}

__version() {

}

__wait() {
Ahmed Azaan's avatar
Ahmed Azaan committed
184
    __docker_containers
Ahmed Azaan's avatar
Ahmed Azaan committed
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
}

# end commands ---------
# ----------------------

local -a _1st_arguments
_1st_arguments=(
    "attach":"Attach to a running container"
    "build":"Build a container from a Dockerfile"
    "commit":"Create a new image from a container's changes"
    "diff":"Inspect changes on a container's filesystem"
    "export":"Stream the contents of a container as a tar archive"
    "history":"Show the history of an image"
    "images":"List images"
    "import":"Create a new filesystem image from the contents of a tarball"
    "info":"Display system-wide information"
    "insert":"Insert a file in an image"
    "inspect":"Return low-level information on a container"
    "kill":"Kill a running container"
    "login":"Register or Login to the docker registry server"
    "logs":"Fetch the logs of a container"
    "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
    "top":"Lookup the running processes of a container"
    "ps":"List containers"
    "pull":"Pull an image or a repository from the docker registry server"
    "push":"Push an image or a repository to the docker registry server"
    "restart":"Restart a running container"
    "rm":"Remove one or more containers"
    "rmi":"Remove one or more images"
    "run":"Run a command in a new container"
    "search":"Search for an image in the docker index"
    "start":"Start a stopped container"
    "stop":"Stop a running container"
    "tag":"Tag an image into a repository"
    "version":"Show the docker version information"
    "wait":"Block until a container stops, then print its exit code"
)

_arguments '*:: :->command'

if (( CURRENT == 1 )); then
    _describe -t commands "docker command" _1st_arguments
    return
fi

local -a _command_args
case "$words[1]" in
    attach)
       __docker_containers ;;
    build)
        __build ;;
    commit)
        __commit ;;
    diff)
        __diff ;;
    export)
        __export ;;
    history)
        __history ;;
    images)
        __images ;;
    import)
        __import ;;
    info)
        __info ;;
    insert)
        __insert ;;
    inspect)
        __inspect ;;
    kill)
        __kill ;;
    login)
        __login ;;
    logs)
        __logs ;;
    port)
        __port ;;
    top)
        __top ;;
    ps)
        __ps ;;
    pull)
        __pull ;;
    push)
        __push ;;
    restart)
        __restart ;;
    rm)
        __rm ;;
    rmi)
        __rmi ;;
    run)
        __run ;;
    search)
        __search ;;
    start)
        __start ;;
    stop)
        __stop ;;
    tag)
        __tag ;;
    version)
        __version ;;
    wait)
        __wait ;;
esac