about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/docker-20180820.1130/docker-image.el
blob: e5fd79a8a4190aacafe71a1f2d9710c40b92637b (plain) (blame)
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
;;; 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 'magit-popup)

(require 'docker-group)
(require 'docker-process)
(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))))

(defcustom docker-image-run-arguments '("-i" "-t" "--rm")
  "Default arguments for `docker-image-run-popup'."
  :group 'docker-image
  :type '(repeat (string :tag "Argument")))

(defun docker-image-parse (line)
  "Convert a LINE from \"docker images\" to a `tabulated-list-entries' entry."
  (let* ((data (s-split "\t" line))
         (name (format "%s:%s" (nth 0 data) (nth 1 data))))
    (setf (nth 3 data) (format-time-string "%F %T" (date-to-time (nth 3 data))))
    (list
     (if (s-contains? "<none>" name) (nth 2 data) name)
     (apply #'vector data))))

(defun docker-image-entries ()
  "Return the docker images data for `tabulated-list-entries'."
  (let* ((fmt "{{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.CreatedAt}}\\t{{.Size}}")
         (data (docker-run "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-pull (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 "pull" (when all "-a ") name))

;;;###autoload
(defun docker-push (name)
  "Push the image named NAME."
  (interactive (list (docker-image-read-name)))
  (docker-run "push" name))

;;;###autoload
(defun docker-rmi (name &optional force no-prune)
  "Destroy or untag the image named NAME.

Force removal of the image when FORCE is set.
Do not delete untagged parents when NO-PRUNE is set."
  (interactive (list (docker-image-read-name) current-prefix-arg))
  (docker-run "rmi" (when force "-f") (when no-prune "--no-prune") name))

;;;###autoload
(defun docker-tag (image name)
  "Tag IMAGE using NAME."
  (interactive (list (docker-image-read-name) (read-string "Name: ")))
  (docker-run "tag" image name))

(defun docker-image-inspect-selection ()
  "Run \"docker inspect\" on the images selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-utils-with-buffer (format "inspect %s" it)
      (insert (docker-run "inspect" (docker-image-inspect-arguments) it))
      (json-mode))))

(defun docker-image-pull-selection ()
  "Run \"docker pull\" on the images selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-run "pull" (docker-image-pull-arguments) it))
  (tablist-revert))

(defun docker-image-push-selection ()
  "Run \"docker push\" on the images selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-run "push" (docker-image-push-arguments) it)))

(defun docker-image-rm-selection ()
  "Run \"docker rmi\" on the images selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-run "rmi" (docker-image-rm-arguments) it))
  (tablist-revert))

(defun docker-image-run-selection (command)
  "Run \"docker run\" on the images selection."
  (interactive "sCommand: ")
  (let ((default-directory (if (and docker-run-as-root
                                    (not (file-remote-p default-directory)))
                               "/sudo::"
                             default-directory)))
    (--each (docker-utils-get-marked-items-ids)
      (async-shell-command
       (format "%s run %s %s %s" docker-command (s-join " " (docker-image-run-arguments)) it command)
       (generate-new-buffer (format "*run %s*" it))))))

(defun docker-image-tag-selection ()
  "Tag images."
  (interactive)
  (docker-utils-select-if-empty)
  (--each (docker-utils-get-marked-items-ids)
    (docker-tag it (read-string (format "Tag for %s: " it))))
  (tablist-revert))

(magit-define-popup docker-image-inspect-popup
  "Popup for inspecting images."
  'docker-image
  :man-page "docker-inspect"
  :actions  '((?I "Inspect" docker-image-inspect-selection))
  :setup-function #'docker-utils-setup-popup)

(magit-define-popup docker-image-ls-popup
  "Popup for listing images."
  'docker-image
  :man-page "docker-image-ls"
  :switches  '((?a "All" "--all")
               (?d "Dangling" "-f dangling=true")
               (?n "Don't truncate" "--no-trunc"))
  :options   '((?f "Filter" "--filter "))
  :actions   `((?l "List" ,(docker-utils-set-then-call 'docker-image-ls-arguments 'tablist-revert))))

(magit-define-popup docker-image-pull-popup
  "Popup for pulling images."
  'docker-image
  :man-page "docker-pull"
  :switches '((?a "All" "-a"))
  :actions  '((?F "Pull" docker-image-pull-selection))
  :setup-function #'docker-utils-setup-popup)

(magit-define-popup docker-image-push-popup
  "Popup for pushing images."
  'docker-image
  :man-page "docker-push"
  :actions  '((?P "Push" docker-image-push-selection))
  :setup-function #'docker-utils-setup-popup)

(magit-define-popup docker-image-rm-popup
  "Popup for removing images."
  'docker-image
  :man-page "docker-rmi"
  :switches '((?f "Force" "-f")
              (?n "Don't prune" "--no-prune"))
  :actions  '((?D "Remove" docker-image-rm-selection))
  :setup-function #'docker-utils-setup-popup)

(magit-define-popup docker-image-run-popup
  "Popup for running images."
  'docker-image
  :man-page "docker-run"
  :switches '((?D "With display" "-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY")
              (?T "Synchronize time" "-v /etc/localtime:/etc/localtime:ro")
              (?W "Web ports" "-p 80:80 -p 443:443 -p 8080:8080")
              (?d "Daemonize" "-d")
              (?i "Interactive" "-i")
              (?o "Read only" "--read-only")
              (?p "Privileged" "--privileged")
              (?r "Remove container when it exits" "--rm")
              (?t "TTY" "-t"))
  :options  '((?e "environment" "-e ")
              (?m "name" "--name ")
              (?n "entrypoint" "--entrypoint ")
              (?p "port" "-p ")
              (?u "user" "-u ")
              (?v "volume" "-v ")
              (?w "workdir" "-w "))
  :actions  '((?R "Run images" docker-image-run-selection))
  :setup-function #'docker-utils-setup-popup)

(magit-define-popup docker-image-help-popup
  "Help popup for docker images."
  'docker-image
  :actions '("Docker images help"
             (?D "Remove"  docker-image-rm-popup)
             (?F "Pull"    docker-image-pull-popup)
             (?I "Inspect" docker-image-inspect-popup)
             (?P "Push"    docker-image-push-popup)
             (?R "Run"     docker-image-run-popup)
             (?T "Tag"     docker-image-tag-selection)
             (?l "List"    docker-image-ls-popup)))

(defvar docker-image-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "?" 'docker-image-help-popup)
    (define-key map "D" 'docker-image-rm-popup)
    (define-key map "F" 'docker-image-pull-popup)
    (define-key map "I" 'docker-image-inspect-popup)
    (define-key map "P" 'docker-image-push-popup)
    (define-key map "R" 'docker-image-run-popup)
    (define-key map "T" 'docker-image-tag-selection)
    (define-key map "l" 'docker-image-ls-popup)
    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