]> git.eshelyaron.com Git - emacs.git/commitdiff
kubed.el: Minor cleanups
authorEshel Yaron <me@eshelyaron.com>
Fri, 26 Jul 2024 08:52:14 +0000 (10:52 +0200)
committerEshel Yaron <me@eshelyaron.com>
Fri, 26 Jul 2024 08:52:14 +0000 (10:52 +0200)
lisp/net/kubed.el

index c43d94b2783fae4ba6d3076c73d9e8ea8942412a..84a16225dca48fea7420773d7cebd851f31e0f23 100644 (file)
@@ -832,6 +832,7 @@ Optional argument DEFAULT is the minibuffer default argument." resource)
 (kubed-define-resource pod
     ((phase ".status.phase" 10) (starttime ".status.startTime" 20))
   :prefix ("L" #'kubed-logs
+           "A" #'kubed-attach
            "F" #'kubed-forward-port-to-pod)
   (dired "C-d" "Start Dired in home directory of first container of"
          (let ((ns (when k8sns (concat "%" k8sns))))
@@ -840,6 +841,9 @@ Optional argument DEFAULT is the minibuffer default argument." resource)
          (let* ((ns (when k8sns (concat "%" k8sns)))
                 (default-directory (concat "/kubernetes:" pod ns ":")))
            (shell (format "*kubed-pod-%s-shell*" pod))))
+  (attach "a" "Attach to remote process running on"
+          (kubed-attach pod (kubed-read-container pod "Container" t k8sns)
+                        k8sns t t))
   (logs "l" "Show logs for a container of"
         (kubed-logs pod (kubed-read-container pod "Container" t k8sns)))
   (forward-port "F" "Forward local network port to remote port of"
@@ -942,9 +946,7 @@ optional command to run in the images."
           (port nil)
           (command nil)
           (namespace nil))
-      (dolist (arg (when (and (fboundp 'transient-args)
-                              (fboundp 'kubed-transient-create-deployment))
-                     (transient-args 'kubed-transient-create-deployment)))
+      (dolist (arg (kubed-transient-args 'kubed-transient-create-deployment))
         (cond
          ((string-match "--replicas=\\(.+\\)" arg)
           (setq replicas (string-to-number (match-string 1 arg))))
@@ -1108,13 +1110,11 @@ completion candidates."
 (defun kubed-create (definition &optional kind)
   "Create Kubernetes resource of kind KIND with definition DEFINITION."
   (interactive
-   (list (or (and (fboundp 'transient-args)
-                  (fboundp 'kubed-transient-create-deployment)
-                  (seq-some
-                   (lambda (arg)
-                     (when (string-match "--filename=\\(.+\\)" arg)
-                       (match-string 1 arg)))
-                   (transient-args 'kubed-transient-create)))
+   (list (or (seq-some
+              (lambda (arg)
+                (when (string-match "--filename=\\(.+\\)" arg)
+                  (match-string 1 arg)))
+              (kubed-transient-args 'kubed-transient-create))
              (kubed-read-resource-definition-file-name))))
   (let ((kind (or kind "resource")))
     (message "Creating Kubernetes %s with definition `%s'..." kind definition)
@@ -1145,9 +1145,7 @@ providing it with arguments."
    (let ((pod (read-string "Run image in pod with name: "))
          (image nil) (port nil) (namespace nil) (attach nil) (stdin nil)
          (tty nil) (rm nil) (envs nil) (command nil) (args nil))
-     (dolist (arg (when (and (fboundp 'transient-args)
-                             (fboundp 'kubed-transient-run))
-                    (transient-args 'kubed-transient-run)))
+     (dolist (arg (kubed-transient-args 'kubed-transient-run))
        (cond
         ((string-match "--image=\\(.+\\)" arg)
          (setq image (match-string 1 arg)))
@@ -1346,6 +1344,17 @@ Optional argument DEFAULT is the minibuffer default argument."
    (format-prompt prompt default) nil nil nil nil
    'kubed-container-images-history default))
 
+(defvar transient-current-command)
+
+(defun kubed-transient-args (&optional prefix)
+  "Return current arguments from transient PREFIX.
+
+If PREFIX nil, it defaults to the value of `transient-current-command'."
+  (when-let ((prefix (or prefix (bound-and-true-p transient-current-command))))
+    (and (featurep 'kubed-transient)
+         (fboundp 'transient-args)
+         (transient-args prefix))))
+
 ;;;###autoload
 (defun kubed-attach (pod &optional container namespace stdin tty)
   "Attach to running process in CONTAINER in Kubernetes POD.
@@ -1353,12 +1362,17 @@ Optional argument DEFAULT is the minibuffer default argument."
 Optional argument NAMESPACE is the namespace in which to look for POD.
 pod/foobar created
 Non-nil STDIN says to connect local standard input to remote process.
-Non-nil TTY says to use a TTY for standard input."
+Non-nil TTY says to use a TTY for standard input.
+
+Interactively, prompt for POD; if there are multiple pod containers,
+prompt for CONTAINER as well; STDIN is t unless you call this command
+with \\[universal-argument] \\[universal-argument]; and TTY is t unless\
+ you call this command with \\[universal-argument]."
   (interactive
-   (let ((namespace nil) (stdin nil) (tty nil))
-     (dolist (arg (when (and (fboundp 'transient-args)
-                             (fboundp 'kubed-transient-attach))
-                    (transient-args 'kubed-transient-attach)))
+   (let ((namespace nil) (stdin t) (tty t))
+     (when (<= 4  (prefix-numeric-value current-prefix-arg)) (setq tty   nil))
+     (when (<= 16 (prefix-numeric-value current-prefix-arg)) (setq stdin nil))
+     (dolist (arg (kubed-transient-args 'kubed-transient-attach))
        (cond
         ((string-match "--namespace=\\(.+\\)" arg)
          (setq namespace (match-string 1 arg)))
@@ -1372,7 +1386,7 @@ Non-nil TTY says to use a TTY for standard input."
        ;; FIXME: When namespace is set from transient prefix, read pod
        ;; name in that namespace instead.
        (let* ((p (kubed-read-pod "Attach to pod"))
-              (c (kubed-read-container p "Container" t)))
+              (c (kubed-read-container p "Container" t namespace)))
          (list p c namespace stdin tty)))))
   (pop-to-buffer
    (apply #'make-comint "kubed-attach" kubed-kubectl-program nil
@@ -1417,8 +1431,6 @@ argument) says to include managed fields in the comparison."
 (defvar kubed-kubectl-command-history nil
   "Minibuffer history for `kubed-kubectl-command'.")
 
-(defvar transient-current-command)
-
 ;;;###autoload
 (defun kubed-kubectl-command (command)
   "Execute `kubectl' COMMAND.
@@ -1431,16 +1443,12 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments."
           "Command: "
           (concat
            kubed-kubectl-program " "
-           (when-let
-               ((args
-                 (and (fboundp 'transient-args)
-                      (fboundp 'transient-scope)
-                      transient-current-command
-                      (string-join
-                       (append (transient-scope)
-                               (transient-args transient-current-command))
-                       " "))))
-             (concat args " ")))
+           (when-let ((args (kubed-transient-args)))
+             (concat (string-join
+                      (append (and (fboundp 'transient-scope) (transient-scope))
+                              args)
+                      " ")
+                     " ")))
           'kubed-kubectl-command-history)))
   (shell-command command))
 
@@ -1460,7 +1468,6 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments."
   "+" #'kubed-create
   "R" #'kubed-run
   "D" #'kubed-diff
-  "@" #'kubed-attach
   "!" #'kubed-kubectl-command)
 
 (provide 'kubed)