clean token validation code
This commit is contained in:
parent
5dbcac68b6
commit
e37d8b2ab0
1 changed files with 6 additions and 65 deletions
|
|
@ -1,8 +1,5 @@
|
|||
import sys
|
||||
import asyncio
|
||||
import select
|
||||
import termios
|
||||
import tty
|
||||
from rich.console import Console
|
||||
from rich.prompt import Prompt, Confirm
|
||||
from rich.panel import Panel
|
||||
|
|
@ -26,34 +23,7 @@ class MigrationCLI:
|
|||
self.progress_callback_task = None
|
||||
self.tokens_valid = False
|
||||
|
||||
async def _check_skip(self):
|
||||
"""Non-blocking check for 'S' key press."""
|
||||
if not sys.stdin.isatty():
|
||||
return False
|
||||
|
||||
fd = sys.stdin.fileno()
|
||||
old_settings = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setcbreak(fd)
|
||||
while True:
|
||||
if select.select([sys.stdin], [], [], 0.05)[0]:
|
||||
char = sys.stdin.read(1).lower()
|
||||
if char == 's':
|
||||
return True
|
||||
await asyncio.sleep(0.05)
|
||||
except Exception:
|
||||
return False
|
||||
finally:
|
||||
# Shift back to old settings immediately
|
||||
termios.tcsetattr(fd, termios.TCSANOW, old_settings)
|
||||
|
||||
async def validate_config(self):
|
||||
console.print("[yellow]press (S) to skip[/yellow]")
|
||||
|
||||
# Save terminal settings for safety
|
||||
fd = sys.stdin.fileno() if sys.stdin.isatty() else None
|
||||
old_settings = termios.tcgetattr(fd) if fd is not None else None
|
||||
|
||||
self.validation_results = {
|
||||
"discord_token": False, "discord_bot_name": None,
|
||||
"discord_server": False, "discord_server_name": None,
|
||||
|
|
@ -64,33 +34,16 @@ class MigrationCLI:
|
|||
|
||||
discord_task = asyncio.create_task(self.engine.discord_reader.validate())
|
||||
fluxer_task = asyncio.create_task(self.engine.fluxer_writer.validate())
|
||||
skip_task = asyncio.create_task(self._check_skip())
|
||||
|
||||
try:
|
||||
with console.status("[yellow]Validating tokens...[/yellow]"):
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
done, pending = await asyncio.wait(
|
||||
[discord_task, fluxer_task, skip_task],
|
||||
timeout=5.0,
|
||||
return_when=asyncio.FIRST_COMPLETED
|
||||
[discord_task, fluxer_task],
|
||||
timeout=3.0,
|
||||
return_when=asyncio.ALL_COMPLETED
|
||||
)
|
||||
|
||||
if skip_task in done and skip_task.result():
|
||||
console.print("[yellow]Validation skipped by user.[/yellow]")
|
||||
for t in [discord_task, fluxer_task]: t.cancel()
|
||||
return
|
||||
|
||||
# Wait a bit more for validations if they are almost done
|
||||
elapsed = asyncio.get_event_loop().time() - start_time
|
||||
remaining = max(0, 5.0 - elapsed)
|
||||
if not (discord_task.done() and fluxer_task.done()) and remaining > 0:
|
||||
done2, pending2 = await asyncio.wait(
|
||||
[discord_task, fluxer_task],
|
||||
timeout=remaining,
|
||||
return_when=asyncio.ALL_COMPLETED
|
||||
)
|
||||
done.update(done2)
|
||||
|
||||
# Process Discord Result
|
||||
if discord_task in done:
|
||||
try:
|
||||
|
|
@ -106,7 +59,7 @@ class MigrationCLI:
|
|||
except Exception as e:
|
||||
console.print(f"[bold red]Discord validation failed with error: {e}[/bold red]")
|
||||
else:
|
||||
console.print("[bold red]Discord bot token validation timed out after 5 seconds.[/bold red]")
|
||||
console.print("[bold red]Discord bot token validation timed out after 3 seconds.[/bold red]")
|
||||
discord_task.cancel()
|
||||
|
||||
# Process Fluxer Result
|
||||
|
|
@ -124,7 +77,7 @@ class MigrationCLI:
|
|||
except Exception as e:
|
||||
console.print(f"[bold red]Fluxer validation failed with error: {e}[/bold red]")
|
||||
else:
|
||||
console.print("[bold red]Fluxer bot token validation timed out after 5 seconds.[/bold red]")
|
||||
console.print("[bold red]Fluxer bot token validation timed out after 3 seconds.[/bold red]")
|
||||
fluxer_task.cancel()
|
||||
|
||||
self.tokens_valid = all(self.validation_results.values())
|
||||
|
|
@ -132,19 +85,7 @@ class MigrationCLI:
|
|||
except Exception as e:
|
||||
console.print(f"[bold red]Validation system failure: {e}[/bold red]")
|
||||
finally:
|
||||
# Crucial: Ensure the skip check task is dead and terminal is restored
|
||||
if not skip_task.done():
|
||||
skip_task.cancel()
|
||||
try:
|
||||
await skip_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# Additional safety: manually restore terminal if somehow it's still weird
|
||||
if fd is not None and old_settings is not None:
|
||||
termios.tcsetattr(fd, termios.TCSANOW, old_settings)
|
||||
|
||||
# Ensure other tasks are also cleaned up
|
||||
# Ensure tasks are cleaned up
|
||||
for t in [discord_task, fluxer_task]:
|
||||
if not t.done(): t.cancel()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue