]> git.eshelyaron.com Git - emacs.git/commitdiff
New command 'kubed-kubectl-command'
authorEshel Yaron <me@eshelyaron.com>
Wed, 24 Jul 2024 15:03:15 +0000 (17:03 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 24 Jul 2024 15:03:15 +0000 (17:03 +0200)
lisp/minibuffer.el
lisp/net/kubed.el

index 31c365bf8501ce4ff92cda647b4a5b4a2ae9f4a8..ad8d0f12ea9082b822601e1d27e70321caca1be5 100644 (file)
@@ -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.
index 6b1bdde6773b4218f3e8fef76903e37cdc04ec5c..5289b9bff903716398050bd1c01cea01e1ff1466 100644 (file)
@@ -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