1- import asyncio
21import os
32from pathlib import Path
43from typing import List
54
65import click
7- import uvicorn
8- from click import Abort
9-
10- from recce import event
11- from recce .artifact import (
12- delete_dbt_artifacts ,
13- download_dbt_artifacts ,
14- upload_artifacts_to_session ,
15- upload_dbt_artifacts ,
16- )
17- from recce .config import RECCE_CONFIG_FILE , RECCE_ERROR_LOG_FILE , RecceConfig
18- from recce .connect_to_cloud import (
19- generate_key_pair ,
20- prepare_connection_url ,
21- run_one_time_http_server ,
22- )
23- from recce .exceptions import RecceConfigException
24- from recce .git import current_branch , current_default_branch
25- from recce .run import check_github_ci_env , cli_run
26- from recce .server import RecceServerMode
27- from recce .state import (
28- CloudStateLoader ,
29- FileStateLoader ,
30- RecceCloudStateManager ,
31- RecceShareStateManager ,
32- )
33- from recce .summary import generate_markdown_summary
34- from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
35- from recce .util .logger import CustomFormatter
36- from recce .util .recce_cloud import (
37- RecceCloudException ,
38- )
39- from recce .util .startup_perf import track_timing
406
41- from . core import RecceContext
42- from . event . track import TrackCommand
7+ from recce . constants import RECCE_CONFIG_FILE , RECCE_ERROR_LOG_FILE
8+ from recce . util . startup_perf import track_timing
439
44- event . init ()
10+ from . track import TrackCommand
4511
4612
4713def create_state_loader (review_mode , cloud_mode , state_file , cloud_options ):
4814 from rich .console import Console
4915
16+ from recce .state import CloudStateLoader , FileStateLoader
17+ from recce .util .recce_cloud import RecceCloudException
18+
5019 console = Console ()
5120
5221 try :
@@ -140,6 +109,8 @@ def handle_debug_flag(**kwargs):
140109 if kwargs .get ("debug" ):
141110 import logging
142111
112+ from recce .util .logger import CustomFormatter
113+
143114 ch = logging .StreamHandler ()
144115 ch .setFormatter (CustomFormatter ())
145116 logging .basicConfig (handlers = [ch ], level = logging .DEBUG )
@@ -249,7 +220,7 @@ def _add_options(func):
249220 click .option (
250221 "--mode" ,
251222 envvar = "RECCE_SERVER_MODE" ,
252- type = click .Choice (RecceServerMode . available_members () , case_sensitive = False ),
223+ type = click .Choice ([ "server" , "preview" , "read-only" ] , case_sensitive = False ),
253224 hidden = True ,
254225 ),
255226 click .option (
@@ -426,6 +397,8 @@ def query(sql, base: bool = False, **kwargs):
426397 - run an adhoc query on base environment\n
427398 recce query --base --sql 'select * from {{ ref("mymodel") }} order by 1'
428399 """
400+ from .core import RecceContext
401+
429402 context = RecceContext .load (** kwargs )
430403 result = _execute_sql (context , sql , base = base )
431404 print (result .to_string (na_rep = "-" , index = False ))
@@ -458,6 +431,8 @@ def diff(sql, primary_keys: List[str] = None, keep_shape: bool = False, keep_equ
458431 recce diff --sql 'select * from {{ ref("mymodel") }} order by 1'
459432 """
460433
434+ from .core import RecceContext
435+
461436 context = RecceContext .load (** kwargs )
462437 before = _execute_sql (context , sql , base = True )
463438 if primary_keys is not None :
@@ -528,9 +503,15 @@ def server(host, port, lifetime, idle_timeout=0, state_file=None, **kwargs):
528503
529504 """
530505
506+ import uvicorn
531507 from rich .console import Console
532508 from rich .prompt import Confirm
533509
510+ from recce .config import RecceConfig
511+ from recce .exceptions import RecceConfigException
512+ from recce .server import RecceServerMode
513+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
514+
534515 from .server import AppState , app
535516
536517 RecceConfig (config_file = kwargs .get ("config" ))
@@ -716,8 +697,17 @@ def run(output, **kwargs):
716697 recce run --cloud --cloud-token <token> --password <password>
717698
718699 """
700+ import asyncio
701+
719702 from rich .console import Console
720703
704+ from recce .config import RecceConfig
705+ from recce .exceptions import RecceConfigException
706+ from recce .run import check_github_ci_env , cli_run
707+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
708+
709+ from .core import RecceContext
710+
721711 handle_debug_flag (** kwargs )
722712 console = Console ()
723713 is_github_action , pr_url = check_github_ci_env (** kwargs )
@@ -799,6 +789,8 @@ def summary(state_file, **kwargs):
799789 """
800790 from rich .console import Console
801791
792+ from recce .summary import generate_markdown_summary
793+
802794 from .core import load_context
803795
804796 handle_debug_flag (** kwargs )
@@ -845,6 +837,12 @@ def connect_to_cloud():
845837
846838 from rich .console import Console
847839
840+ from recce .connect_to_cloud import (
841+ generate_key_pair ,
842+ prepare_connection_url ,
843+ run_one_time_http_server ,
844+ )
845+
848846 console = Console ()
849847
850848 # Prepare RSA keys for connecting to cloud
@@ -892,6 +890,8 @@ def purge(**kwargs):
892890 """
893891 from rich .console import Console
894892
893+ from recce .state import RecceCloudStateManager
894+
895895 handle_debug_flag (** kwargs )
896896 console = Console ()
897897 state_loader = None
@@ -976,6 +976,8 @@ def upload(state_file, **kwargs):
976976 """
977977 from rich .console import Console
978978
979+ from recce .state import RecceCloudStateManager
980+
979981 handle_debug_flag (** kwargs )
980982 cloud_options = {
981983 "host" : kwargs .get ("state_file_host" ),
@@ -1045,6 +1047,8 @@ def download(**kwargs):
10451047 """
10461048 from rich .console import Console
10471049
1050+ from recce .state import RecceCloudStateManager
1051+
10481052 handle_debug_flag (** kwargs )
10491053 filepath = kwargs .get ("output" )
10501054 cloud_options = {
@@ -1078,11 +1082,9 @@ def download(**kwargs):
10781082@click .option (
10791083 "--branch" ,
10801084 "-b" ,
1081- help = "The branch of the provided artifacts." ,
1085+ help = "The branch of the provided artifacts. Defaults to current branch. " ,
10821086 type = click .STRING ,
10831087 envvar = "GITHUB_HEAD_REF" ,
1084- default = current_branch (),
1085- show_default = True ,
10861088)
10871089@click .option (
10881090 "--target-path" ,
@@ -1112,11 +1114,14 @@ def upload_artifacts(**kwargs):
11121114 """
11131115 from rich .console import Console
11141116
1117+ from recce .artifact import upload_dbt_artifacts
1118+ from recce .git import current_branch
1119+
11151120 console = Console ()
11161121 cloud_token = kwargs .get ("cloud_token" )
11171122 password = kwargs .get ("password" )
11181123 target_path = kwargs .get ("target_path" )
1119- branch = kwargs .get ("branch" )
1124+ branch = kwargs .get ("branch" ) or current_branch ()
11201125
11211126 try :
11221127 rc = upload_dbt_artifacts (
@@ -1135,6 +1140,8 @@ def upload_artifacts(**kwargs):
11351140
11361141
11371142def _download_artifacts (branch , cloud_token , console , kwargs , password , target_path ):
1143+ from recce .artifact import download_dbt_artifacts
1144+
11381145 try :
11391146 rc = download_dbt_artifacts (
11401147 target_path ,
@@ -1175,11 +1182,9 @@ def _download_artifacts(branch, cloud_token, console, kwargs, password, target_p
11751182@click .option (
11761183 "--branch" ,
11771184 "-b" ,
1178- help = "The branch of the selected artifacts." ,
1185+ help = "The branch of the selected artifacts. Defaults to current branch. " ,
11791186 type = click .STRING ,
11801187 envvar = "GITHUB_BASE_REF" ,
1181- default = current_branch (),
1182- show_default = True ,
11831188)
11841189@click .option (
11851190 "--target-path" ,
@@ -1210,11 +1215,13 @@ def download_artifacts(**kwargs):
12101215 """
12111216 from rich .console import Console
12121217
1218+ from recce .git import current_branch
1219+
12131220 console = Console ()
12141221 cloud_token = kwargs .get ("cloud_token" )
12151222 password = kwargs .get ("password" )
12161223 target_path = kwargs .get ("target_path" )
1217- branch = kwargs .get ("branch" )
1224+ branch = kwargs .get ("branch" ) or current_branch ()
12181225 return _download_artifacts (branch , cloud_token , console , kwargs , password , target_path )
12191226
12201227
@@ -1223,11 +1230,9 @@ def download_artifacts(**kwargs):
12231230@click .option (
12241231 "--branch" ,
12251232 "-b" ,
1226- help = "The branch of the selected artifacts." ,
1233+ help = "The branch of the selected artifacts. Defaults to default branch. " ,
12271234 type = click .STRING ,
12281235 envvar = "GITHUB_BASE_REF" ,
1229- default = current_default_branch (),
1230- show_default = True ,
12311236)
12321237@click .option (
12331238 "--target-path" ,
@@ -1257,11 +1262,13 @@ def download_base_artifacts(**kwargs):
12571262 """
12581263 from rich .console import Console
12591264
1265+ from recce .git import current_default_branch
1266+
12601267 console = Console ()
12611268 cloud_token = kwargs .get ("cloud_token" )
12621269 password = kwargs .get ("password" )
12631270 target_path = kwargs .get ("target_path" )
1264- branch = kwargs .get ("branch" )
1271+ branch = kwargs .get ("branch" ) or current_default_branch ()
12651272 # If recce can't infer default branch from "GITHUB_BASE_REF" and current_default_branch()
12661273 if branch is None :
12671274 console .print (
@@ -1277,11 +1284,9 @@ def download_base_artifacts(**kwargs):
12771284@click .option (
12781285 "--branch" ,
12791286 "-b" ,
1280- help = "The branch to delete artifacts from." ,
1287+ help = "The branch to delete artifacts from. Defaults to current branch. " ,
12811288 type = click .STRING ,
12821289 envvar = "GITHUB_HEAD_REF" ,
1283- default = current_branch (),
1284- show_default = True ,
12851290)
12861291@click .option ("--force" , "-f" , help = "Bypasses the confirmation prompt. Delete the artifacts directly." , is_flag = True )
12871292@add_options (recce_options )
@@ -1296,9 +1301,13 @@ def delete_artifacts(**kwargs):
12961301 """
12971302 from rich .console import Console
12981303
1304+ from recce .artifact import delete_dbt_artifacts
1305+ from recce .git import current_branch
1306+ from recce .util .recce_cloud import RecceCloudException
1307+
12991308 console = Console ()
13001309 cloud_token = kwargs .get ("cloud_token" )
1301- branch = kwargs .get ("branch" )
1310+ branch = kwargs .get ("branch" ) or current_branch ()
13021311 force = kwargs .get ("force" , False )
13031312
13041313 if not force :
@@ -1334,6 +1343,10 @@ def list_organizations(**kwargs):
13341343 from rich .console import Console
13351344 from rich .table import Table
13361345
1346+ from recce .exceptions import RecceConfigException
1347+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1348+ from recce .util .recce_cloud import RecceCloudException
1349+
13371350 console = Console ()
13381351 handle_debug_flag (** kwargs )
13391352
@@ -1403,6 +1416,10 @@ def list_projects(**kwargs):
14031416 from rich .console import Console
14041417 from rich .table import Table
14051418
1419+ from recce .exceptions import RecceConfigException
1420+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1421+ from recce .util .recce_cloud import RecceCloudException
1422+
14061423 console = Console ()
14071424 handle_debug_flag (** kwargs )
14081425
@@ -1491,6 +1508,10 @@ def list_sessions(**kwargs):
14911508 from rich .console import Console
14921509 from rich .table import Table
14931510
1511+ from recce .exceptions import RecceConfigException
1512+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1513+ from recce .util .recce_cloud import RecceCloudException
1514+
14941515 console = Console ()
14951516 handle_debug_flag (** kwargs )
14961517
@@ -1581,8 +1602,14 @@ def share(state_file, **kwargs):
15811602 """
15821603 Share the state file
15831604 """
1605+ from click import Abort
15841606 from rich .console import Console
15851607
1608+ from recce .exceptions import RecceConfigException
1609+ from recce .state import RecceShareStateManager
1610+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1611+ from recce .util .recce_cloud import RecceCloudException
1612+
15861613 console = Console ()
15871614 handle_debug_flag (** kwargs )
15881615 cloud_options = None
@@ -1682,6 +1709,11 @@ def upload_session(**kwargs):
16821709 """
16831710 from rich .console import Console
16841711
1712+ from recce .artifact import upload_artifacts_to_session
1713+ from recce .config import RecceConfig
1714+ from recce .exceptions import RecceConfigException
1715+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1716+
16851717 console = Console ()
16861718 handle_debug_flag (** kwargs )
16871719
@@ -1736,6 +1768,8 @@ def snapshot(**kwargs):
17361768@click .option ("--share-url" , help = "The share URL triggers this instance." , type = click .STRING , envvar = "RECCE_SHARE_URL" )
17371769@click .pass_context
17381770def read_only (ctx , state_file = None , ** kwargs ):
1771+ from recce .server import RecceServerMode
1772+
17391773 # Invoke `recce server --mode read-only <state_file> ...
17401774 kwargs ["mode" ] = RecceServerMode .read_only
17411775 ctx .invoke (server , state_file = state_file , ** kwargs )
@@ -1801,8 +1835,14 @@ def mcp_server(state_file, sse, host, port, **kwargs):
18011835
18021836 SSE Connection URL (when using --sse): http://<host>:<port>/sse
18031837 """
1838+ import asyncio
1839+
18041840 from rich .console import Console
18051841
1842+ from recce .config import RecceConfig
1843+ from recce .exceptions import RecceConfigException
1844+ from recce .util .api_token import prepare_api_token , show_invalid_api_token_message
1845+
18061846 # In stdio mode, stdout is the JSON-RPC transport — all human-readable
18071847 # output must go to stderr to avoid MCP client parse errors.
18081848 console = Console (stderr = True ) if not sse else Console ()
0 commit comments