Skip to content

Commit

Permalink
feat: add whoami in --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
Romazes committed Aug 15, 2024
1 parent 2ab10c9 commit 9eee6b4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
38 changes: 38 additions & 0 deletions lean/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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)
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 9eee6b4

Please sign in to comment.