From: Eshel Yaron Date: Wed, 14 Aug 2024 14:45:41 +0000 (+0200) Subject: Teach edit commands about buffer-local contexts and namespaces X-Git-Tag: v0.3.2~15 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=716e951e7195b4cf92b6d4d723b3d299ef230122;p=kubed.git Teach edit commands about buffer-local contexts and namespaces * kubed.el (kubed-edit-resource): Add interactive spec. (kubed-define-resource): Refine interactive spec for generated 'kubed-edit-RESOURCE' commands. * kubed-transient.el (kubed-transient-edit): New transient. (kubed-transient): Bind it. --- diff --git a/kubed-transient.el b/kubed-transient.el index 1641050..fe78a78 100644 --- a/kubed-transient.el +++ b/kubed-transient.el @@ -51,6 +51,7 @@ ("RET" "Display" kubed-transient-display) ("+" "Create" kubed-transient-create) ("*" "Apply" kubed-transient-apply) + ("e" "Edit" kubed-transient-edit) ("D" "Delete" kubed-transient-delete)] ;; Second column. [("r" "Run" kubed-transient-run) @@ -239,6 +240,32 @@ (transient-setup 'kubed-transient-display nil nil :scope '("get"))) +;;;###autoload (autoload 'kubed-transient-edit "kubed-transient" nil t) +(transient-define-prefix kubed-transient-edit () + "Edit Kubernetes resource." + ["Kubernetes Edit\n" + ["Kinds" + ("p" "Pod" kubed-edit-pod) + ("d" "Deployment" kubed-edit-deployment) + ("j" "Job" kubed-edit-job) + ("c" "CronJob" kubed-edit-cronjob) + ("s" "Service" kubed-edit-service)] + ["More" + :pad-keys t + ("S" "Secret" kubed-edit-secret) + ("N" "Namespace" kubed-edit-namespace) + ("i" "Ingress" kubed-edit-ingress) + ("RET" "Any type" kubed-edit-resource) + ("!" "Command line" kubed-kubectl-command)] + ["Options" + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace) + ("-C" "Context" "--context=" + :prompt "Context" :reader kubed-transient-read-context)]] + (interactive) + (transient-setup 'kubed-transient-edit nil nil + :scope '("edit"))) + ;;;###autoload (autoload 'kubed-transient-delete "kubed-transient" nil t) (transient-define-prefix kubed-transient-delete () "Delete Kubernetes resource." diff --git a/kubed.el b/kubed.el index 8515ce3..a63e5c3 100644 --- a/kubed.el +++ b/kubed.el @@ -1004,8 +1004,9 @@ mode as their parent." (setq-local bookmark-make-record-function #'kubed-list-make-bookmark) (add-hook 'context-menu-functions #'kubed-list-context-menu nil t)) +;;;###autoload (defun kubed-delete-resources (type resources context &optional namespace) - "Delete Kubernetes RESOURCES of type TYPE in CONTEXT. + "Delete Kubernetes RESOURCES of type TYPE in context CONTEXT. For namespaced resource types, NAMESPACE is the namespace of RESOURCE. @@ -1069,8 +1070,44 @@ compatibility with earlier Emacs versions." then-form (when else-forms (cons 'progn else-forms)))) -(defun kubed-edit-resource (type resource &optional context namespace) - "Edit Kubernetes RESOURCE of type TYPE." +;;;###autoload +(defun kubed-edit-resource (type resource context &optional namespace) + "Edit Kubernetes RESOURCE of type TYPE in context CONTEXT. + +For namespaced resource types, NAMESPACE is the namespace of RESOURCE. + +Interactively, use the current context and namespace by default, and +prompt for TYPE and RESOURCES. With a prefix argument \ +\\[universal-argument], +prompt for NAMESPACE. With a double prefix argument \ +\\[universal-argument] \\[universal-argument], +prompt for CONTEXT as well." + (interactive + (let ((type nil) (context nil) (namespace nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-edit)) + (cond + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if (equal current-prefix-arg '(16)) + (kubed-read-context "Context" cxt) + cxt)))) + (unless type + (setq type (kubed-read-resource-type "Type of resource to edit" + nil context))) + (when (and (kubed-namespaced-p type context) (null namespace)) + (setq namespace + (let ((cur (kubed-local-namespace context))) + (if current-prefix-arg + (kubed-read-namespace "Namespace" cur nil context) + cur)))) + (list type (kubed-read-resource-name type "Edit" nil nil + context namespace) + context namespace))) (unless (bound-and-true-p server-process) (server-start)) (let ((process-environment (cons (kubed--static-if (<= 30 emacs-major-version) @@ -1270,15 +1307,55 @@ Interactively, use the current context. With a prefix argument . ,(when namespaced '((or namespace (kubed-local-namespace context))))))) (defun ,edt-name (,resource &optional context . ,(when namespaced '(namespace))) - ,(format "Edit Kubernetes %S %s." resource (upcase (symbol-name resource))) - (interactive ,(if namespaced - `(let ((namespace (and current-prefix-arg - (kubed-read-namespace - "Namespace" (kubed-current-namespace))))) - (list (,read-fun "Edit" nil nil nil namespace) nil namespace)) - `(list (,read-fun "Edit")))) - (kubed-edit-resource ,(symbol-name plrl-var) ,resource context - . ,(when namespaced '(namespace)))) + ,(if namespaced + (format "Edit Kubernetes %S %s in CONTEXT and NAMESPACE. + +Interactively, use the current context and namespace by default. With a +prefix argument \\[universal-argument], prompt for NAMESPACE. With a +double prefix argument \\[universal-argument] \\[universal-argument], \ +prompt for CONTEXT as well." resource (upcase (symbol-name resource))) + (format "Edit Kubernetes %S %s in context CONTEXT. + +Interactively, use the current context. With a prefix argument +\\[universal-argument], prompt for CONTEXT." resource (upcase (symbol-name resource)))) + (interactive + ,(if namespaced + `(let ((context nil) (namespace nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-edit)) + (cond + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if (equal current-prefix-arg '(16)) + (kubed-read-context "Context" cxt) + cxt)))) + (unless namespace + (setq namespace + (let ((cur (kubed-local-namespace context))) + (if current-prefix-arg + (kubed-read-namespace "Namespace" cur nil context) + cur)))) + (list (,read-fun "Edit" nil nil context namespace) context namespace)) + `(let ((context nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-edit)) + (cond + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if current-prefix-arg + (kubed-read-context "Context" cxt) + cxt)))) + (list (,read-fun "Edit" nil nil context) context)))) + (let ((context (or context (kubed-local-context)))) + (kubed-edit-resource + ,(symbol-name plrl-var) ,resource context + . ,(when namespaced '((or namespace (kubed-local-namespace context))))))) (defun ,dlt-name (,plrl-var &optional context . ,(when namespaced '(namespace)))