forked from Silex/docker.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-image.el
221 lines (190 loc) · 7.86 KB
/
docker-image.el
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
;;; docker-image.el --- Emacs interface to docker-image -*- lexical-binding: t -*-
;; Author: Philippe Vaucher <philippe.vaucher@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-utils)
(defgroup docker-image nil
"Docker images customization group."
:group 'docker)
(defcustom docker-image-default-sort-key '("Repository" . nil)
"Sort key for docker images.
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-image
:type '(cons (choice (const "Repository")
(const "Tag")
(const "Id")
(const "Created")
(const "Size"))
(choice (const :tag "Ascending" nil)
(const :tag "Descending" t))))
(defun docker-image-parse (line)
"Convert a LINE from \"docker image ls\" to a `tabulated-list-entries' entry."
(condition-case nil
(let* ((data (json-read-from-string line))
(name (format "%s:%s" (aref data 0) (aref data 1))))
(aset data 3 (format-time-string "%F %T" (date-to-time (aref data 3))))
(list (if (s-contains? "<none>" name) (aref data 2) name) data))
(json-readtable-error
(error "Could not read following string as json:\n%s" line))))
(defun docker-image-entries ()
"Return the docker images data for `tabulated-list-entries'."
(let* ((fmt "[{{json .Repository}},{{json .Tag}},{{json .ID}},{{json .CreatedAt}},{{json .Size}}]")
(data (docker-run-docker "image ls" (docker-image-ls-arguments) (format "--format=\"%s\"" fmt)))
(lines (s-split "\n" data t)))
(-map #'docker-image-parse lines)))
(defun docker-image-refresh ()
"Refresh the images list."
(setq tabulated-list-entries (docker-image-entries)))
(defun docker-image-read-name ()
"Read an image name."
(completing-read "Image: " (-map #'car (docker-image-entries))))
(defun docker-image-human-size-predicate (a b)
"Sort A and B by image size."
(let* ((a-size (elt (cadr a) 4))
(b-size (elt (cadr b) 4)))
(< (docker-utils-human-size-to-bytes a-size) (docker-utils-human-size-to-bytes b-size))))
;;;###autoload
(defun docker-image-pull-one (name &optional all)
"Pull the image named NAME. If ALL is set, use \"-a\"."
(interactive (list (docker-image-read-name) current-prefix-arg))
(docker-run-docker "pull" (when all "-a ") name))
(defun docker-image-run-selection (command)
"Run \"docker image run\" with COMMAND on the images selection."
(interactive "sCommand: ")
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker-async "container run" (transient-args 'docker-image-run) it command))
(tablist-revert))
(defun docker-image-tag-selection ()
"Tag images."
(interactive)
(docker-utils-ensure-items)
(--each (docker-utils-get-marked-items-ids)
(docker-run-docker "tag" it (read-string (format "Tag for %s: " it))))
(tablist-revert))
(docker-utils-transient-define-prefix docker-image-inspect ()
"Transient for inspecting images."
:man-page "docker-image-inspect"
[:description docker-utils-generic-actions-heading
("I" "Inspect" docker-utils-generic-action-with-buffer:json)])
(defun docker-image-ls-arguments ()
"Return the latest used arguments in the `docker-image-ls' transient."
(car (alist-get 'docker-image-ls transient-history)))
(transient-define-prefix docker-image-ls ()
"Transient for listing images."
:man-page "docker-image-ls"
["Arguments"
("a" "All" "--all")
("d" "Dangling" "-f dangling=true")
("f" "Filter" "--filter" read-string)
("n" "Don't truncate" "--no-trunc")]
["Actions"
("l" "List" tablist-revert)])
(transient-define-prefix docker-image-pull ()
"Transient for pulling images."
:man-page "docker-image-pull"
["Arguments"
("a" "All" "-a")]
[:description docker-utils-generic-actions-heading
("F" "Pull selection" docker-utils-generic-action)
("N" "Pull a new image" docker-image-pull-one)])
(docker-utils-transient-define-prefix docker-image-push ()
"Transient for pushing images."
:man-page "docker-image-push"
[:description docker-utils-generic-actions-heading
("P" "Push" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-image-rm ()
"Transient for removing images."
:man-page "docker-image-rm"
["Arguments"
("-f" "Force" "-f")
("-n" "Don't prune" "--no-prune")]
[:description docker-utils-generic-actions-heading
("D" "Remove" docker-utils-generic-action)])
(docker-utils-transient-define-prefix docker-image-run ()
"Transient for running images."
:man-page "docker-image-run"
:value '("-i" "-t" "--rm")
["Arguments"
("D" "With display" "-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY")
("M" "Mount volume" "--mount=" read-string)
("N" "Network" "--network " read-string)
("P" "Privileged" "--privileged")
("T" "Synchronize time" "-v /etc/localtime:/etc/localtime:ro")
("W" "Web ports" "-p 80:80 -p 443:443 -p 8080:8080")
("d" "Detach" "-d")
("e" "environment" "-e " read-string)
("i" "Interactive" "-i")
("m" "name" "--name " read-string)
("n" "entrypoint" "--entrypoint " read-string)
("o" "Read only" "--read-only")
("p" "port" "-p " read-string)
("r" "Remove container when it exits" "--rm")
("t" "TTY" "-t")
("u" "user" "-u " read-string)
("v" "volume" "-v " read-string)
("w" "workdir" "-w " read-string)]
[:description docker-utils-generic-actions-heading
("R" "Run" docker-image-run-selection)])
(transient-define-prefix docker-image-help ()
"Help transient for docker images."
["Docker images help"
("D" "Remove" docker-image-rm)
("F" "Pull" docker-image-pull)
("I" "Inspect" docker-image-inspect)
("P" "Push" docker-image-push)
("R" "Run" docker-image-run)
("T" "Tag" docker-image-tag-selection)
("l" "List" docker-image-ls)])
(defvar docker-image-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "?" 'docker-image-help)
(define-key map "D" 'docker-image-rm)
(define-key map "F" 'docker-image-pull)
(define-key map "I" 'docker-image-inspect)
(define-key map "P" 'docker-image-push)
(define-key map "R" 'docker-image-run)
(define-key map "T" 'docker-image-tag-selection)
(define-key map "l" 'docker-image-ls)
map)
"Keymap for `docker-image-mode'.")
;;;###autoload
(defun docker-images ()
"List docker images."
(interactive)
(docker-utils-pop-to-buffer "*docker-images*")
(docker-image-mode)
(tablist-revert))
(define-derived-mode docker-image-mode tabulated-list-mode "Images Menu"
"Major mode for handling a list of docker images."
(setq tabulated-list-format [("Repository" 30 t)("Tag" 20 t)("Id" 16 t)("Created" 23 t)("Size" 10 docker-image-human-size-predicate)])
(setq tabulated-list-padding 2)
(setq tabulated-list-sort-key docker-image-default-sort-key)
(add-hook 'tabulated-list-revert-hook 'docker-image-refresh nil t)
(tabulated-list-init-header)
(tablist-minor-mode))
(provide 'docker-image)
;;; docker-image.el ends here