Skip to content

Commit

Permalink
refactor: add selection ability back in
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed May 24, 2024
1 parent 9ae5d57 commit 5d01cd7
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def cli():
"""Work with Silverback applications in local context (using Ape)."""


def _runner_callback(ctx, param, val):
if not val:
return None

elif runner := import_from_string(val):
return runner

raise ValueError(f"Failed to import runner '{val}'.")


def _recorder_callback(ctx, param, val):
if not val:
return None
Expand Down Expand Up @@ -87,23 +97,30 @@ async def run_worker(broker: AsyncBroker, worker_count=2, shutdown_timeout=90):
callback=_network_callback,
)
@click.option("--account", type=AccountAliasPromptChoice(), callback=_account_callback)
@click.option(
"--runner",
"runner_class",
help="An import str in format '<module>:<CustomRunner>'",
callback=_runner_callback,
)
@click.option(
"--recorder",
help="An import string in format '<module>:<CustomRecorder>'",
callback=_recorder_callback,
)
@click.option("-x", "--max-exceptions", type=int, default=3)
@click.argument("path")
def run(cli_ctx, account, recorder, max_exceptions, path):
if cli_ctx.provider.ws_uri:
def run(cli_ctx, account, runner_class, recorder, max_exceptions, path):
if not runner_class:
# NOTE: Automatically select runner class
runner_class = WebsocketRunner
elif cli_ctx.provider.http_uri:
runner_class = PollingRunner
else:
raise click.BadOptionUsage(
option_name="network", message="Network choice cannot support app"
)
if cli_ctx.provider.ws_uri:
runner_class = WebsocketRunner
elif cli_ctx.provider.http_uri:
runner_class = PollingRunner
else:
raise click.BadOptionUsage(
option_name="network", message="Network choice cannot support running app"
)

app = import_from_string(path)
runner = runner_class(app, recorder=recorder, max_exceptions=max_exceptions)
Expand Down

0 comments on commit 5d01cd7

Please sign in to comment.