Skip to content

Commit

Permalink
Feature: new several improvements (#490)
Browse files Browse the repository at this point in the history
* feat: add label version in verbose

* feat: add whoami in --verbose

* feat: add try\except of get_whoami
  • Loading branch information
Romazes authored Aug 15, 2024
1 parent 51968f6 commit 5118352
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
43 changes: 42 additions & 1 deletion lean/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,48 @@
from click import Command, Context, Parameter, ParamType, Option as ClickOption
from click.decorators import FC, option

from lean.constants import DEFAULT_LEAN_CONFIG_FILE_NAME
from lean.constants import DEFAULT_LEAN_CONFIG_FILE_NAME, CONTAINER_LABEL_LEAN_VERSION_NAME
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"],
Expand Down Expand Up @@ -73,6 +109,7 @@ def _parse_verbose_option(ctx: Context, param: Parameter, value: Optional[bool])
container.docker_manager.get_image_label(engine_image, 'strict_python_version', "Unknown")
container.docker_manager.get_image_label(engine_image, 'python_version', "Unknown")
container.docker_manager.get_image_label(engine_image, 'target_framework', "Unknown")
container.docker_manager.get_image_label(engine_image, CONTAINER_LABEL_LEAN_VERSION_NAME, None)
except:
pass

Expand All @@ -83,6 +120,10 @@ def _parse_verbose_option(ctx: Context, param: Parameter, value: Optional[bool])
f" .NET version: {dotnet_version}\n"
f" VS Code version: {vscode_version}\n"
f" VS Code installed versions: {vscode_installed_extensions}")
try:
logger.debug(get_whoami_message())
except:
logger.debug("Unable to retrieve login information. The user might not be logged in.")


def verbose_option() -> Callable[[FC], FC]:
Expand Down
26 changes: 2 additions & 24 deletions lean/commands/whoami.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}')

0 comments on commit 5118352

Please sign in to comment.