2023-07-19 21:47:47 +09:00
|
|
|
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
|
2023-07-19 21:47:47 +09:00
|
|
|
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")
|
2023-07-19 21:47:47 +09:00
|
|
|
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")
|
2023-07-19 21:47:47 +09:00
|
|
|
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")
|
2023-07-19 21:47:47 +09:00
|
|
|
update_parser.set_defaults(func=cls.func_update)
|
2023-05-01 20:00:32 +09:00
|
|
|
# config list
|
2024-07-04 12:35:59 +09:00
|
|
|
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")
|
2023-07-19 21:47:47 +09:00
|
|
|
list_parser.set_defaults(func=cls.func_list)
|
2023-05-01 20:00:32 +09:00
|
|
|
# config delete
|
2024-07-04 12:35:59 +09:00
|
|
|
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")
|
2023-07-19 21:47:47 +09:00
|
|
|
delete_parser.set_defaults(func=cls.func_delete)
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-07-19 21:47:47 +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:
|
2023-07-19 21:47:47 +09:00
|
|
|
config.url = url
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-07-19 21:47:47 +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:
|
2023-07-19 21:47:47 +09:00
|
|
|
config.url = url
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-07-19 21:47:47 +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}:"
|
2023-07-19 21:47:47 +09:00
|
|
|
if is_long:
|
2023-05-01 20:00:32 +09:00
|
|
|
line += f"\t{url}"
|
|
|
|
print(line)
|
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@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
|