66 lines
2.9 KiB
Python
66 lines
2.9 KiB
Python
from argparse import Namespace, _SubParsersAction
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
from mdrsclient.config import ConfigFile
|
|
from mdrsclient.exceptions import IllegalArgumentException
|
|
|
|
|
|
class ConfigCommand(BaseCommand):
|
|
@classmethod
|
|
def register(cls, parsers: _SubParsersAction) -> None:
|
|
command = cls()
|
|
# config
|
|
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")
|
|
# config create
|
|
create_parser = config_parsers.add_parser("create", help="create a new remote host")
|
|
create_parser.add_argument("remote", help="label of remote host")
|
|
create_parser.add_argument("url", help="API entrypoint url of remote host")
|
|
create_parser.set_defaults(func=command.create)
|
|
# config update
|
|
update_parser = config_parsers.add_parser("update", help="update a new remote host")
|
|
update_parser.add_argument("remote", help="label of remote host")
|
|
update_parser.add_argument("url", help="API entrypoint url of remote host")
|
|
update_parser.set_defaults(func=command.update)
|
|
# config list
|
|
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)
|
|
# config delete
|
|
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)
|
|
|
|
def create(self, args: Namespace) -> None:
|
|
remote = self._parse_remote_host(args.remote)
|
|
config = ConfigFile(remote=remote)
|
|
if config.url is not None:
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is already exists.")
|
|
else:
|
|
config.url = args.url
|
|
|
|
def update(self, args: Namespace) -> None:
|
|
remote = self._parse_remote_host(args.remote)
|
|
config = ConfigFile(remote=remote)
|
|
if config.url is None:
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
|
|
else:
|
|
config.url = args.url
|
|
|
|
def list(self, args: Namespace) -> None:
|
|
config = ConfigFile("")
|
|
for remote, url in config.list():
|
|
line = f"{remote}:"
|
|
if args.long:
|
|
line += f"\t{url}"
|
|
print(line)
|
|
|
|
def delete(self, args: Namespace) -> None:
|
|
remote = self._parse_remote_host(args.remote)
|
|
config = ConfigFile(remote=remote)
|
|
if config.url is None:
|
|
raise IllegalArgumentException(f"Remote host `{remote}` is not exists.")
|
|
else:
|
|
del config.url
|