forked from kubernetes-el/kubernetes-el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes-popups.el
154 lines (128 loc) · 5.6 KB
/
kubernetes-popups.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;;; kubernetes-popups.el --- Magit popups for Kubernetes buffers. -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'magit-popup)
(require 'transient)
(require 'kubernetes-contexts)
(require 'kubernetes-process)
(require 'kubernetes-state)
(require 'kubernetes-utils)
(defgroup kubernetes nil
"Emacs porcelain for Kubernetes."
:group 'tools
:prefix "kubernetes-")
;; Utilities
(defun kubernetes-popups--read-existing-file (prompt &optional default)
(read-file-name prompt nil default t nil #'file-regular-p))
(defun kubernetes-popups--read-server-and-port (&optional _option defaults)
(-let* (((server port) (split-string (or defaults "") ":" t))
(port (or (ignore-errors (string-to-number port)) 8080)))
(format "%s:%s" (read-string "Server: " server) (read-number "Port: " port))))
;; Popup definitions
(transient-define-prefix kubernetes-proxy ()
[["Connection"
("=p" "Port" "--port=" read-string)]]
[["Actions"
;; TODO: Update this label dynamically based on current proxy status
("P" "Enable/disable" kubernetes-proxy-toggle)]])
(defun kubernetes-proxy-toggle (enable-disable args)
"Enable/disable kubectl proxy according to ENABLE-DISABLE, using ARGS.
If disabling the proxy, ARGS is ignored."
(interactive (list (not (proxy-active-p kubernetes--global-process-ledger))
(transient-args 'kubernetes-proxy)))
(if enable-disable
(get-proxy-process kubernetes--global-process-ledger args)
(kill-proxy-process kubernetes--global-process-ledger)))
(transient-define-prefix kubernetes-dispatch ()
[["Environment"
("c" "Configuration" kubernetes-config-popup)]
["Marks"
("D" "Delete" kubernetes-mark-for-delete)
("u" "Unmark" kubernetes-unmark)
("U" "Unmark (all)" kubernetes-unmark-all)]
["Commands"
("d" "Describe" kubernetes-describe)
("E" "Edit" kubernetes-edit)
("e" "Exec" kubernetes-exec)
("f" "File" kubernetes-file)
("L" "Labels" kubernetes-labels)
("l" "Logs" kubernetes-logs)
("P" "Proxy" kubernetes-proxy)]])
(transient-define-prefix kubernetes-exec ()
"Execute into Kubernetes resources."
:value '("--stdin" "--tty")
["Switches"
("-i" "Pass stdin to container" "--stdin")
("-t" "Stdin is a TTY" "--tty")]
["Options"
("=c" "Container to exec within" "--container=" :reader kubernetes-utils-read-container-name)]
[["Actions"
("e" "Exec" kubernetes-exec-into)
("v" "Exec into container using vterm" kubernetes-exec-using-vterm
:inapt-if-not (lambda () (require 'vterm nil 'noerror)))]])
(transient-define-prefix kubernetes-file ()
"Work with files in Kubernetes resources."
[["Options"
;; TODO: This doesn't currently get picked up by the suffixes
("=c" "Container" "--container=" kubernetes-utils-read-container-name)]]
[["Actions"
("f" "Find file" kubernetes-tramp-find-file)
("d" "Dired" kubernetes-tramp-dired)]])
(transient-define-prefix kubernetes-describe ()
"Describe Kubernetes resources."
[["Actions"
("d" "Dwim" kubernetes-describe-dwim)
("p" "Pod" kubernetes-describe-pod)]])
(transient-define-prefix kubernetes-labels ()
"Act on Kubernetes labels."
[["Actions"
("p" "Pods" kubernetes-show-pods-for-label)]])
(transient-define-prefix kubernetes-edit ()
"Edit Kubernetes resources."
[["Actions"
("e" "Dwim" kubernetes-edit-resource-dwim)]])
(transient-define-prefix kubernetes-context ()
"Work with kubectl contexts."
[["Actions"
("r" "Rename a context" kubernetes-contexts-rename)
;; TODO: This suffix descriptor could be a little more colorful,
;; e.g. "Change from context <current-context-name> to...", but we can
;; improve later
("c" "Change current context" kubernetes-contexts-use-context)]])
;; Config popup
;;
;; The macro definition is expanded here and modified to support marshalling
;; state between the state manager and the magit popup's global state.
(defconst kubernetes-config-popup
(list :group 'kubernetes
:variable 'kubernetes-kubectl-flags
:options
`("Configuration"
(?f "Kubeconfig file" "--kubeconfig=" kubernetes-popups--read-existing-file)
(?l "Cluster name in config" "--cluster=" read-string)
(?s "Server address and port" "--server=" kubernetes-popups--read-server-and-port)
(?u "User in kubeconfig" "--user=" read-string)
"Authentication"
(?a "CA cert file" "--certificate-authority=" kubernetes-popups--read-existing-file)
(?k "Client key file" "--client-key=" kubernetes-popups--read-existing-file)
(?r "Client cert file" "--client-certificate=" kubernetes-popups--read-existing-file)
(?t "API token" "--token=" read-string))
:actions
'((?c "Change context" kubernetes-contexts-use-context)
(?n "Set namespace" kubernetes-set-namespace))))
(defun kubernetes-popups--update-kubectl-state ()
;; HACK: Need to use internal magit vars, since this is run inside the popup
;; refresh hook.
(when (eq magit-this-popup 'kubernetes-config-popup)
(kubernetes-state-update-kubectl-flags (magit-popup-get-args))))
(defun kubernetes-config-popup (&optional arg)
"Popup console for showing an overview of available config commands.
With ARG, execute default command."
(interactive "P")
(let ((flags (kubernetes-state-kubectl-flags (kubernetes-state))))
(setq kubernetes-kubectl-flags flags))
(add-hook 'magit-refresh-popup-buffer-hook #'kubernetes-popups--update-kubectl-state)
(magit-invoke-popup 'kubernetes-config-popup nil arg))
(magit-define-popup-keys-deferred 'kubernetes-config-popup)
(provide 'kubernetes-popups)
;;; kubernetes-popups.el ends here