forked from kubernetes-el/kubernetes-el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes-core.el
122 lines (100 loc) · 4.82 KB
/
kubernetes-core.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
;; kubernetes-core.el --- core functionality -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'magit-section)
(require 's)
(require 'kubernetes-ast)
(require 'kubernetes-vars)
(defvar kubernetes-state--current-state nil)
(defun kubernetes-state ()
kubernetes-state--current-state)
(defun kubernetes--overview-render (state)
(let ((sections (kubernetes-state-overview-sections state)))
`(section (root nil)
,(kubernetes-errors-render state)
,(when (member 'context sections)
(kubernetes-contexts-render state))
,(mapcar (lambda (section)
`(,(intern (if (eq section 'overview)
"aggregated-view"
(format "%s-list" section)))
,state))
(remove 'context sections)))))
(defun kubernetes--redraw-overview-buffer ()
"Redraws the main buffer using the current state."
(when-let (buf (get-buffer kubernetes-overview-buffer-name))
(with-current-buffer buf
;; If a region is active, a redraw would affect the region in
;; unpredictable ways.
(unless (region-active-p)
;; Suppress redrawing if the overview is not selected. This prevents
;; point from jumping around when a magit popup is open.
(when (member (selected-window) (get-buffer-window-list buf))
(kubernetes-utils--save-window-state
(let ((inhibit-read-only t))
(erase-buffer)
(kubernetes-ast-eval (kubernetes--overview-render (kubernetes-state)))))
;; Force the section at point to highlight.
(magit-section-update-highlight))))))
(defun kubernetes--message (format &rest args)
"Call `message' with FORMAT and ARGS.
We `inhibit-message' the message when the cursor is in the
minibuffer and when Emacs version is before Emacs 27 due to the
fact that we often use `kubernetes--info', `kubernetes--warn' and
`kubernetes--error' in async context and the call to these
function is removing the minibuffer prompt. The issue with async
messages is already fixed in Emacs 27."
(when kubernetes-show-message
(let ((inhibit-message (and (minibufferp)
(version< emacs-version "27.0"))))
(apply #'message format args))))
(defun kubernetes--info (format &rest args)
"Display kubernetes info message with FORMAT with ARGS."
(kubernetes--message "%s :: %s" (propertize "k8s" 'face 'success) (apply #'format format args)))
(defun kubernetes--warn (format &rest args)
"Display kubernetes warn message with FORMAT with ARGS."
(kubernetes--message "%s :: %s" (propertize "k8s" 'face 'warning) (apply #'format format args)))
(defun kubernetes--error (format &rest args)
"Display kubernetes error message with FORMAT with ARGS."
(kubernetes--message "%s :: %s" (propertize "k8s" 'face 'error) (apply #'format format args)))
(defun kubernetes--val-from-arg-list (arg-list key)
"Find value for flag KEY in CLI-flag-style ARG-LIST.
Flag-value pairs in ARG-LIST can be either separate or paired with =,
e.g. '(\"--foo\" bar) or '(\"--foo=bar\").
This function expects long flags only.
If ARG-LIST is nil or KEY is not present in ARG-LIST, returns nil."
(when arg-list
(-when-let* ((key-index (--find-index
(s-prefix? (format "--%s" (symbol-name key)) it)
arg-list))
(key-val (nth key-index arg-list)))
(if (s-contains? "=" key-val)
(cadr (s-split "=" key-val))
(nth (+ 1 key-index) arg-list)))))
(defvar kubernetes--poll-timer nil
"Background timer used to poll for updates.
This is used to regularly synchronise local state with Kubernetes.")
(defvar kubernetes--redraw-timer nil
"Background timer used to trigger buffer redrawing.
This is used to display the current state.")
(defun kubernetes--initialize-timers ()
"Initialize kubernetes.el global timers.
Global timers are responsible for overview redrawing and resource
polling according to `kubernetes-redraw-frequency' and
`kubernetes-poll-frequency', respectively."
(unless kubernetes--redraw-timer
(setq kubernetes--redraw-timer (run-with-timer 0 kubernetes-redraw-frequency #'kubernetes-state-trigger-redraw)))
(unless kubernetes--poll-timer
(setq kubernetes--poll-timer (run-with-timer 0 kubernetes-poll-frequency
(lambda ()
(run-hooks 'kubernetes-poll-hook))))))
(defun kubernetes--kill-timers ()
"Kill kubernetes.el global timers."
(when-let (timer kubernetes--redraw-timer)
(cancel-timer timer))
(when-let (timer kubernetes--poll-timer)
(cancel-timer timer))
(setq kubernetes--redraw-timer nil)
(setq kubernetes--poll-timer nil))
(provide 'kubernetes-core)
;;; kubernetes-core.el ends here