Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.7 KiB

  1. ;;; docker-network.el --- Emacs interface to docker-network -*- lexical-binding: t -*-
  2. ;; Author: Philippe Vaucher <philippe.vaucher@gmail.com>
  3. ;; This file is NOT part of GNU Emacs.
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation; either version 3, or (at your option)
  7. ;; any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;; Boston, MA 02110-1301, USA.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (require 's)
  21. (require 'dash)
  22. (require 'json)
  23. (require 'tablist)
  24. (require 'magit-popup)
  25. (require 'docker-group)
  26. (require 'docker-process)
  27. (require 'docker-utils)
  28. (defgroup docker-network nil
  29. "Docker network customization group."
  30. :group 'docker)
  31. (defcustom docker-network-default-sort-key '("Name" . nil)
  32. "Sort key for docker networks.
  33. This should be a cons cell (NAME . FLIP) where
  34. NAME is a string matching one of the column names
  35. and FLIP is a boolean to specify the sort order."
  36. :group 'docker-network
  37. :type '(cons (choice (const "Network ID")
  38. (const "Name")
  39. (const "Driver"))
  40. (choice (const :tag "Ascending" nil)
  41. (const :tag "Descending" t))))
  42. (defun docker-network-parse (line)
  43. "Convert a LINE from \"docker network ls\" to a `tabulated-list-entries' entry."
  44. (condition-case nil
  45. (let ((data (json-read-from-string line)))
  46. (list (aref data 1) data))
  47. (json-readtable-error
  48. (error "Could not read following string as json:\n%s" line))))
  49. (defun docker-network-entries ()
  50. "Return the docker networks data for `tabulated-list-entries'."
  51. (let* ((fmt (if (docker-utils-podman-p) "[{{json .ID}},{{json .Name}},{{json .Driver}},{{json .Scope}}]"))
  52. (data (docker-run "network ls" docker-network-ls-arguments (format "--format=\"%s\"" fmt)))
  53. (lines (s-split "\n" data t)))
  54. (-map #'docker-network-parse lines)))
  55. (defun docker-network-refresh ()
  56. "Refresh the networks list."
  57. (setq tabulated-list-entries (docker-network-entries)))
  58. (defun docker-network-read-name ()
  59. "Read a network name."
  60. (completing-read "Network: " (-map #'car (docker-network-entries))))
  61. ;;;###autoload
  62. (defun docker-network-rm (name)
  63. "Destroy the network named NAME."
  64. (interactive (list (docker-network-read-name)))
  65. (docker-run "network rm" name))
  66. (defun docker-network-rm-selection ()
  67. "Run \"docker network rm\" on the selection."
  68. (interactive)
  69. (--each (docker-utils-get-marked-items-ids)
  70. (docker-run "network rm" it))
  71. (tablist-revert))
  72. (magit-define-popup docker-network-ls-popup
  73. "Popup for listing networks."
  74. 'docker-network
  75. :man-page "docker-network-ls"
  76. :switches '((?n "Don't truncate" "--no-trunc"))
  77. :options '((?f "Filter" "--filter "))
  78. :actions `((?l "List" ,(docker-utils-set-then-call 'docker-network-ls-arguments 'tablist-revert))))
  79. (magit-define-popup docker-network-rm-popup
  80. "Popup for removing networks."
  81. 'docker-network
  82. :man-page "docker-network-rm"
  83. :actions '((?D "Remove" docker-network-rm-selection))
  84. :setup-function #'docker-utils-popup-setup)
  85. (magit-define-popup docker-network-help-popup
  86. "Help popup for docker networks."
  87. 'docker-network
  88. :actions '("Docker networks help"
  89. (?D "Remove" docker-network-rm-popup)
  90. (?l "List" docker-network-ls-popup)))
  91. (defvar docker-network-mode-map
  92. (let ((map (make-sparse-keymap)))
  93. (define-key map "?" 'docker-network-help-popup)
  94. (define-key map "D" 'docker-network-rm-popup)
  95. (define-key map "l" 'docker-network-ls-popup)
  96. map)
  97. "Keymap for `docker-network-mode'.")
  98. ;;;###autoload
  99. (defun docker-networks ()
  100. "List docker networks."
  101. (interactive)
  102. (docker-utils-pop-to-buffer "*docker-networks*")
  103. (docker-network-mode)
  104. (tablist-revert))
  105. (define-derived-mode docker-network-mode tabulated-list-mode "Networks Menu"
  106. "Major mode for handling a list of docker networks."
  107. (setq tabulated-list-format [("Network ID" 20 t)("Name" 50 t)("Driver" 10 t)("Scope" 10 t)])
  108. (setq tabulated-list-padding 2)
  109. (setq tabulated-list-sort-key docker-network-default-sort-key)
  110. (add-hook 'tabulated-list-revert-hook 'docker-network-refresh nil t)
  111. (tabulated-list-init-header)
  112. (tablist-minor-mode))
  113. (provide 'docker-network)
  114. ;;; docker-network.el ends here