From 26ba79238fa15041fca0dc98d00d1570fa21c24e Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 24 Jul 2024 17:03:15 +0200 Subject: [PATCH] New command 'kubed-kubectl-command' --- lisp/minibuffer.el | 1 + lisp/net/kubed.el | 77 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 31c365bf850..ad8d0f12ea9 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1171,6 +1171,7 @@ styles for specific categories, such as files, buffers, etc." '((buffer (styles basic substring) (export-function . minibuffer-export-list-buffers)) (file (export-function . minibuffer-export-dired)) + (kubed-command-line (styles basic)) (unicode-name (styles basic substring)) ;; A new style that combines substring and pcm might be better, ;; e.g. one that does not anchor to bos. diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index 6b1bdde6773..5289b9bff90 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -1266,6 +1266,80 @@ Optional argument DEFAULT is the minibuffer default argument." (format-prompt prompt default) nil nil nil nil 'kubed-container-images-history default)) +(defun kubed-command-line-completion-table (s p a) + "Completion table for `kubectl' command lines. + +Perform completion action A on string S with predicate P." + (let ((start 0)) + (while (string-match "[[:space:]]" s start) + (setq start (match-end 0))) + (cond + ((eq (car-safe a) 'boundaries) + `(boundaries ,start . ,(and (string-match "[[:space:]]" (cdr a)) + (match-beginning 0)))) + ((eq a 'metadata) + `(metadata + (category . kubed-command-line) + (affixation-function + . ,(lambda (cands) + (let ((max (seq-max + (cons 0 (mapcar #'string-width cands))))) + (mapcar + (lambda (cand) + (list + cand "" + (if-let + ((desc (get-text-property + 0 'kubed-command-line-argument-description + cand))) + (concat + (make-string (1+ (- max (string-width cand))) ?\s) + (propertize desc 'face 'completions-annotations)) + ""))) + cands)))))) + (t (let ((table + (let* ((lines + (apply #'process-lines-ignore-status + kubed-kubectl-executable "__complete" + (let ((args (cdr (split-string-and-unquote s)))) + (if (string-suffix-p " " s) + (nconc args '("")) + args)))) + (code nil) + (comps (seq-take-while + (lambda (line) + (not (and (string-match "^:\\([0-9]+\\)$" line) + (setq code (string-to-number + (match-string 1 line)))))) + lines))) + (when (and code (zerop (logand #b1001 code))) + (mapcar + (lambda (comp) + (pcase (split-string comp "\t" t) + (`(,c ,a . ,_) + (propertize c 'kubed-command-line-argument-description + (car (split-string a "\\." t)))) + (`(,c . ,_) c))) + comps))))) + (if a (complete-with-action a table (substring s start) p) + (let ((comp (complete-with-action a table (substring s start) p))) + (if (stringp comp) (concat (substring s 0 start) comp) comp)))))))) + +(defvar kubed-kubectl-command-history nil + "Minibuffer history for `kubed-kubectl-command'.") + +;;;###autoload +(defun kubed-kubectl-command (command) + "Execute `kubectl' COMMAND. + +This function calls `shell-command' (which see) to do the work. + +Interactively, prompt for COMMAND with completion for `kubectl' arguments." + (interactive + (list (completing-read "Command: " #'kubed-command-line-completion-table + nil nil "kubectl " 'kubed-kubectl-command-history))) + (shell-command command)) + ;;;###autoload (autoload 'kubed-prefix-map "kubed" nil t 'keymap) (defvar-keymap kubed-prefix-map :doc "Prefix keymap for Kubed commands." @@ -1278,7 +1352,8 @@ Optional argument DEFAULT is the minibuffer default argument." "d" 'kubed-deployment-prefix-map "C" #'kubed-use-context "A" #'kubed-all-namespaces-mode - "+" #'kubed-create) + "+" #'kubed-create + "!" #'kubed-kubectl-command) (provide 'kubed) ;;; kubed.el ends here -- 2.39.2