forked from Silex/docker.el
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-container.el
More file actions
389 lines (337 loc) · 15.1 KB
/
docker-container.el
File metadata and controls
389 lines (337 loc) · 15.1 KB
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
34
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
63
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
;;; docker-container.el --- Emacs interface to docker-container -*- lexical-binding: t -*-
;; Author: Philippe Vaucher <philippe.vaucher@gmail.com>
;; Yuki Inoue <inouetakahiroki@gmail.com>
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Code:
(require 's)
(require 'dash)
(require 'json)
(require 'tablist)
(require 'transient)
(require 'docker-core)
(require 'docker-faces)
(require 'docker-utils)
(defgroup docker-container nil
"Docker container customization group."
:group 'docker)
(defcustom docker-container-shell-file-name "/bin/sh"
"Shell to use when entering containers."
:group 'docker-container
:type 'string)
(defcustom docker-container-default-sort-key '("Image" . nil)
"Sort key for docker containers.
This should be a cons cell (NAME . FLIP) where
NAME is a string matching one of the column names
and FLIP is a boolean to specify the sort order."
:group 'docker-container
:type '(cons (choice (const "Id")
(const "Image")
(const "Command")
(const "Created")
(const "Status")
(const "Ports")
(const "Names"))
(choice (const :tag "Ascending" nil)
(const :tag "Descending" t))))
(defun docker-container--read-shell (&optional read-shell-name)
"Return `docker-container-shell-file-name' or read a shell name if READ-SHELL-NAME is truthy."
(if read-shell-name (read-shell-command "Shell: ") docker-container-shell-file-name))
(defun docker-container-parse (line)
"Convert a LINE from \"docker container ls\" to a `tabulated-list-entries' entry."
(condition-case nil
(let* ((data (json-read-from-string line))
(uptime (aref data 3))
(status (aref data 4)))
(aset data 3 (format-time-string "%F %T" (date-to-time uptime)))
(aset data 4 (propertize status 'font-lock-face (docker-container-status-face status)))
(list (aref data 6) data))
(json-readtable-error
(error "Could not read following string as json:\n%s" line))))
(defun docker-container-status-face (status)
"Return the correct face according to STATUS."
(cond
((s-contains? "(Paused)" status)
'docker-face-status-other)
((s-starts-with? "Up" status)
'docker-face-status-up)
((s-starts-with? "Exited" status)
'docker-face-status-down)
(t
'docker-face-status-other)))
(defun docker-container-entries ()
"Return the docker containers data for `tabulated-list-entries'."
(let* ((fmt "[{{json .ID}},{{json .Image}},{{json .Command}},{{json .CreatedAt}},{{json .Status}},{{json .Ports}},{{json .Names}}]")
(data (docker-run-docker "container ls" (docker-container-ls-arguments) (format "--format=\"%s\"" fmt)))
(lines (s-split "\n" data t)))
(-map #'docker-container-parse lines)))
(defun docker-container-refresh ()
"Refresh the containers list."
(setq tabulated-list-entries (docker-container-entries)))
(defun docker-container-read-name ()
"Read an container name."
(completing-read "Container: " (-map #'car (docker-container-entries))))
(defvar eshell-buffer-name)
;;;###autoload
(defun docker-container-eshell (container)
"Open `eshell' in CONTAINER."
(interactive (list (docker-container-read-name)))
(let* ((container-address (format "docker:%s:/" container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
"/")))
(default-directory (format "%s%s" file-prefix container-address))
(eshell-buffer-name (docker-generate-new-buffer-name "eshell" default-directory)))
(eshell)))
;;;###autoload
(defun docker-container-find-directory (container directory)
"Inside CONTAINER open DIRECTORY."
(interactive
(let* ((container-name (docker-container-read-name))
(tramp-filename (read-directory-name "Directory: " (format "/docker:%s:/" container-name))))
(with-parsed-tramp-file-name tramp-filename nil
(list host localname))))
(dired (format "/docker:%s:%s" container directory)))
(defalias 'docker-container-dired 'docker-container-find-directory)
;;;###autoload
(defun docker-container-find-file (container file)
"Open FILE inside CONTAINER."
(interactive
(let* ((container-name (docker-container-read-name))
(tramp-filename (read-file-name "File: " (format "/docker:%s:/" container-name))))
(with-parsed-tramp-file-name tramp-filename nil
(list host localname))))
(find-file (format "/docker:%s:%s" container file)))
;;;###autoload
(defun docker-container-shell (container &optional read-shell)
"Open `shell' in CONTAINER. When READ-SHELL is not nil, ask the user for it."
(interactive (list
(docker-container-read-name)
current-prefix-arg))
(let* ((shell-file-name (docker-container--read-shell read-shell))
(container-address (format "docker:%s:/" container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
"/")))
(default-directory (format "%s%s" file-prefix container-address)))
(shell (docker-generate-new-buffer "shell" default-directory))))
(defun docker-container-cp-from-selection (container-path host-path)
"Run \"docker cp\" from CONTAINER-PATH to HOST-PATH for selected container."
(interactive "sContainer path: \nFHost path: ")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker "cp" (concat it ":" container-path) host-path)))
(defun docker-container-cp-to-selection (host-path container-path)
"Run \"docker cp\" from HOST-PATH to CONTAINER-PATH for selected containers."
(interactive "fHost path: \nsContainer path: ")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker "cp" host-path (concat it ":" container-path))))
(defun docker-container-eshell-selection ()
"Run `docker-container-eshell' on the containers selection."
(interactive)
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-container-eshell it)))
(defun docker-container-find-directory-selection (path)
"Run `docker-container-find-directory' for PATH on the containers selection."
(interactive "sPath: ")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-container-find-directory it path)))
(defun docker-container-find-file-selection (path)
"Run `docker-container-find-file' for PATH on the containers selection."
(interactive "sPath: ")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-container-find-file it path)))
(defun docker-container-rename-selection ()
"Rename containers."
(interactive)
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker "rename" it (read-string (format "Rename \"%s\" to: " it))))
(tablist-revert))
(defun docker-container-shell-selection (prefix)
"Run `docker-container-shell' on the containers selection."
(interactive "P")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-container-shell it prefix)))
(defun docker-container-unpause-selection ()
"Run `docker-container-unpause' on the containers selection."
(interactive)
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker "unpause" it))
(tablist-revert))
(docker-utils-transient-define-prefix docker-container-attach ()
"Transient for attaching to containers."
:man-page "docker-container-attach"
["Arguments"
("n" "No STDIN" "--no-stdin")
("d" "Key sequence for detaching" "--detach-keys=" read-string)]
[:description docker-utils-generic-actions-heading
("a" "Attach" docker-utils-generic-action-async)])
(docker-utils-transient-define-prefix docker-container-cp ()
"Transient for copying files from/to containers."
:man-page "docker-container-cp"
[:description docker-utils-generic-actions-heading
("f" "Copy From" docker-container-cp-from-selection)
("t" "Copy To" docker-container-cp-to-selection)])
(docker-utils-transient-define-prefix docker-container-diff ()
"Transient for showing containers diffs."
:man-page "docker-container-diff"
[:description docker-utils-generic-actions-heading
("d" "Diff" docker-utils-generic-action-with-buffer)])
(docker-utils-transient-define-prefix docker-container-open ()
"Transient for opening containers files."
[:description docker-utils-generic-actions-heading
("d" "Open directory" docker-container-find-directory-selection)
("f" "Open file" docker-container-find-file-selection)])
(docker-utils-transient-define-prefix docker-container-inspect ()
"Transient for inspecting containers."
:man-page "docker-container-inspect"
[:description docker-utils-generic-actions-heading
("I" "Inspect" docker-utils-generic-action-with-buffer:json)])
(docker-utils-transient-define-prefix docker-container-kill ()
"Transient for kill signaling containers"
:man-page "docker-container-kill"
["Arguments"
("s" "Signal" "-s " read-string)]
[:description docker-utils-generic-actions-heading
("K" "Kill" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-container-logs ()
"Transient for showing containers logs."
:man-page "docker-container-logs"
["Arguments"
("f" "Follow" "-f")
("s" "Since" "--since " read-string)
("t" "Tail" "--tail " read-string)
("u" "Until" "--until " read-string)]
[:description docker-utils-generic-actions-heading
("L" "Logs" docker-utils-generic-action-async)])
(defun docker-container-ls-arguments ()
"Return the latest used arguments in the `docker-container-ls' transient."
(car (alist-get 'docker-container-ls transient-history)))
(transient-define-prefix docker-container-ls ()
"Transient for listing containers."
:man-page "docker-container-ls"
:value '("--all")
["Arguments"
("N" "Last" "--last " transient-read-number-N0)
("a" "All" "--all")
("e" "Exited containers" "--filter status=exited")
("f" "Filter" "--filter " read-string)
("n" "Don't truncate" "--no-trunc")]
["Actions"
("l" "List" tablist-revert)])
(docker-utils-transient-define-prefix docker-container-pause ()
"Transient for pauseing containers."
:man-page "docker-container-pause"
[:description docker-utils-generic-actions-heading
("P" "Pause" docker-utils-generic-action)
("U" "Unpause" docker-container-unpause-selection)])
(docker-utils-transient-define-prefix docker-container-restart ()
"Transient for restarting containers."
:man-page "docker-container-restart"
["Arguments"
("t" "Timeout" "-t " transient-read-number-N0)]
[:description docker-utils-generic-actions-heading
("R" "Restart" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-container-rm ()
"Transient for removing containers."
:man-page "docker-container-rm"
["Arguments"
("f" "Force" "-f")
("v" "Volumes" "-v")]
[:description docker-utils-generic-actions-heading
("D" "Remove" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-container-shells ()
"Transient for doing M-x `shell'/`eshell' to containers."
[:description docker-utils-generic-actions-heading
("b" "Shell" docker-container-shell-selection)
("e" "Eshell" docker-container-eshell-selection)])
(docker-utils-transient-define-prefix docker-container-start ()
"Transient for starting containers."
:man-page "docker-container-start"
[:description docker-utils-generic-actions-heading
("S" "Start" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-container-stop ()
"Transient for stoping containers."
:man-page "docker-container-stop"
["Arguments"
("t" "Timeout" "-t " transient-read-number-N0)]
[:description docker-utils-generic-actions-heading
("O" "Stop" docker-utils-generic-action)])
(transient-define-prefix docker-container-help ()
"Help transient for docker containers."
["Docker containers help"
("C" "Copy" docker-container-cp)
("D" "Remove" docker-container-rm)
("I" "Inspect" docker-container-inspect)
("K" "Kill" docker-container-kill)
("L" "Logs" docker-container-logs)
("O" "Stop" docker-container-stop)
("P" "Pause" docker-container-pause)
("R" "Restart" docker-container-restart)
("S" "Start" docker-container-start)
("a" "Attach" docker-container-attach)
("b" "Shell" docker-container-shells)
("d" "Diff" docker-container-diff)
("f" "Find file" docker-container-open)
("l" "List" docker-container-ls)
("r" "Rename" docker-container-rename-selection)])
(defvar docker-container-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "?" 'docker-container-help)
(define-key map "C" 'docker-container-cp)
(define-key map "D" 'docker-container-rm)
(define-key map "I" 'docker-container-inspect)
(define-key map "K" 'docker-container-kill)
(define-key map "L" 'docker-container-logs)
(define-key map "O" 'docker-container-stop)
(define-key map "P" 'docker-container-pause)
(define-key map "R" 'docker-container-restart)
(define-key map "S" 'docker-container-start)
(define-key map "a" 'docker-container-attach)
(define-key map "b" 'docker-container-shells)
(define-key map "d" 'docker-container-diff)
(define-key map "f" 'docker-container-open)
(define-key map "l" 'docker-container-ls)
(define-key map "r" 'docker-container-rename-selection)
map)
"Keymap for `docker-container-mode'.")
;;;###autoload
(defun docker-containers ()
"List docker containers."
(interactive)
(docker-utils-pop-to-buffer "*docker-containers*")
(docker-container-mode)
(tablist-revert))
(define-derived-mode docker-container-mode tabulated-list-mode "Containers Menu"
"Major mode for handling a list of docker containers."
(setq tabulated-list-format [("Id" 16 t)("Image" 15 t)("Command" 30 t)("Created" 23 t)("Status" 20 t)("Ports" 10 t)("Names" 10 t)])
(setq tabulated-list-padding 2)
(setq tabulated-list-sort-key docker-container-default-sort-key)
(add-hook 'tabulated-list-revert-hook 'docker-container-refresh nil t)
(tabulated-list-init-header)
(tablist-minor-mode))
(provide 'docker-container)
;;; docker-container.el ends here