From 9eee6b49a8b43ae2b0074a627fbe2ff6eedefda1 Mon Sep 17 00:00:00 2001 From: Romazes Date: Fri, 16 Aug 2024 00:16:14 +0300 Subject: [PATCH] feat: add whoami in --verbose --- lean/click.py | 38 ++++++++++++++++++++++++++++++++++++++ lean/commands/whoami.py | 26 ++------------------------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/lean/click.py b/lean/click.py index 9b00c2a4..d635caa5 100644 --- a/lean/click.py +++ b/lean/click.py @@ -21,8 +21,44 @@ from lean.container import container from lean.models.errors import MoreInfoError from lean.models.logger import Option +from lean.models.errors import AuthenticationError +def get_whoami_message() -> str: + """ + Retrieves a message indicating the currently logged-in user's name and email. + + This function checks if the user is logged in by verifying the presence of a user ID + and API token. If the user is logged in, it retrieves the user's personal organization + and finds the admin member associated with that organization. It then returns a message + containing the admin member's name and email address. If the user is not logged in, + it returns a message indicating that the user is not logged in. + + Returns: + str: A message indicating the logged-in user's name and email, + or a message stating that the user is not logged in. + """ + api_client = container.api_client + cli_config_manager = container.cli_config_manager + + if cli_config_manager.user_id.get_value() is not None and cli_config_manager.api_token.get_value() is not None: + try: + organizations = api_client.organizations.get_all() + logged_in = True + except AuthenticationError: + logged_in = False + else: + logged_in = False + + if not logged_in: + return "not logged in" + + personal_organization_id = next(o.id for o in organizations if o.ownerName == "You") + personal_organization = api_client.organizations.get(personal_organization_id) + member = next(m for m in personal_organization.members if m.isAdmin) + + return f"logged in as {member.name} ({member.email})" + class VerboseOption(ClickOption): def __init__(self, *args, **kwargs): super().__init__(["--verbose"], @@ -85,6 +121,8 @@ def _parse_verbose_option(ctx: Context, param: Parameter, value: Optional[bool]) f" VS Code version: {vscode_version}\n" f" VS Code installed versions: {vscode_installed_extensions}") + logger.debug(get_whoami_message()) + def verbose_option() -> Callable[[FC], FC]: return option(cls=VerboseOption) diff --git a/lean/commands/whoami.py b/lean/commands/whoami.py index 6f4d5e43..0d3f2fb3 100644 --- a/lean/commands/whoami.py +++ b/lean/commands/whoami.py @@ -13,33 +13,11 @@ from click import command -from lean.click import LeanCommand +from lean.click import LeanCommand, get_whoami_message from lean.container import container -from lean.models.errors import AuthenticationError @command(cls=LeanCommand) def whoami() -> None: """Display who is logged in.""" - logger = container.logger - api_client = container.api_client - cli_config_manager = container.cli_config_manager - - if cli_config_manager.user_id.get_value() is not None and cli_config_manager.api_token.get_value() is not None: - try: - organizations = api_client.organizations.get_all() - logged_in = True - except AuthenticationError: - logged_in = False - else: - logged_in = False - - if not logged_in: - logger.info("You are not logged in") - return - - personal_organization_id = next(o.id for o in organizations if o.ownerName == "You") - personal_organization = api_client.organizations.get(personal_organization_id) - member = next(m for m in personal_organization.members if m.isAdmin) - - logger.info(f"You are logged in as {member.name} ({member.email})") + container.logger.info(f'You are {get_whoami_message()}')