2023-05-01 20:00:32 +09:00
|
|
|
from argparse import Namespace, _SubParsersAction
|
|
|
|
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
from mdrsclient.config import ConfigFile
|
|
|
|
from mdrsclient.exceptions import IllegalArgumentException
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigCommand(BaseCommand):
|
2023-05-10 18:17:35 +09:00
|
|
|
@classmethod
|
|
|
|
def register(cls, parsers: _SubParsersAction) -> None:
|
|
|
|
command = cls()
|
2023-05-01 20:00:32 +09:00
|
|
|
# config
|
2023-05-10 18:17:35 +09:00
|
|
|
config_parser = parsers.add_parser("config", help="configure remote hosts")
|
|
|
|
config_parser.set_defaults(func=lambda x: config_parser.print_help())
|
|
|
|
config_parsers = config_parser.add_subparsers(title="config subcommands")
|
2023-05-01 20:00:32 +09:00
|
|
|
# config create
|
2023-05-10 18:17:35 +09:00
|
|
|
create_parser = config_parsers.add_parser("create", help="create a new remote host")
|
|
|
|
create_parser.add_argument("remote", help="label of remote host")
|
2023-05-01 20:00:32 +09:00
|
|
|
create_parser.add_argument("url", help="API entrypoint url of remote host")
|
2023-05-10 18:17:35 +09:00
|
|
|
create_parser.set_defaults(func=command.create)
|
2023-05-01 20:00:32 +09:00
|
|
|
# config update
|
2023-05-10 18:17:35 +09:00
|
|
|
update_parser = config_parsers.add_parser("update", help="update a new remote host")
|
|
|
|
update_parser.add_argument("remote", help="label of remote host")
|
2023-05-01 20:00:32 +09:00
|
|
|
update_parser.add_argument("url", help="API entrypoint url of remote host")
|
2023-05-10 18:17:35 +09:00
|
|
|
update_parser.set_defaults(func=command.update)
|
2023-05-01 20:00:32 +09:00
|
|
|
# config list
|
2023-05-10 18:17:35 +09:00
|
|
|
list_parser = config_parsers.add_parser("list", help="list all the remote hosts")
|
|
|
|
list_parser.add_argument("-l", "--long", help="show the api url", action="store_true")
|
|
|
|
list_parser.set_defaults(func=command.list)
|
2023-05-01 20:00:32 +09:00
|
|
|
# config delete
|
2023-05-10 18:17:35 +09:00
|
|
|
delete_parser = config_parsers.add_parser("delete", help="delete an existing remote host")
|
|
|
|
delete_parser.add_argument("remote", help="label of remote host")
|
|
|
|
delete_parser.set_defaults(func=command.delete)
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
def create(self, args: Namespace) -> None:
|
|
|
|
remote = self._parse_remote_host(args.remote)
|
2023-05-01 20:00:32 +09:00
|
|
|
config = ConfigFile(remote=remote)
|
|
|
|
if config.url is not None:
|
|
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is already exists.")
|
|
|
|
else:
|
|
|
|
config.url = args.url
|
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
def update(self, args: Namespace) -> None:
|
|
|
|
remote = self._parse_remote_host(args.remote)
|
2023-05-01 20:00:32 +09:00
|
|
|
config = ConfigFile(remote=remote)
|
|
|
|
if config.url is None:
|
|
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
|
|
|
|
else:
|
|
|
|
config.url = args.url
|
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
def list(self, args: Namespace) -> None:
|
2023-05-01 20:00:32 +09:00
|
|
|
config = ConfigFile("")
|
|
|
|
for remote, url in config.list():
|
|
|
|
line = f"{remote}:"
|
|
|
|
if args.long:
|
|
|
|
line += f"\t{url}"
|
|
|
|
print(line)
|
|
|
|
|
2023-05-10 18:17:35 +09:00
|
|
|
def delete(self, args: Namespace) -> None:
|
|
|
|
remote = self._parse_remote_host(args.remote)
|
2023-05-01 20:00:32 +09:00
|
|
|
config = ConfigFile(remote=remote)
|
|
|
|
if config.url is None:
|
|
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
|
|
|
|
else:
|
|
|
|
del config.url
|