99from centml .sdk import auth
1010from centml .sdk .api import get_centml_client
1111from centml .sdk .config import settings
12- from centml .sdk .shell import ShellError
13- from centml .sdk .shell .session import build_ws_url , exec_session , interactive_session , resolve_pod
12+ from centml .sdk .shell import ShellError , build_ws_url , exec_session , interactive_session , resolve_pod
1413
1514
16- @click .command (help = "Open an interactive shell to a deployment pod" )
17- @click .argument ("deployment_id" , type = int )
18- @click .option ("--pod" , default = None , help = "Specific pod name (auto-selects first running pod)" )
19- @click .option ("--shell" , "shell_type" , default = None , type = click .Choice (["bash" , "sh" , "zsh" ]), help = "Shell type" )
20- @handle_exception
21- def shell (deployment_id , pod , shell_type ):
22- if not sys .stdin .isatty ():
23- raise click .ClickException ("Interactive shell requires a terminal (TTY)" )
24-
15+ def _connect_args (deployment_id , pod , shell_type ):
16+ """Resolve pod, build WebSocket URL, and obtain auth token."""
2517 with get_centml_client () as cclient :
2618 try :
2719 pod_name , warning = resolve_pod (cclient , deployment_id , pod )
@@ -32,6 +24,19 @@ def shell(deployment_id, pod, shell_type):
3224
3325 ws_url = build_ws_url (settings .CENTML_PLATFORM_API_URL , deployment_id , pod_name , shell_type )
3426 token = auth .get_centml_token ()
27+ return ws_url , token
28+
29+
30+ @click .command (help = "Open an interactive shell to a deployment pod" )
31+ @click .argument ("deployment_id" , type = int )
32+ @click .option ("--pod" , default = None , help = "Specific pod name (auto-selects first running pod)" )
33+ @click .option ("--shell" , "shell_type" , default = None , type = click .Choice (["bash" , "sh" , "zsh" ]), help = "Shell type" )
34+ @handle_exception
35+ def shell (deployment_id , pod , shell_type ):
36+ if not sys .stdin .isatty ():
37+ raise click .ClickException ("Interactive shell requires a terminal (TTY)" )
38+
39+ ws_url , token = _connect_args (deployment_id , pod , shell_type )
3540 exit_code = asyncio .run (interactive_session (ws_url , token ))
3641 sys .exit (exit_code )
3742
@@ -43,16 +48,6 @@ def shell(deployment_id, pod, shell_type):
4348@click .option ("--shell" , "shell_type" , default = None , type = click .Choice (["bash" , "sh" , "zsh" ]), help = "Shell type" )
4449@handle_exception
4550def exec_cmd (deployment_id , command , pod , shell_type ):
46- with get_centml_client () as cclient :
47- try :
48- pod_name , warning = resolve_pod (cclient , deployment_id , pod )
49- except ShellError as exc :
50- raise click .ClickException (str (exc )) from exc
51- if warning :
52- click .echo (f"{ warning } Use --pod to specify a different pod." , err = True )
53-
54- ws_url = build_ws_url (settings .CENTML_PLATFORM_API_URL , deployment_id , pod_name , shell_type )
55- token = auth .get_centml_token ()
56- cmd_str = " " .join (command )
57- exit_code = asyncio .run (exec_session (ws_url , token , cmd_str ))
51+ ws_url , token = _connect_args (deployment_id , pod , shell_type )
52+ exit_code = asyncio .run (exec_session (ws_url , token , " " .join (command )))
5853 sys .exit (exit_code )
0 commit comments