1
+ import sys
1
2
from colorama import Fore
2
3
3
4
from src .api import RedAPI , OpsAPI
10
11
11
12
def cli_entrypoint (args ):
12
13
try :
13
- config = Config ().load (args .config_file )
14
- red_api , ops_api = __verify_api_keys (config )
15
- injector = Injection (config ).setup () if config .inject_torrents else None
14
+ # using input_file means this is probably running as a script and extra printing wouldn't be appreciated
15
+ should_print = args .input_directory or args .server
16
+ config = command_log_wrapper ("Reading config file:" , should_print , lambda : Config ().load (args .config_file ))
17
+ red_api , ops_api = command_log_wrapper ("Verifying API keys:" , should_print , lambda : __verify_api_keys (config ))
18
+
19
+ if config .inject_torrents :
20
+ injector = command_log_wrapper ("Connecting to torrent client:" , should_print , lambda : Injection (config ).setup ())
21
+ else :
22
+ injector = None
16
23
17
24
if args .server :
18
25
run_webserver (args .input_directory , args .output_directory , red_api , ops_api , injector , port = config .server_port )
@@ -37,6 +44,23 @@ def __verify_api_keys(config):
37
44
return red_api , ops_api
38
45
39
46
47
+ def command_log_wrapper (label , should_print , func ):
48
+ def maybe_print (str , * args , ** kwargs ):
49
+ if should_print :
50
+ print (str , * args , ** kwargs )
51
+ sys .stdout .flush ()
52
+
53
+ maybe_print (f"{ label } " , end = "" )
54
+
55
+ try :
56
+ result = func ()
57
+ maybe_print (f"{ Fore .GREEN } Success{ Fore .RESET } " )
58
+ return result
59
+ except Exception as e :
60
+ maybe_print (f"{ Fore .RED } Error{ Fore .RESET } " )
61
+ raise e
62
+
63
+
40
64
if __name__ == "__main__" :
41
65
args = parse_args ()
42
66
0 commit comments