30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
from argparse import Namespace, _SubParsersAction
|
|
from typing import Final
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
from mdrsclient.config import ConfigFile
|
|
from mdrsclient.connection import MDRSConnection
|
|
from mdrsclient.exceptions import MissingConfigurationException
|
|
|
|
|
|
class WhoamiCommand(BaseCommand):
|
|
ANONYMOUS_USERNAME: Final[str] = "(Anonymous)"
|
|
|
|
@classmethod
|
|
def register(cls, parsers: _SubParsersAction) -> None:
|
|
command = cls()
|
|
whoami_parser = parsers.add_parser("whoami", help="show current user name")
|
|
whoami_parser.add_argument("remote", help="label of remote host")
|
|
whoami_parser.set_defaults(func=command.whoami)
|
|
|
|
def whoami(self, args: Namespace) -> None:
|
|
remote = self._parse_remote_host(args.remote)
|
|
config = ConfigFile(remote)
|
|
if config.url is None:
|
|
raise MissingConfigurationException(f"Remote host `{remote}` is not found.")
|
|
connection = MDRSConnection(config.remote, config.url)
|
|
if connection.token is not None and connection.token.is_expired:
|
|
connection.logout()
|
|
username = connection.user.username if connection.user is not None else self.ANONYMOUS_USERNAME
|
|
print(username)
|