-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
65 lines (51 loc) · 1.76 KB
/
Copy pathcli.py
File metadata and controls
65 lines (51 loc) · 1.76 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
"""CLI entry point for the Binance Futures trading bot."""
import os
import sys
import traceback
import typer
from rich.console import Console
from bot.app import startup_and_run
from bot.config import ENV_PATH, ENV_TEMPLATE
from bot.logging_config import LOG_DIR, LOG_PATH
console = Console()
app = typer.Typer(name="trading-bot", no_args_is_help=False)
def _ensure_env_example() -> None:
"""Create .env.example if missing."""
example = ENV_PATH.with_suffix(".env.example")
if not example.exists():
example.write_text(ENV_TEMPLATE.lstrip("\n"))
console.print("[dim]Created .env.example[/dim]")
def _ensure_gitignore() -> None:
"""Add .env to .gitignore if not already present."""
gi = ENV_PATH.parent / ".gitignore"
if not gi.exists():
gi.write_text("# Environment\n.env\n")
console.print("[dim]Created .gitignore with .env entry[/dim]")
return
content = gi.read_text()
if ".env" not in content:
gi.write_text(content.rstrip("\n") + "\n\n# Environment\n.env\n")
console.print("[dim]Appended .env to .gitignore[/dim]")
@app.command()
def run() -> None:
"""Launch the Binance Futures Trading Bot."""
_ensure_env_example()
_ensure_gitignore()
try:
startup_and_run()
except typer.Exit:
raise
except Exception:
os.makedirs(LOG_DIR, exist_ok=True)
with open(LOG_PATH, "a") as f:
f.write(f"Unhandled error:\n{traceback.format_exc()}")
console.print("\n[red]✖ An unexpected error occurred.[/red]")
console.print(
f"[dim]Details written to {LOG_PATH}. "
"No traceback shown to protect UX.[/dim]"
)
sys.exit(1)
def main() -> None:
app()
if __name__ == "__main__":
main()