Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion python/cocoindex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ def show(app_flow_specifier: str, color: bool, verbose: bool) -> None:
console.print(table)


def _drop_flows(flows: Iterable[flow.Flow], force: bool = False) -> None:
"""
Helper function to drop flows without user interaction.
Used internally by --reset flag

Args:
flows: Iterable of Flow objects to drop
force: If True, skip confirmation prompts
"""
flows_list = list(flows)
if not flows_list:
return

setup_bundle = flow.make_drop_bundle(flows_list)
description, is_up_to_date = setup_bundle.describe()

if is_up_to_date:
return

click.echo(description)
setup_bundle.apply(report_to_stdout=True)


def _setup_flows(
flow_iter: Iterable[flow.Flow],
*,
Expand Down Expand Up @@ -269,14 +292,26 @@ async def _update_all_flows_with_hint_async(
default=False,
help="Force setup without confirmation prompts.",
)
def setup(app_target: str, force: bool) -> None:
@click.option(
"--reset",
is_flag=True,
show_default=True,
default=False,
help="Drop existing setup before running setup (equivalent to running 'cocoindex drop' first).",
)
def setup(app_target: str, force: bool, reset: bool) -> None:
"""
Check and apply backend setup changes for flows, including the internal storage and target (to export to).

APP_TARGET: path/to/app.py or installed_module.
"""
app_ref = _get_app_ref_from_specifier(app_target)
_load_user_app(app_ref)

# If --reset is specified, drop existing setup first
if reset:
_drop_flows(flow.flows().values(), force=force)

_setup_flows(flow.flows().values(), force=force, always_show_setup=True)


Expand Down Expand Up @@ -375,6 +410,13 @@ def drop(app_target: str | None, flow_name: tuple[str, ...], force: bool) -> Non
default=False,
help="Automatically setup backends for the flow if it's not setup yet.",
)
@click.option(
"--reset",
is_flag=True,
show_default=True,
default=False,
help="Drop existing setup before updating (equivalent to running 'cocoindex drop' first).",
)
@click.option(
"-f",
"--force",
Expand All @@ -396,6 +438,7 @@ def update(
live: bool,
reexport: bool,
setup: bool, # pylint: disable=redefined-outer-name
reset: bool,
force: bool,
quiet: bool,
) -> None:
Expand All @@ -408,6 +451,15 @@ def update(
app_ref, flow_name = _parse_app_flow_specifier(app_flow_specifier)
_load_user_app(app_ref)

# If --reset is specified, drop existing setup first
if reset:
if flow_name:
# Reset specific flow only
_drop_flows([flow.flow_by_name(flow_name)], force=force)
else:
# Reset all flows
_drop_flows(flow.flows().values(), force=force)

if live:
click.secho(
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code.\n",
Expand Down Expand Up @@ -529,6 +581,13 @@ def evaluate(
default=False,
help="Automatically setup backends for the flow if it's not setup yet.",
)
@click.option(
"--reset",
is_flag=True,
show_default=True,
default=False,
help="Drop existing setup before starting server (equivalent to running 'cocoindex drop' first).",
)
@click.option(
"--reexport",
is_flag=True,
Expand Down Expand Up @@ -565,6 +624,7 @@ def server(
address: str | None,
live_update: bool,
setup: bool, # pylint: disable=redefined-outer-name
reset: bool,
reexport: bool,
force: bool,
quiet: bool,
Expand All @@ -589,6 +649,7 @@ def server(
cors_local,
live_update,
setup,
reset,
reexport,
force,
quiet,
Expand Down Expand Up @@ -638,6 +699,7 @@ def _run_server(
cors_local: int | None = None,
live_update: bool = False,
run_setup: bool = False,
run_reset: bool = False,
reexport: bool = False,
force: bool = False,
quiet: bool = False,
Expand All @@ -664,6 +726,10 @@ def _run_server(
)
raise click.Abort()

# If --reset is specified, drop existing setup first
if run_reset:
_drop_flows(flow.flows().values(), force=force)

server_settings = setting.ServerSettings.from_env()
cors_origins: set[str] = set(server_settings.cors_origins or [])
if cors_origin is not None:
Expand Down