mdrs-client-python/mdrsclient/commands/config.py

93 lines
3.5 KiB
Python
Raw Permalink Normal View History

from argparse import Namespace
from typing import Any, Callable
2023-05-01 20:00:32 +09:00
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: Any) -> None:
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")
func_help: Callable[[Namespace], None] = lambda _: config_parser.print_help()
config_parser.set_defaults(func=func_help)
2023-05-10 18:17:35 +09:00
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")
create_parser.set_defaults(func=cls.func_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")
update_parser.set_defaults(func=cls.func_update)
2023-05-01 20:00:32 +09:00
# config list
list_parser = config_parsers.add_parser("list", help="list all the remote hosts", aliases=["ls"])
2023-05-10 18:17:35 +09:00
list_parser.add_argument("-l", "--long", help="show the api url", action="store_true")
list_parser.set_defaults(func=cls.func_list)
2023-05-01 20:00:32 +09:00
# config delete
delete_parser = config_parsers.add_parser("delete", help="delete an existing remote host", aliases=["remove"])
2023-05-10 18:17:35 +09:00
delete_parser.add_argument("remote", help="label of remote host")
delete_parser.set_defaults(func=cls.func_delete)
2023-05-01 20:00:32 +09:00
@classmethod
def func_create(cls, args: Namespace) -> None:
remote = str(args.remote)
url = str(args.url)
cls.create(remote, url)
@classmethod
def func_update(cls, args: Namespace) -> None:
remote = str(args.remote)
url = str(args.url)
cls.update(remote, url)
@classmethod
def func_list(cls, args: Namespace) -> None:
is_long = bool(args.long)
cls.list(is_long)
@classmethod
def func_delete(cls, args: Namespace) -> None:
remote = str(args.remote)
cls.delete(remote)
@classmethod
def create(cls, remote: str, url: str) -> None:
remote = cls._parse_remote_host(remote)
config = ConfigFile(remote)
2023-05-01 20:00:32 +09:00
if config.url is not None:
raise IllegalArgumentException(f"Remote host `{remote}` is already exists.")
else:
config.url = url
2023-05-01 20:00:32 +09:00
@classmethod
def update(cls, remote: str, url: str) -> None:
remote = cls._parse_remote_host(remote)
config = ConfigFile(remote)
2023-05-01 20:00:32 +09:00
if config.url is None:
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
else:
config.url = url
2023-05-01 20:00:32 +09:00
@classmethod
def list(cls, is_long: bool) -> None:
2023-05-01 20:00:32 +09:00
config = ConfigFile("")
for remote, url in config.list():
line = f"{remote}:"
if is_long:
2023-05-01 20:00:32 +09:00
line += f"\t{url}"
print(line)
@classmethod
def delete(cls, remote: str) -> None:
remote = cls._parse_remote_host(remote)
config = ConfigFile(remote)
2023-05-01 20:00:32 +09:00
if config.url is None:
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
else:
del config.url