feat(shared): allow cli parametrized decorators (#1527)
This commit is contained in:
parent
2de1ce6e0d
commit
1561353b20
|
@ -17,8 +17,8 @@ DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue")
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@cli_logging_options
|
@cli_logging_options()
|
||||||
@cli_config_options
|
@cli_config_options()
|
||||||
@click.option(
|
@click.option(
|
||||||
"--retry-queue-filepath",
|
"--retry-queue-filepath",
|
||||||
envvar=f"{DEFAULT_ENV_PREFIX}_RETRY_QUEUE_FILEPATH",
|
envvar=f"{DEFAULT_ENV_PREFIX}_RETRY_QUEUE_FILEPATH",
|
||||||
|
|
|
@ -16,7 +16,7 @@ PYPO_HOME = "/var/tmp/airtime/pypo/"
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@cli_logging_options
|
@cli_logging_options()
|
||||||
def cli(log_level: int, log_filepath: Optional[Path]):
|
def cli(log_level: int, log_filepath: Optional[Path]):
|
||||||
"""
|
"""
|
||||||
Run liquidsoap.
|
Run liquidsoap.
|
||||||
|
|
|
@ -110,7 +110,7 @@ def liquidsoap_startup_test(telnet_lock, ls_host, ls_port):
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@cli_logging_options
|
@cli_logging_options()
|
||||||
def cli(log_level: str, log_filepath: Optional[Path]):
|
def cli(log_level: str, log_filepath: Optional[Path]):
|
||||||
"""
|
"""
|
||||||
Run playout.
|
Run playout.
|
||||||
|
|
|
@ -27,7 +27,7 @@ def api_client():
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@cli_logging_options
|
@cli_logging_options()
|
||||||
def cli(log_level: str, log_filepath: Optional[Path]):
|
def cli(log_level: str, log_filepath: Optional[Path]):
|
||||||
"""
|
"""
|
||||||
A gateway between Liquidsoap and the API.
|
A gateway between Liquidsoap and the API.
|
||||||
|
|
|
@ -69,8 +69,8 @@ def cli():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@cli_config_options
|
@cli_config_options()
|
||||||
@cli_logging_options
|
@cli_logging_options()
|
||||||
def run(**kwargs):
|
def run(**kwargs):
|
||||||
app = App(**kwargs)
|
app = App(**kwargs)
|
||||||
return app.run()
|
return app.run()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Callable
|
from typing import Any, Callable, Optional
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
|
@ -7,51 +7,61 @@ from .config import DEFAULT_ENV_PREFIX
|
||||||
from .logging import INFO, LOG_LEVEL_MAP
|
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.
|
"""
|
||||||
|
Decorator function to add logging options to a click application.
|
||||||
|
|
||||||
This decorator add the following arguments:
|
This decorator add the following arguments:
|
||||||
- log_level: str
|
- log_level: str
|
||||||
- log_filepath: Optional[Path]
|
- log_filepath: Optional[Path]
|
||||||
"""
|
"""
|
||||||
func = click.option(
|
func = click.option(
|
||||||
"--log-level",
|
"--log-level",
|
||||||
"log_level",
|
"log_level",
|
||||||
envvar=f"{DEFAULT_ENV_PREFIX}_LOG_LEVEL",
|
envvar=f"{DEFAULT_ENV_PREFIX}_LOG_LEVEL",
|
||||||
type=click.Choice(list(LOG_LEVEL_MAP.keys())),
|
type=click.Choice(list(LOG_LEVEL_MAP.keys())),
|
||||||
default=INFO.name,
|
default=INFO.name,
|
||||||
help="Name of the logging level.",
|
help="Name of the logging level.",
|
||||||
)(func)
|
)(func)
|
||||||
|
|
||||||
func = click.option(
|
func = click.option(
|
||||||
"--log-filepath",
|
"--log-filepath",
|
||||||
"log_filepath",
|
"log_filepath",
|
||||||
envvar=f"{DEFAULT_ENV_PREFIX}_LOG_FILEPATH",
|
envvar=f"{DEFAULT_ENV_PREFIX}_LOG_FILEPATH",
|
||||||
type=click.Path(path_type=Path),
|
type=click.Path(path_type=Path),
|
||||||
help="Path to the logging file.",
|
help="Path to the logging file.",
|
||||||
default=None,
|
default=None,
|
||||||
)(func)
|
)(func)
|
||||||
|
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
def cli_config_options(func: Callable) -> Callable:
|
def cli_config_options(
|
||||||
"""
|
required: bool = False,
|
||||||
Decorator function to add config file options to a click application.
|
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:
|
This decorator add the following arguments:
|
||||||
- config_filepath: Optional[Path]
|
- config_filepath: Optional[Path] or Path
|
||||||
"""
|
"""
|
||||||
|
|
||||||
func = click.option(
|
func = click.option(
|
||||||
"--c",
|
"--c",
|
||||||
"--config",
|
"--config",
|
||||||
"config_filepath",
|
"config_filepath",
|
||||||
envvar=f"{DEFAULT_ENV_PREFIX}_CONFIG_FILEPATH",
|
envvar=f"{DEFAULT_ENV_PREFIX}_CONFIG_FILEPATH",
|
||||||
type=click.Path(path_type=Path),
|
type=click.Path(path_type=Path),
|
||||||
help="Path to the config file.",
|
help="Path to the config file.",
|
||||||
default=None,
|
required=required,
|
||||||
)(func)
|
default=default,
|
||||||
|
)(func)
|
||||||
|
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
Loading…
Reference in New Issue