Skip to content

Commit 89dc8c2

Browse files
authored
feat: add --reset flag for setup, update, and server commands (#1106)
feat: add --reset flag
1 parent c25d179 commit 89dc8c2

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

python/cocoindex/cli.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ def show(app_flow_specifier: str, color: bool, verbose: bool) -> None:
220220
console.print(table)
221221

222222

223+
def _drop_flows(flows: Iterable[flow.Flow], force: bool = False) -> None:
224+
"""
225+
Helper function to drop flows without user interaction.
226+
Used internally by --reset flag
227+
228+
Args:
229+
flows: Iterable of Flow objects to drop
230+
force: If True, skip confirmation prompts
231+
"""
232+
flows_list = list(flows)
233+
if not flows_list:
234+
return
235+
236+
setup_bundle = flow.make_drop_bundle(flows_list)
237+
description, is_up_to_date = setup_bundle.describe()
238+
239+
if is_up_to_date:
240+
return
241+
242+
click.echo(description)
243+
setup_bundle.apply(report_to_stdout=True)
244+
245+
223246
def _setup_flows(
224247
flow_iter: Iterable[flow.Flow],
225248
*,
@@ -269,14 +292,26 @@ async def _update_all_flows_with_hint_async(
269292
default=False,
270293
help="Force setup without confirmation prompts.",
271294
)
272-
def setup(app_target: str, force: bool) -> None:
295+
@click.option(
296+
"--reset",
297+
is_flag=True,
298+
show_default=True,
299+
default=False,
300+
help="Drop existing setup before running setup (equivalent to running 'cocoindex drop' first).",
301+
)
302+
def setup(app_target: str, force: bool, reset: bool) -> None:
273303
"""
274304
Check and apply backend setup changes for flows, including the internal storage and target (to export to).
275305
276306
APP_TARGET: path/to/app.py or installed_module.
277307
"""
278308
app_ref = _get_app_ref_from_specifier(app_target)
279309
_load_user_app(app_ref)
310+
311+
# If --reset is specified, drop existing setup first
312+
if reset:
313+
_drop_flows(flow.flows().values(), force=force)
314+
280315
_setup_flows(flow.flows().values(), force=force, always_show_setup=True)
281316

282317

@@ -375,6 +410,13 @@ def drop(app_target: str | None, flow_name: tuple[str, ...], force: bool) -> Non
375410
default=False,
376411
help="Automatically setup backends for the flow if it's not setup yet.",
377412
)
413+
@click.option(
414+
"--reset",
415+
is_flag=True,
416+
show_default=True,
417+
default=False,
418+
help="Drop existing setup before updating (equivalent to running 'cocoindex drop' first).",
419+
)
378420
@click.option(
379421
"-f",
380422
"--force",
@@ -396,6 +438,7 @@ def update(
396438
live: bool,
397439
reexport: bool,
398440
setup: bool, # pylint: disable=redefined-outer-name
441+
reset: bool,
399442
force: bool,
400443
quiet: bool,
401444
) -> None:
@@ -408,6 +451,15 @@ def update(
408451
app_ref, flow_name = _parse_app_flow_specifier(app_flow_specifier)
409452
_load_user_app(app_ref)
410453

454+
# If --reset is specified, drop existing setup first
455+
if reset:
456+
if flow_name:
457+
# Reset specific flow only
458+
_drop_flows([flow.flow_by_name(flow_name)], force=force)
459+
else:
460+
# Reset all flows
461+
_drop_flows(flow.flows().values(), force=force)
462+
411463
if live:
412464
click.secho(
413465
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code.\n",
@@ -529,6 +581,13 @@ def evaluate(
529581
default=False,
530582
help="Automatically setup backends for the flow if it's not setup yet.",
531583
)
584+
@click.option(
585+
"--reset",
586+
is_flag=True,
587+
show_default=True,
588+
default=False,
589+
help="Drop existing setup before starting server (equivalent to running 'cocoindex drop' first).",
590+
)
532591
@click.option(
533592
"--reexport",
534593
is_flag=True,
@@ -565,6 +624,7 @@ def server(
565624
address: str | None,
566625
live_update: bool,
567626
setup: bool, # pylint: disable=redefined-outer-name
627+
reset: bool,
568628
reexport: bool,
569629
force: bool,
570630
quiet: bool,
@@ -589,6 +649,7 @@ def server(
589649
cors_local,
590650
live_update,
591651
setup,
652+
reset,
592653
reexport,
593654
force,
594655
quiet,
@@ -638,6 +699,7 @@ def _run_server(
638699
cors_local: int | None = None,
639700
live_update: bool = False,
640701
run_setup: bool = False,
702+
run_reset: bool = False,
641703
reexport: bool = False,
642704
force: bool = False,
643705
quiet: bool = False,
@@ -664,6 +726,10 @@ def _run_server(
664726
)
665727
raise click.Abort()
666728

729+
# If --reset is specified, drop existing setup first
730+
if run_reset:
731+
_drop_flows(flow.flows().values(), force=force)
732+
667733
server_settings = setting.ServerSettings.from_env()
668734
cors_origins: set[str] = set(server_settings.cors_origins or [])
669735
if cors_origin is not None:

0 commit comments

Comments
 (0)