From 1621e133609b816450164caa1e37c6ae473ffe2a Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Thu, 21 Oct 2021 04:02:28 +0200 Subject: [PATCH] decorators for instancem, project and service commands --- kiwi_scp/misc.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/kiwi_scp/misc.py b/kiwi_scp/misc.py index 7202fd3..cfa808b 100644 --- a/kiwi_scp/misc.py +++ b/kiwi_scp/misc.py @@ -1,6 +1,47 @@ -from typing import Any, Type +from typing import Any, Type, List, Callable +import attr import click +from click.decorators import FC + + +@attr.s +class _MultiDecorator: + options: List[Callable[[FC], FC]] = attr.ib(factory=list) + + def __call__(self, target: FC): + for option in reversed(self.options): + target = option(target) + + return target + + +_instance_args = [] + +instance_command = _MultiDecorator(_instance_args) + +_project_args = [ + *_instance_args, + click.argument( + "project", + required=False, + type=click.Path(exists=True), + default=".", + ), +] + +project_command = _MultiDecorator(_project_args) + +_service_args = [ + *_project_args, + click.argument( + "service", + required=False, + type=str, + ), +] + +service_command = _MultiDecorator(_service_args) def user_query(description: str, default: Any, cast_to: Type[Any] = str):