feat(shared): allow cli parametrized decorators (#1527)

This commit is contained in:
Jonas L 2022-01-20 06:30:35 +01:00 committed by GitHub
parent 2de1ce6e0d
commit 1561353b20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 48 deletions

View File

@ -17,8 +17,8 @@ DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue")
@click.command()
@cli_logging_options
@cli_config_options
@cli_logging_options()
@cli_config_options()
@click.option(
"--retry-queue-filepath",
envvar=f"{DEFAULT_ENV_PREFIX}_RETRY_QUEUE_FILEPATH",

View File

@ -16,7 +16,7 @@ PYPO_HOME = "/var/tmp/airtime/pypo/"
@click.command()
@cli_logging_options
@cli_logging_options()
def cli(log_level: int, log_filepath: Optional[Path]):
"""
Run liquidsoap.

View File

@ -110,7 +110,7 @@ def liquidsoap_startup_test(telnet_lock, ls_host, ls_port):
@click.command()
@cli_logging_options
@cli_logging_options()
def cli(log_level: str, log_filepath: Optional[Path]):
"""
Run playout.

View File

@ -27,7 +27,7 @@ def api_client():
@click.group()
@cli_logging_options
@cli_logging_options()
def cli(log_level: str, log_filepath: Optional[Path]):
"""
A gateway between Liquidsoap and the API.

View File

@ -69,8 +69,8 @@ def cli():
pass
@cli.command()
@cli_config_options
@cli_logging_options
@cli_config_options()
@cli_logging_options()
def run(**kwargs):
app = App(**kwargs)
return app.run()

View File

@ -1,5 +1,5 @@
from pathlib import Path
from typing import Callable
from typing import Any, Callable, Optional
import click
@ -7,7 +7,8 @@ from .config import DEFAULT_ENV_PREFIX
from .logging import INFO, LOG_LEVEL_MAP
def cli_logging_options(func: Callable) -> Callable:
def cli_logging_options() -> Callable:
def decorator(func: Callable) -> Callable:
"""
Decorator function to add logging options to a click application.
@ -35,13 +36,19 @@ def cli_logging_options(func: Callable) -> Callable:
return func
return decorator
def cli_config_options(func: Callable) -> Callable:
def cli_config_options(
required: bool = False,
default: Optional[Any] = None,
) -> Callable:
def decorator(func: Callable) -> Callable:
"""
Decorator function to add config file options to a click application.
This decorator add the following arguments:
- config_filepath: Optional[Path]
- config_filepath: Optional[Path] or Path
"""
func = click.option(
@ -51,7 +58,10 @@ def cli_config_options(func: Callable) -> Callable:
envvar=f"{DEFAULT_ENV_PREFIX}_CONFIG_FILEPATH",
type=click.Path(path_type=Path),
help="Path to the config file.",
default=None,
required=required,
default=default,
)(func)
return func
return decorator