From: Eshel Yaron Date: Wed, 7 Aug 2024 15:33:11 +0000 (+0200) Subject: New commands for editing displayed resources X-Git-Tag: v0.3.2~24 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=704c4f2804eabbef512ff18c3b85698762bfd991;p=kubed.git New commands for editing displayed resources * kubed.el (kubed-display-resource-diff) (kubed-display-resource-replace): New commands. (kubed-display-resource-mode-map): Bind them. (kubed-display-config): Fix typo. (kubed-diff): Learn to use a buffer as DEFINITION. * kubed.texi (Display Resource): Document new commands. * NEWS.org: Mention it. --- diff --git a/NEWS.org b/NEWS.org index 2ba9c74..c02395b 100644 --- a/NEWS.org +++ b/NEWS.org @@ -10,6 +10,19 @@ for Kubernetes. For further details, see the Kubed manual: [[https://eshelyaron.com/sweep.html][https://eshelyaron.com/kubed.html]]. +* Version 0.3.2 in development + +** New filter operators ~<~ and ~>~ in resource tables. + +These operators let you filter line with columns that are less than or +greater than a given value. + +** New command for applying changes to displayed resource. + +You can now edit displayed resource YAML buffers after you select a +resource with ~RET~ in the table buffer, and apply your changes to the +live resource with ~C-c C-c~. + * Version 0.3.1 on 2024-08-06 ** New command for fitting column width to content in resource tables. diff --git a/kubed.el b/kubed.el index 972aff2..bcb86fd 100644 --- a/kubed.el +++ b/kubed.el @@ -295,6 +295,30 @@ the namespace of the resource, or nil if TYPE is not namespaced.") (when namespace (list namespace))) (kubed-list-go-to-line name))) +(defun kubed-display-resource-diff () + "Show diff of current buffer with current state of the displayed resource." + (interactive) + (kubed-diff (current-buffer) nil (nth 2 kubed-display-resource-info))) + +(defun kubed-display-resource-replace () + "Replace displayed Kubernetes resource with current buffer contents." + (interactive) + (let (choice) + (while (= ?d (setq choice + (car (read-multiple-choice + "Replace resource with changes in current buffer?" + '((?y "yes") (?n "no") (?d "diff")))))) + (kubed-display-resource-diff)) + (when (= ?y choice) + (let ((err-buf (get-buffer-create " *kubed-replace*"))) + (with-current-buffer err-buf (erase-buffer)) + (unless (zerop (call-process-region nil nil kubed-kubectl-program + nil err-buf nil "replace" "-f" "-")) + (display-buffer err-buf) + (user-error "`kubectl replace' failed!"))) + (message "Replaced Kubernetes resource with changes in current buffer.") + (revert-buffer)))) + (defun kubed-display-resource-p (_symbol buffer) "Return non-nil if `kubed-display-resource-mode' is enabled in BUFFER. @@ -303,12 +327,16 @@ the `completion-predicate' property of commands that you define that should only be available in buffers that display Kuberenetes resources." (buffer-local-value 'kubed-display-resource-mode buffer)) -(put 'kubed-display-resource-jump-to-list 'completion-predicate - #'kubed-display-resource-p) +(dolist (cmd '(kubed-display-resource-jump-to-list + 'kubed-display-resource-diff + 'kubed-display-resource-replace)) + (put cmd 'completion-predicate #'kubed-display-resource-p)) (defvar-keymap kubed-display-resource-mode-map :doc "Keymap buffers that display Kubernetes resource." - "C-c C-j" #'kubed-display-resource-jump-to-list) + "C-c C-j" #'kubed-display-resource-jump-to-list + "C-c C-=" #'kubed-display-resource-diff + "C-c C-c" #'kubed-display-resource-replace) (define-minor-mode kubed-display-resource-mode "Minor mode for buffers that display a Kuberenetes resource." @@ -1963,7 +1991,7 @@ Optional argument DEFAULT is the minibuffer default argument." (unless (zerop (call-process kubed-kubectl-program nil t nil "config" "view")) - (error "`kubectl config view'")) + (error "`kubectl config view' failed")) (let ((source (current-buffer))) (with-current-buffer target (replace-buffer-contents source) @@ -2377,6 +2405,9 @@ with \\[universal-argument] \\[universal-argument]; and TTY is t unless\ (defun kubed-diff (definition &optional include-managed context) "Display difference between Kubernetes resource DEFINITION and current state. +DEFINITION is either a file name or a buffer. Interactively, prompt for +a YAML or JSON file name to use as DEFINITION. + Optional argument CONTEXT is the `kubectl' context to use, defaulting to the current context; non-nil INCLUDE-MANAGED (interactively, the prefix argument) says to include managed fields in the comparison." @@ -2394,16 +2425,24 @@ argument) says to include managed fields in the comparison." (list (or definition (kubed-read-resource-definition-file-name)) (or include-managed current-prefix-arg) context))) - (let ((buf (get-buffer-create "*kubed-diff*"))) + (let ((buf (get-buffer-create "*kubed-diff*")) + (args (cons "diff" + (append + (when context (list "--context" context)) + (when include-managed + (list "--show-managed-fields" "true")) + (list "-f" (if (bufferp definition) "-" + (expand-file-name definition))))))) (with-current-buffer buf (setq buffer-read-only nil) (delete-region (point-min) (point-max)) (fundamental-mode) - (apply #'call-process kubed-kubectl-program nil t nil "diff" - (concat "--show-managed-fields=" - (if include-managed "true" "false")) - "-f" (expand-file-name definition) - (when context (list "--context" context))) + (if (bufferp definition) + (with-current-buffer definition + (apply #'call-process-region nil nil kubed-kubectl-program + nil buf nil args)) + (apply #'call-process kubed-kubectl-program + nil t nil args)) (setq buffer-read-only t) (diff-mode) (goto-char (point-min))) diff --git a/kubed.texi b/kubed.texi index 8642f40..bc59045 100644 --- a/kubed.texi +++ b/kubed.texi @@ -261,6 +261,31 @@ It binds the key sequence @kbd{C-c C-j} to command @code{kubed-display-resource-jump-to-list}, which pops up the resources list buffer for the type of the displayed resource, and takes you to the line corresponding to it. +@item +@kindex C-c C-c +@findex kubed-display-resource-replace +@kindex C-c C-= +@findex kubed-display-resource-diff +@kindex C-x C-q +@findex read-only-mode +It binds @kbd{C-c C-c} to command +@code{kubed-display-resource-replace}, which lets you apply any +changes that you make in the YAML buffer back to the resource. When +you hit @kbd{C-c C-c}, Kubed asks you for confirmation before +proceeding; you can reply with @kbd{d} (@samp{diff}) to show a diff +between the current state of the resource and the current buffer +contents. You can also display this diff anytime with @kbd{C-c C-=}. +Note that by default Kubed displays resources in read-only buffers, so +you may need to hit @kbd{C-x C-q} (@code{read-only-mode}) before +making changes in the buffer. +@item +@kindex C-x x g +@findex revert-buffer +@findex revert-buffer-quick +It lets you repopulate the buffer with updated information about the +resource with @kbd{C-x x g} (@code{revert-buffer-quick}) and +@w{@kbd{M-x revert-buffer}}. This is also useful if you edit the +resource and want to restart from a clean slate. @end itemize @node Browse Resources