about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/docker-20180710.743/docker-network.el
blob: 78cad55291706741ec3f45168ee71d05dd4e5f40 (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
;;; docker-network.el --- Emacs interface to docker-network  -*- 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 'docker-process)
(require 'docker-utils)
(require 'magit-popup)
(require 'tablist)

(defun docker-network-parse (line)
  "Convert a LINE from \"docker network ls\" to a `tabulated-list-entries' entry."
  (let ((data (s-split " \\{3,\\}" line t)))
    (list (nth 1 data) (apply #'vector data))))

(defun docker-network-entries ()
  "Return the docker networks data for `tabulated-list-entries'."
  (let* ((data (docker-run "network" "ls"))
         (lines (cdr (s-split "\n" data t))))
    (-map #'docker-network-parse lines)))

(defun docker-network-refresh ()
  "Refresh the networks list."
  (setq tabulated-list-entries (docker-network-entries)))

(defun docker-network-read-name ()
  "Read a network name."
  (completing-read "Network: " (-map #'car (docker-network-entries))))

;;;###autoload
(defun docker-network-rm (name)
  "Destroy the network named NAME."
  (interactive (list (docker-network-read-name)))
  (docker-run "network rm" name))

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

(magit-define-popup docker-network-rm-popup
  "Popup for removing networks."
  'docker-network-popups
  :man-page "docker-network-rm"
  :actions  '((?D "Remove" docker-network-rm-selection))
  :setup-function #'docker-utils-popup-setup)

(magit-define-popup docker-network-help-popup
  "Help popup for docker networks."
  :actions '("Docker networks help"
             (?D "Remove"     docker-network-rm-popup)
             "Switch to other parts"
             (?c "Containers" docker-containers)
             (?i "Images"     docker-images)
             (?m "Machines"   docker-machines)
             (?v "Volumes"    docker-volumes)))

(defvar docker-network-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "?" 'docker-network-help-popup)
    (define-key map "D" 'docker-network-rm-popup)
    map)
  "Keymap for `docker-network-mode'.")

;;;###autoload
(defun docker-networks ()
  "List docker networks."
  (interactive)
  (docker-utils-pop-to-buffer "*docker-networks*")
  (docker-network-mode)
  (tablist-revert))

(define-derived-mode docker-network-mode tabulated-list-mode "Networks Menu"
  "Major mode for handling a list of docker networks."
  (setq tabulated-list-format [("Network ID" 20 t)("Name" 50 t)("Driver" 10 t)])
  (setq tabulated-list-padding 2)
  (setq tabulated-list-sort-key (cons "Name" nil))
  (add-hook 'tabulated-list-revert-hook 'docker-network-refresh nil t)
  (tabulated-list-init-header)
  (tablist-minor-mode))

(provide 'docker-network)

;;; docker-network.el ends here