Initial commit

This commit is contained in:
Digital Studium 2024-04-07 16:48:02 +03:00
commit 4fce2c7930
1 changed files with 97 additions and 0 deletions

97
kls Executable file
View File

@ -0,0 +1,97 @@
#!/usr/bin/python3
import curses
import subprocess
stdscr = curses.initscr()
class Menu:
def __init__(self, name, rows, begin_x):
self.name = name # заголовок окна
self.rows = rows
self.begin_x = begin_x
self.win = curses.newwin(curses.LINES, curses.COLS // 3, 0, begin_x)
self.win.box()
self.win.addstr(1, 2, self.name)
for index, row in enumerate(self.rows):
self.win.addstr(index + 3, 2, row)
self.row = 0
# рисуем первое меню
## готовим контент
bytes_list = subprocess.check_output("kubectl get ns --no-headers -o template='{{range .items}}{{.metadata.name}} {{end}}'", shell=True).split()
namespaces = [bytes_list[i].decode('utf-8') for i in range(len(bytes_list))]
## отрисовываем меню
menu1 = Menu("Namespaces", namespaces, 0)
# рисуем второе меню
## готовим контент
api_resources = ["pods", "services", "deployments", "ingresses"]
## отрисовываем меню
menu2 = Menu("API resources", api_resources, 0 + curses.COLS // 3)
# рисуем третье меню
## готовим контент
bytes_list = subprocess.check_output("kubectl get pods -n kube-system --no-headers -o template='{{range .items}}{{.metadata.name}} {{end}}'", shell=True).split()
pods = [bytes_list[i].decode('utf-8') for i in range(len(bytes_list))]
## отрисовываем меню
menu3 = Menu("Resources", pods, 0 + curses.COLS // 3 * 2)
menus = [menu1, menu2, menu3]
def main(stdscr):
stdscr.refresh()
running = True
current_menu = 0
menus[current_menu].win.addstr(1, 2, menus[current_menu].name, curses.A_REVERSE | curses.A_ITALIC)
for menu in menus:
menu.win.addstr(3, 2, menu.rows[0], curses.A_REVERSE | curses.A_ITALIC)
while running:
[menu.win.refresh() for menu in menus] # refresh all menus
the_user_presses_the_key_et_voila = stdscr.getkey()
match the_user_presses_the_key_et_voila:
case 'q':
running = False
case '\t' | "KEY_RIGHT":
menus[current_menu].win.addstr(1, 2, menus[current_menu].name)
current_menu = (current_menu + 1) % 3
menus[current_menu].win.addstr(1, 2, menus[current_menu].name, curses.A_REVERSE | curses.A_ITALIC)
case "KEY_BTAB" | "KEY_LEFT":
menus[current_menu].win.addstr(1, 2, menus[current_menu].name)
current_menu = (current_menu - 1) % 3
menus[current_menu].win.addstr(1, 2, menus[current_menu].name, curses.A_REVERSE | curses.A_ITALIC)
case "KEY_DOWN":
menu = menus[current_menu]
menu.win.addstr(menu.row + 3, 2, menu.rows[menu.row]) # удаляем выделение с текущей строки
menu.row = (menu.row + 1) % len(menu.rows) # переходим к следующей строке
menu.win.addstr(menu.row + 3, 2, menu.rows[menu.row], curses.A_REVERSE | curses.A_ITALIC) # и выделяем её
if current_menu != 2: # если изменился выбор нэймспейса или апи ресурса
namespace = menus[0].rows[menus[0].row]
api_resource = menus[1].rows[menus[1].row]
command = f"kubectl get {api_resource} -n {namespace} " + "--no-headers -o template='{{range .items}}{{.metadata.name}} {{end}}'"
#raise ValueError(command)
bytes_list = subprocess.check_output(command, shell=True).split()
resources = [bytes_list[i].decode('utf-8') for i in range(len(bytes_list))]
if not resources:
resources = [f"No resources found in {namespace} namespace.",]
menus[2].win.clear()
menus[2].win.box()
menus[2].win.addstr(1, 2, menus[2].name)
menus[2].rows = resources
menus[2].row = 0
for index, row in enumerate(menus[2].rows):
if index == 0 and not resources[0].startswith("No resources"):
menus[2].win.addstr(index + 3, 2, row, curses.A_REVERSE | curses.A_ITALIC)
else:
menus[2].win.addstr(index + 3, 2, row)
case "KEY_UP":
pass
curses.wrapper(main)
#curses.nocbreak()
#stdscr.keypad(False)
#curses.echo()
#curses.endwin()