2023-05-01 20:00:32 +09:00
|
|
|
import getpass
|
|
|
|
from argparse import Namespace, _SubParsersAction
|
|
|
|
|
|
|
|
from mdrsclient.api import UserApi
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
from mdrsclient.commands.utils import parse_remote_host
|
|
|
|
from mdrsclient.config import ConfigFile
|
2023-05-09 13:08:50 +09:00
|
|
|
from mdrsclient.connection import MDRSConnection
|
2023-05-01 20:00:32 +09:00
|
|
|
from mdrsclient.exceptions import MissingConfigurationException
|
|
|
|
|
|
|
|
|
|
|
|
class UserCommand(BaseCommand):
|
|
|
|
@staticmethod
|
|
|
|
def register(top_level_subparsers: _SubParsersAction) -> None:
|
|
|
|
# login
|
|
|
|
login_parser = top_level_subparsers.add_parser("login", help="login to remote host")
|
|
|
|
login_parser.add_argument("remote", help="Label of remote host")
|
|
|
|
login_parser.set_defaults(func=UserCommand.login)
|
|
|
|
# logout
|
|
|
|
logout_parser = top_level_subparsers.add_parser("logout", help="logout from remote host")
|
|
|
|
logout_parser.add_argument("remote", help="Label of remote host")
|
|
|
|
logout_parser.set_defaults(func=UserCommand.logout)
|
|
|
|
# whoami
|
|
|
|
whoami_parser = top_level_subparsers.add_parser("whoami", help="show current user name")
|
|
|
|
whoami_parser.add_argument("remote", help="Label of remote host")
|
|
|
|
whoami_parser.set_defaults(func=UserCommand.whoami)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def login(args: Namespace) -> None:
|
|
|
|
remote = parse_remote_host(args.remote)
|
|
|
|
config = ConfigFile(remote)
|
|
|
|
if config.url is None:
|
|
|
|
raise MissingConfigurationException(f"Remote host `{remote}` is not found.")
|
2023-05-09 13:08:50 +09:00
|
|
|
connection = MDRSConnection(config.remote, config.url)
|
2023-05-01 20:00:32 +09:00
|
|
|
username = input("Username: ").strip()
|
|
|
|
password = getpass.getpass("Password: ").strip()
|
2023-05-09 13:08:50 +09:00
|
|
|
user_api = UserApi(connection)
|
2023-05-01 20:00:32 +09:00
|
|
|
(user, token) = user_api.auth(username, password)
|
2023-05-09 13:08:50 +09:00
|
|
|
connection.user = user
|
|
|
|
connection.token = token
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def logout(args: Namespace) -> None:
|
|
|
|
remote = parse_remote_host(args.remote)
|
|
|
|
config = ConfigFile(remote)
|
|
|
|
if config.url is None:
|
|
|
|
raise MissingConfigurationException(f"Remote host `{remote}` is not found.")
|
2023-05-09 13:08:50 +09:00
|
|
|
connection = MDRSConnection(config.remote, config.url)
|
|
|
|
connection.logout()
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def whoami(args: Namespace) -> None:
|
|
|
|
remote = parse_remote_host(args.remote)
|
|
|
|
config = ConfigFile(remote)
|
|
|
|
if config.url is None:
|
|
|
|
raise MissingConfigurationException(f"Remote host `{remote}` is not found.")
|
2023-05-09 13:08:50 +09:00
|
|
|
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 "(Anonymous)"
|
2023-05-01 20:00:32 +09:00
|
|
|
print(username)
|