Skip to content

Commit

Permalink
Provide kubel-kill-buffer command. (abrochard#134)
Browse files Browse the repository at this point in the history
Since C-c C-k is fairly popular binding (magit, message, ...), provide it as
well.  It allows quickly canceling the edit without applying the changes.

This also changes kubel-yaml-editing-mode to be defined using
define-derived-mode from yaml-mode, with standalone map in
kubel-yaml-editing-map.

* kubel.el (kubel-kill-buffer-query): New custom.
(kubel-kill-buffer): New function.
(kubel-yaml-editing-map): New variable.
(kubel-yaml-editing-mode): Change to derived-mode from minor-mode.
(kubel--describe-resource, kubel-get-resource-details): Stop calling yaml-mode.
  • Loading branch information
graywolf committed Aug 21, 2024
1 parent 68da30c commit a3f3fc9
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions kubel.el
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ with information about the shell's connection. The following
:type 'string
:group 'kubel)

(defcustom kubel-kill-buffer-query t
"Non-nil means that killing a modified resource buffer has to be confirmed.
This is used by `kubel-kill-buffer'."
:type 'boolean
:group 'kubel)

(defun kubel--append-to-process-buffer (str)
"Append string STR to the process buffer."
(with-current-buffer (get-buffer-create kubel--process-buffer)
Expand Down Expand Up @@ -590,12 +596,13 @@ NAME is the string name of the resource to decribe.
DESCRIBE is boolean to describe instead of get resource details"
(let* ((resource (kubel--select-resource name))
(process-name (format "kubel - %s - %s" name resource))
(callback (lambda () (goto-char (point-min)))))
(callback (lambda ()
(set-buffer-modified-p nil)
(goto-char (point-min)))))
(if describe
(kubel--exec process-name (list "describe" name resource) nil callback)
(kubel--exec process-name (list "get" name "-o" kubel-output resource) nil callback))
(when (string-equal kubel-output "yaml")
(yaml-mode)
(kubel-yaml-editing-mode))))

(defun kubel--show-rollout-revision (type name)
Expand Down Expand Up @@ -640,15 +647,29 @@ TYPENAME is the resource type/name."
(-contains? '("ReplicaSets" "replicasets" "replicasets.apps") kubel-resource)
(-contains? '("StatefulSets" "statefulsets" "statefulsets.apps") kubel-resource)))

(defun kubel-kill-buffer ()
"Kill the current buffer."
(interactive)
(when (or (not (buffer-modified-p))
(not kubel-kill-buffer-query)
(yes-or-no-p "Resource modified; kill anyway? "))
(kill-buffer (current-buffer))))

(defvar kubel-yaml-editing-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'kubel-apply)
(define-key map (kbd "C-c C-k") #'kubel-kill-buffer)
map)
"Keymap used in `kubel-yaml-editing-mode' buffers.")

;; interactive
;;;###autoload
(define-minor-mode kubel-yaml-editing-mode
(define-derived-mode kubel-yaml-editing-mode yaml-mode "kubel/e"
"Kubel Yaml editing mode.
Use C-c C-c to kubectl apply the current yaml buffer."
:init-value nil
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'kubel-apply)
map))
Allows simple apply of the changes made.
\\{kubel-yaml-editing-map}")

(defun kubel-apply ()
"Save the current buffer to a temp file and try to kubectl apply it."
Expand Down Expand Up @@ -679,12 +700,13 @@ Use C-c C-c to kubectl apply the current yaml buffer."
(ns kubel-namespace)
(res kubel-resource)
(process-name (format "kubel - %s - %s" kubel-resource resource))
(callback (lambda () (goto-char (point-min)))))
(callback (lambda ()
(set-buffer-modified-p nil)
(goto-char (point-min)))))
(if describe
(kubel--exec process-name (list "describe" kubel-resource (kubel--get-resource-under-cursor)) nil callback)
(kubel--exec process-name (list "get" kubel-resource (kubel--get-resource-under-cursor) "-o" kubel-output) nil callback))
(when (or (string-equal kubel-output "yaml") (transient-args 'kubel-describe-popup))
(yaml-mode)
(kubel-yaml-editing-mode)
(setq kubel-context ctx)
(setq kubel-namespace ns)
Expand Down

0 comments on commit a3f3fc9

Please sign in to comment.