|
1 | | -"""CLI entrypoint for `auralogger` (mirrors node/src/bin/auralogger.ts).""" |
| 1 | +"""CLI entrypoint for `auralogger` — parity with node/src/cli/bin/auralogger.ts.""" |
2 | 2 |
|
| 3 | +import random |
3 | 4 | import sys |
4 | | -from typing import TextIO |
5 | | - |
6 | | -from auralogger.cli.cli_load_env import load_cli_env_files |
| 5 | +from typing import List, TextIO |
| 6 | + |
| 7 | +from auralogger.cli.aside_pools import ( |
| 8 | + BIN_UNKNOWN_COMMAND_TEMPLATES, |
| 9 | + BIN_USAGE_ASIDES, |
| 10 | + BIN_USAGE_LEGENDARY_ASIDES, |
| 11 | + BIN_USAGE_RARE_MULTI_ASIDES, |
| 12 | + CLI_VETERAN_USAGE_ASIDES, |
| 13 | + DEFAULT_SILENCE_ASIDE_CHANCE, |
| 14 | + ENV_SETUP_RECOVERY_ASIDES, |
| 15 | + WOLVERINE_NUDGE_ASIDES, |
| 16 | + classify_error_for_aside, |
| 17 | + format_aside_template, |
| 18 | + pick_adaptive_fatal_aside, |
| 19 | + pick_aside, |
| 20 | + pick_tiered_aside, |
| 21 | +) |
| 22 | +from auralogger.cli.cli_load_env import ensure_utf8_stdio, load_cli_env_files |
| 23 | +from auralogger.cli.cli_personality_state import ( |
| 24 | + get_consecutive_failures, |
| 25 | + get_total_successful_commands, |
| 26 | + note_command_dispatch, |
| 27 | + record_cli_failure, |
| 28 | + record_cli_success, |
| 29 | +) |
| 30 | +from auralogger.cli.cli_style import bold, bold_hex, dim, hex_color, red, red_bold, white |
| 31 | +from auralogger.cli.cli_tone import maybe_print_generic_spice, print_aside, print_aside_maybe |
7 | 32 | from auralogger.cli.commands.client_check import run_client_check |
8 | 33 | from auralogger.cli.commands.get_logs_cmd import run_get_logs_command |
9 | 34 | from auralogger.cli.commands.init import run_init |
10 | 35 | from auralogger.cli.commands.server_check import run_server_check |
11 | 36 | from auralogger.cli.commands.test_clientlog import run_test_clientlog |
12 | 37 | from auralogger.cli.commands.test_serverlog import run_test_serverlog |
13 | 38 |
|
| 39 | +KNOWN_COMMANDS = { |
| 40 | + "init", |
| 41 | + "get-logs", |
| 42 | + "server-check", |
| 43 | + "client-check", |
| 44 | + "test-serverlog", |
| 45 | + "test-clientlog", |
| 46 | +} |
| 47 | + |
14 | 48 |
|
15 | 49 | def print_usage(stream: TextIO = sys.stdout) -> None: |
16 | | - print("Usage:", file=stream) |
17 | | - print(" auralogger init", file=stream) |
18 | | - print(" auralogger server-check", file=stream) |
19 | | - print(" auralogger client-check", file=stream) |
20 | | - print(" auralogger test-serverlog", file=stream) |
21 | | - print(" auralogger test-clientlog", file=stream) |
22 | | - print(" auralogger get-logs [filters...]", file=stream) |
23 | 50 | print("", file=stream) |
24 | | - print("See user-docs/commands.md (in the python package source tree) for filter syntax.", file=stream) |
| 51 | + print( |
| 52 | + bold_hex("#ffa657", "✨ Auralogger CLI") + dim(" — pick a command:"), |
| 53 | + file=stream, |
| 54 | + ) |
| 55 | + print( |
| 56 | + hex_color("#7ee787", " init") + dim(" wire up secrets + copy-paste client config"), |
| 57 | + file=stream, |
| 58 | + ) |
| 59 | + print( |
| 60 | + hex_color("#7ee787", " server-check") + dim(" make sure the server logger can talk"), |
| 61 | + file=stream, |
| 62 | + ) |
| 63 | + print( |
| 64 | + hex_color("#7ee787", " client-check") + dim(" same vibes, browser-style pipe"), |
| 65 | + file=stream, |
| 66 | + ) |
| 67 | + print( |
| 68 | + hex_color("#7ee787", " test-serverlog") + dim(" five fake server logs, just for kicks"), |
| 69 | + file=stream, |
| 70 | + ) |
| 71 | + print( |
| 72 | + hex_color("#7ee787", " test-clientlog") + dim(" five fake client logs, same deal"), |
| 73 | + file=stream, |
| 74 | + ) |
| 75 | + print( |
| 76 | + hex_color("#7ee787", " get-logs") + dim(" hunt past logs (filters optional)"), |
| 77 | + file=stream, |
| 78 | + ) |
| 79 | + print("", file=stream) |
| 80 | + print( |
| 81 | + dim("Docs live on npm: auralogger-cli — filter cheat sheet is there."), |
| 82 | + file=stream, |
| 83 | + ) |
| 84 | + veteran = get_total_successful_commands() >= 4 and random.random() < 0.28 |
| 85 | + if veteran: |
| 86 | + a = pick_aside(CLI_VETERAN_USAGE_ASIDES) |
| 87 | + else: |
| 88 | + a = pick_tiered_aside( |
| 89 | + { |
| 90 | + "common": BIN_USAGE_ASIDES, |
| 91 | + "rare": BIN_USAGE_RARE_MULTI_ASIDES, |
| 92 | + "legendary": BIN_USAGE_LEGENDARY_ASIDES, |
| 93 | + } |
| 94 | + ) |
| 95 | + print_aside_maybe(a["emoji"], a["line"], DEFAULT_SILENCE_ASIDE_CHANCE) |
| 96 | + print("", file=stream) |
25 | 97 |
|
26 | 98 |
|
27 | 99 | def main() -> None: |
| 100 | + ensure_utf8_stdio() |
28 | 101 | load_cli_env_files() |
29 | 102 |
|
30 | | - args = sys.argv[1:] |
| 103 | + args: List[str] = sys.argv[1:] |
31 | 104 | command = args[0] if args else None |
32 | 105 |
|
33 | 106 | if not command: |
34 | 107 | print_usage() |
35 | 108 | return |
36 | 109 |
|
| 110 | + if command not in KNOWN_COMMANDS: |
| 111 | + record_cli_failure() |
| 112 | + print( |
| 113 | + red("🤔 Hmm, never heard of ") + bold(command) + red("."), |
| 114 | + file=sys.stderr, |
| 115 | + ) |
| 116 | + t = pick_aside(BIN_UNKNOWN_COMMAND_TEMPLATES) |
| 117 | + print_aside_maybe( |
| 118 | + t["emoji"], |
| 119 | + format_aside_template(t["line"], {"cmd": command}), |
| 120 | + DEFAULT_SILENCE_ASIDE_CHANCE, |
| 121 | + ) |
| 122 | + print_usage(sys.stderr) |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | + note_command_dispatch(command) |
| 126 | + |
37 | 127 | if command == "init": |
38 | 128 | run_init() |
| 129 | + record_cli_success(command) |
39 | 130 | return |
40 | 131 |
|
41 | 132 | if command == "get-logs": |
42 | 133 | run_get_logs_command(args) |
| 134 | + record_cli_success(command) |
43 | 135 | return |
44 | 136 |
|
45 | 137 | if command == "server-check": |
46 | 138 | run_server_check() |
| 139 | + record_cli_success(command) |
47 | 140 | return |
48 | 141 |
|
49 | 142 | if command == "client-check": |
50 | 143 | run_client_check() |
| 144 | + record_cli_success(command) |
51 | 145 | return |
52 | 146 |
|
53 | 147 | if command == "test-serverlog": |
54 | 148 | run_test_serverlog() |
| 149 | + record_cli_success(command) |
55 | 150 | return |
56 | 151 |
|
57 | 152 | if command == "test-clientlog": |
58 | 153 | run_test_clientlog() |
| 154 | + record_cli_success(command) |
59 | 155 | return |
60 | 156 |
|
61 | | - print(f"Unknown command: {command}", file=sys.stderr) |
62 | | - print( |
63 | | - "Valid commands: init, server-check, client-check, test-serverlog, test-clientlog, get-logs", |
64 | | - file=sys.stderr, |
65 | | - ) |
66 | | - print_usage(sys.stderr) |
67 | | - sys.exit(1) |
68 | | - |
69 | 157 |
|
70 | 158 | def _entrypoint() -> None: |
| 159 | + ensure_utf8_stdio() |
71 | 160 | try: |
72 | 161 | main() |
73 | 162 | except SystemExit: |
74 | 163 | raise |
75 | 164 | except Exception as exc: |
76 | | - print( |
77 | | - f"auralogger: {exc}", |
78 | | - file=sys.stderr, |
79 | | - ) |
| 165 | + record_cli_failure() |
| 166 | + message = str(exc) if isinstance(exc, Exception) else repr(exc) |
| 167 | + print("", file=sys.stderr) |
| 168 | + print(red_bold("💥 That didn't work."), file=sys.stderr) |
| 169 | + print(dim(" ") + white(message), file=sys.stderr) |
| 170 | + fails = get_consecutive_failures() |
| 171 | + if fails >= 2 and random.random() < 0.45: |
| 172 | + n = pick_aside(WOLVERINE_NUDGE_ASIDES) |
| 173 | + print_aside(n["emoji"], n["line"]) |
| 174 | + aside = pick_adaptive_fatal_aside(fails, message) |
| 175 | + print_aside_maybe(aside["emoji"], aside["line"], 0.08) |
| 176 | + err_kind = classify_error_for_aside(message) |
| 177 | + if err_kind in ("network", "auth-env") and random.random() < 0.42: |
| 178 | + e = pick_aside(ENV_SETUP_RECOVERY_ASIDES) |
| 179 | + print_aside(e["emoji"], e["line"]) |
| 180 | + maybe_print_generic_spice() |
80 | 181 | sys.exit(1) |
81 | 182 |
|
82 | 183 |
|
|
0 commit comments