32 lines
1.2 KiB
Python

import getpass
from argparse import Namespace, _SubParsersAction
from mdrsclient.api import UserApi
from mdrsclient.commands.base import BaseCommand
from mdrsclient.config import ConfigFile
from mdrsclient.connection import MDRSConnection
from mdrsclient.exceptions import MissingConfigurationException
class LoginCommand(BaseCommand):
@classmethod
def register(cls, parsers: _SubParsersAction) -> None:
command = cls()
login_parser = parsers.add_parser("login", help="login to remote host")
login_parser.add_argument("remote", help="label of remote host")
login_parser.set_defaults(func=command.login)
def login(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)
username = input("Username: ").strip()
password = getpass.getpass("Password: ").strip()
user_api = UserApi(connection)
(user, token) = user_api.auth(username, password)
print("Login Successful")
connection.user = user
connection.token = token