-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
345 lines (313 loc) · 13.1 KB
/
ui.py
File metadata and controls
345 lines (313 loc) · 13.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from __future__ import annotations
import json
from collections.abc import Callable, Sequence
from contextlib import nullcontext
from typing import Any, TypeVar
import questionary
from prompt_toolkit import prompt
from prompt_toolkit.application.current import get_app_or_none
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.formatted_text import HTML
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from o2switch_cli.cli.interactive_support import PageWindow, SearchSuggestion, paginate_items
from o2switch_cli.core.models import (
DNSRecord,
DomainDescriptor,
ErrorEnvelope,
HostnameSearchResult,
MutationPlan,
OperationResult,
SubdomainDescriptor,
)
T = TypeVar("T")
class SearchSuggestionCompleter(Completer):
def __init__(self, suggestions: Sequence[SearchSuggestion], *, limit: int = 8) -> None:
self._suggestions = list(suggestions)
self._limit = limit
def match_count(self, query: str) -> int:
needle = query.strip().lower()
if not needle:
return len(self._suggestions)
return sum(1 for item in self._suggestions if needle in (item.search_blob or item.label.lower()))
def get_completions(self, document, complete_event):
query = document.text_before_cursor
needle = query.strip().lower()
matches = [
item
for item in self._suggestions
if not needle or needle in (item.search_blob or item.label.lower())
]
matches.sort(
key=lambda item: (
0
if not needle or item.label.lower().startswith(needle) or item.value.lower().startswith(needle)
else 1,
len(item.label),
item.label,
)
)
start_position = -len(query)
for item in matches[: self._limit]:
yield Completion(
item.value,
start_position=start_position,
display=item.label,
display_meta=item.meta or "",
)
class TerminalUI:
def __init__(self, console: Console | None = None, output_format: str = "text") -> None:
self.console = console or Console()
self.output_format = output_format
def print_banner(self) -> None:
title = Text("O2SWITCH CLI", style="bold #ffb000")
body = (
"[bold #ffd166]DNS-first cPanel operator console[/]\n"
"[dim]Domains, hosted subdomains, and zone changes without blind writes.[/dim]"
)
self.console.print(
Panel.fit(
body,
title=title,
border_style="#ffb000",
box=box.DOUBLE_EDGE,
padding=(1, 2),
)
)
def status(self, message: str, *, spinner: str = "dots12"):
if self.output_format == "json":
return nullcontext()
return self.console.status(f"[bold #ffb000]{message}[/]", spinner=spinner, spinner_style="#ffb000")
def _print_json(self, payload: Any) -> None:
self.console.print_json(json=json.dumps(payload, default=str))
@staticmethod
def _page_caption(page_window: PageWindow[Any] | None) -> str | None:
if page_window is None or page_window.total_items <= page_window.page_size:
return None
start = page_window.start_index + 1 if page_window.total_items else 0
return (
f"Page {page_window.page}/{page_window.total_pages} · "
f"showing {start}-{page_window.end_index} of {page_window.total_items}"
)
def print_error(self, envelope: ErrorEnvelope) -> None:
if self.output_format == "json":
self._print_json(envelope.model_dump(mode="json"))
return
content = (
f"[bold red]{envelope.message}[/]\n"
f"[dim]operation[/] {envelope.operation}\n"
f"[dim]target[/] {envelope.target or '-'}\n"
f"[dim]next[/] {envelope.safe_next_step}"
)
self.console.print(Panel(content, title="Failure", border_style="red", box=box.DOUBLE))
def _build_domains_table(
self, domains: Sequence[DomainDescriptor], page_window: PageWindow[DomainDescriptor] | None = None
) -> Table:
table = Table(title="Domains", box=box.HEAVY_HEAD, border_style="#ffb000")
table.caption = self._page_caption(page_window)
table.add_column("Domain", style="bold")
table.add_column("Type")
table.add_column("Hosted Create", justify="center")
table.add_column("DNS Zone", justify="center")
for item in domains:
table.add_row(
item.domain,
item.type.value,
"yes" if item.eligible_for_subdomain_creation else "no",
"yes" if item.has_dns_zone else "no",
)
return table
def print_domains(
self, domains: list[DomainDescriptor], page_window: PageWindow[DomainDescriptor] | None = None
) -> None:
if self.output_format == "json":
self._print_json([item.model_dump(mode="json") for item in domains])
return
table = self._build_domains_table(domains, page_window)
self.console.print(table)
def _build_records_table(
self,
records: Sequence[DNSRecord],
page_window: PageWindow[DNSRecord] | None = None,
) -> Table:
table = Table(title="DNS Records", box=box.SIMPLE_HEAVY, border_style="cyan")
table.caption = self._page_caption(page_window)
table.add_column("Hostname", style="bold")
table.add_column("Type")
table.add_column("Value")
table.add_column("TTL", justify="right")
table.add_column("Zone")
for item in records:
table.add_row(item.name, item.type, item.value or "-", str(item.ttl or "-"), item.zone)
return table
def print_records(self, records: list[DNSRecord], page_window: PageWindow[DNSRecord] | None = None) -> None:
if self.output_format == "json":
self._print_json([item.model_dump(mode="json") for item in records])
return
table = self._build_records_table(records, page_window)
self.console.print(table)
def _build_hostname_search_table(
self, results: Sequence[HostnameSearchResult], page_window: PageWindow[HostnameSearchResult] | None = None
) -> Table:
table = Table(title="Hostname Search", box=box.SIMPLE_HEAVY, border_style="cyan")
table.caption = self._page_caption(page_window)
table.add_column("Category", style="bold")
table.add_column("Hostname")
table.add_column("Type")
table.add_column("Value")
table.add_column("Zone")
table.add_column("Hosted")
for item in results:
table.add_row(
item.category.value,
item.hostname,
item.record_type or "-",
item.value or item.docroot or "-",
item.zone or "-",
"yes" if item.managed_by_cpanel else "no",
)
return table
def print_hostname_search_results(
self, results: list[HostnameSearchResult], page_window: PageWindow[HostnameSearchResult] | None = None
) -> None:
if self.output_format == "json":
self._print_json([item.model_dump(mode="json") for item in results])
return
table = self._build_hostname_search_table(results, page_window)
self.console.print(table)
def _build_subdomains_table(
self, subdomains: Sequence[SubdomainDescriptor], page_window: PageWindow[SubdomainDescriptor] | None = None
) -> Table:
table = Table(title="Hosted Subdomains", box=box.ROUNDED, border_style="magenta")
table.caption = self._page_caption(page_window)
table.add_column("FQDN", style="bold")
table.add_column("Root Domain")
table.add_column("Docroot")
for item in subdomains:
table.add_row(item.fqdn, item.root_domain, item.docroot or "-")
return table
def print_subdomains(
self, subdomains: list[SubdomainDescriptor], page_window: PageWindow[SubdomainDescriptor] | None = None
) -> None:
if self.output_format == "json":
self._print_json([item.model_dump(mode="json") for item in subdomains])
return
table = self._build_subdomains_table(subdomains, page_window)
self.console.print(table)
def print_mapping(self, title: str, payload: dict[str, Any]) -> None:
if self.output_format == "json":
self._print_json(payload)
return
table = Table(title=title, box=box.MINIMAL_DOUBLE_HEAD, border_style="#ffb000")
table.add_column("Key", style="bold")
table.add_column("Value")
for key, value in payload.items():
table.add_row(key, json.dumps(value) if isinstance(value, (dict, list)) else str(value))
self.console.print(table)
def print_plan(self, plan: MutationPlan, zone: str | None = None) -> None:
if self.output_format == "json":
payload = plan.model_dump(mode="json")
if zone:
payload["zone"] = zone
self._print_json(payload)
return
lines = [f"[bold]action[/] {plan.planned_action.value}", f"[bold]summary[/] {plan.summary}"]
if zone:
lines.append(f"[bold]zone[/] {zone}")
if plan.before:
lines.append(f"[bold]before[/] {json.dumps(plan.before, default=str)}")
if plan.after:
lines.append(f"[bold]after[/] {json.dumps(plan.after, default=str)}")
self.console.print(Panel("\n".join(lines), title="Planned Change", border_style="yellow", box=box.DOUBLE_EDGE))
def print_result(self, result: OperationResult) -> None:
if self.output_format == "json":
self._print_json(result.model_dump(mode="json"))
return
style = "green" if result.action not in {"dry-run", "no-op"} else "cyan"
body = (
f"[bold]operation[/] {result.operation}\n"
f"[bold]target[/] {result.target}\n"
f"[bold]zone[/] {result.zone or '-'}\n"
f"[bold]action[/] {result.action}\n"
f"[bold]verification[/] {result.verification.value}\n"
f"[bold]message[/] {result.message}\n"
f"[dim]correlation[/] {result.correlation_id}"
)
self.console.print(Panel(body, title="Result", border_style=style, box=box.DOUBLE))
def confirm(self, prompt: str) -> bool:
return bool(questionary.confirm(prompt, default=False).ask())
def prompt_realtime_search(
self,
prompt_text: str,
*,
suggestions: Sequence[SearchSuggestion],
help_text: str,
) -> str:
if self.output_format == "json" or not suggestions:
return (questionary.text(prompt_text).ask() or "").strip()
completer = SearchSuggestionCompleter(suggestions)
def toolbar() -> HTML:
app = get_app_or_none()
query = app.current_buffer.text if app else ""
count = completer.match_count(query)
return HTML(
"<style fg='ansibrightblack'>"
f"{help_text} · {count} live matches · Enter keeps free text"
"</style>"
)
return (
prompt(
f"{prompt_text}: ",
completer=completer,
complete_while_typing=True,
complete_in_thread=True,
mouse_support=True,
reserve_space_for_menu=min(10, max(4, len(suggestions))),
bottom_toolbar=toolbar,
)
.strip()
)
def browse_pages(
self,
items: Sequence[T],
*,
page_size: int,
empty_message: str,
render_page: Callable[[list[T], PageWindow[T]], None],
) -> None:
if not items:
self.print_info(empty_message)
return
page = 1
while True:
window = paginate_items(items, page=page, page_size=page_size)
self.console.clear()
self.print_banner()
render_page(window.items, window)
if window.total_pages <= 1:
return
choices: list[str] = []
if window.page > 1:
choices.append("Previous page")
if window.page < window.total_pages:
choices.append("Next page")
choices.extend(["First page", "Last page", "Close results"])
action = questionary.select(
f"Browse results ({window.page}/{window.total_pages})",
choices=choices,
).ask()
if action == "Previous page":
page -= 1
elif action == "Next page":
page += 1
elif action == "First page":
page = 1
elif action == "Last page":
page = window.total_pages
else:
return
def print_info(self, message: str) -> None:
self.console.print(Panel.fit(message, border_style="cyan", box=box.SQUARE))