Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI command decorators to reduce duplication #1109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
91 changes: 23 additions & 68 deletions livekit-agents/livekit/agents/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,40 @@
from .log import setup_logging


def run_app(opts: WorkerOptions) -> None:
"""Run the CLI to interact with the worker"""
cli = click.Group()

@cli.command(help="Start the worker in production mode.")
@click.option(
def common_options(f):
"""Decorator to add common options to CLI commands."""
f = click.option(
"--log-level",
default="INFO",
default="DEBUG",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False
),
help="Set the logging level",
)
@click.option(
)(f)
f = click.option(
"--url",
envvar="LIVEKIT_URL",
help="LiveKit server or Cloud project's websocket URL",
)
@click.option(
)(f)
f = click.option(
"--api-key",
envvar="LIVEKIT_API_KEY",
help="LiveKit server or Cloud project's API key",
)
@click.option(
)(f)
f = click.option(
"--api-secret",
envvar="LIVEKIT_API_SECRET",
help="LiveKit server or Cloud project's API secret",
)
)(f)
return f


def run_app(opts: WorkerOptions) -> None:
"""Run the CLI to interact with the worker"""
cli = click.Group()

@cli.command(help="Start the worker in production mode.")
@common_options
@click.option(
"--drain-timeout",
default=60,
Expand All @@ -64,29 +70,7 @@ def start(
run_worker(args)

@cli.command(help="Start the worker in development mode")
@click.option(
"--log-level",
default="DEBUG",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False
),
help="Set the logging level",
)
@click.option(
"--url",
envvar="LIVEKIT_URL",
help="LiveKit server or Cloud project's websocket URL",
)
@click.option(
"--api-key",
envvar="LIVEKIT_API_KEY",
help="LiveKit server or Cloud project's API key",
)
@click.option(
"--api-secret",
envvar="LIVEKIT_API_SECRET",
help="LiveKit server or Cloud project's API secret",
)
@common_options
@click.option(
"--asyncio-debug/--no-asyncio-debug",
default=False,
Expand All @@ -108,29 +92,7 @@ def dev(
_run_dev(opts, log_level, url, api_key, api_secret, asyncio_debug, watch)

@cli.command(help="Connect to a specific room")
@click.option(
"--log-level",
default="DEBUG",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False
),
help="Set the logging level",
)
@click.option(
"--url",
envvar="LIVEKIT_URL",
help="LiveKit server or Cloud project's websocket URL",
)
@click.option(
"--api-key",
envvar="LIVEKIT_API_KEY",
help="LiveKit server or Cloud project's API key",
)
@click.option(
"--api-secret",
envvar="LIVEKIT_API_SECRET",
help="LiveKit server or Cloud project's API secret",
)
@common_options
@click.option(
"--asyncio-debug/--no-asyncio-debug",
default=False,
Expand Down Expand Up @@ -168,14 +130,7 @@ def connect(
)

@cli.command(help="Download plugin dependency files")
@click.option(
"--log-level",
default="DEBUG",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False
),
help="Set the logging level",
)
@common_options
def download_files(log_level: str) -> None:
setup_logging(log_level, True)

Expand Down