2021-10-21 02:02:28 +00:00
|
|
|
from typing import Any, Type, List, Callable
|
2021-10-20 12:32:45 +00:00
|
|
|
|
2021-10-21 02:02:28 +00:00
|
|
|
import attr
|
2021-10-20 12:32:45 +00:00
|
|
|
import click
|
2021-10-21 02:02:28 +00:00
|
|
|
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)
|
2021-10-20 12:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def user_query(description: str, default: Any, cast_to: Type[Any] = str):
|
|
|
|
# prompt user as per argument
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
str_value = input(f"Enter {description} [{default}] ").strip()
|
|
|
|
if str_value:
|
|
|
|
return cast_to(str_value)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
except EOFError:
|
|
|
|
click.echo("Input aborted.")
|
|
|
|
return default
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
click.echo(f"Invalid input: {e}")
|
|
|
|
|
|
|
|
|
2020-08-18 12:04:10 +00:00
|
|
|
def _surround(string, bang):
|
|
|
|
midlane = f"{bang * 3} {string} {bang * 3}"
|
2021-09-22 01:06:43 +00:00
|
|
|
sidelane = bang * len(midlane)
|
2020-08-18 12:04:10 +00:00
|
|
|
|
|
|
|
return f"{sidelane}\n{midlane}\n{sidelane}"
|
|
|
|
|
|
|
|
|
2020-08-18 13:05:19 +00:00
|
|
|
def _emphasize(lines):
|
|
|
|
if isinstance(lines, list):
|
|
|
|
return '\n'.join([_emphasize(line) for line in lines])
|
|
|
|
elif lines:
|
|
|
|
return f">>> {lines} <<<"
|
|
|
|
else:
|
|
|
|
return lines
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-18 12:04:10 +00:00
|
|
|
def are_you_sure(prompt, default="no"):
|
|
|
|
if default.lower() == 'yes':
|
|
|
|
suffix = "[YES|no]"
|
|
|
|
else:
|
|
|
|
suffix = "[yes|NO]"
|
|
|
|
|
|
|
|
answer = input(
|
2020-08-25 16:01:39 +00:00
|
|
|
f"{_surround('MUST HAVE CAREFULING IN PROCESS', '!')}\n"
|
2020-08-18 12:04:10 +00:00
|
|
|
f"\n"
|
2020-08-18 13:05:19 +00:00
|
|
|
f"{_emphasize(prompt)}\n"
|
2020-08-18 12:04:10 +00:00
|
|
|
f"\n"
|
|
|
|
f"Are you sure you want to proceed? {suffix} "
|
|
|
|
).strip().lower()
|
|
|
|
|
|
|
|
if answer == '':
|
|
|
|
answer = default
|
|
|
|
|
|
|
|
while answer not in ['yes', 'no']:
|
|
|
|
answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower()
|
|
|
|
|
|
|
|
return answer == 'yes'
|