From 593f2ae1d8836375bc4dac84cfa9e97d710c93bc Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 22 Apr 2020 23:09:05 +0300 Subject: [PATCH] Add maubot server alias feature This means you can add an alias for a maubot server when logging in, and then use that alias instead of typing the full URL each time. Mostly useful for people with more than one maubot instance, as the default server already didn't need to be typed out each time. --- maubot/cli/commands/build.py | 2 +- maubot/cli/commands/login.py | 9 +++++++-- maubot/cli/config.py | 11 +++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/maubot/cli/commands/build.py b/maubot/cli/commands/build.py index 8eddae8..c0969dd 100644 --- a/maubot/cli/commands/build.py +++ b/maubot/cli/commands/build.py @@ -27,7 +27,7 @@ import click from ...loader import PluginMeta from ..cliq.validators import PathValidator from ..base import app -from ..config import get_default_server, get_token +from ..config import get_token from .upload import upload_file yaml = YAML() diff --git a/maubot/cli/commands/login.py b/maubot/cli/commands/login.py index 1d2f3bb..fdf71b3 100644 --- a/maubot/cli/commands/login.py +++ b/maubot/cli/commands/login.py @@ -28,7 +28,8 @@ from ..cliq import cliq @cliq.option("-u", "--username", help="The username of your account", default=os.environ.get("USER", None), required=True) @cliq.option("-p", "--password", help="The password to your account", inq_type="password", required=True) @cliq.option("-s", "--server", help="The server to log in to", default="http://localhost:29316", required=True) -def login(server, username, password) -> None: +@cliq.option("-a", "--alias", help="Alias to reference the server without typing the full URL", default="", required=False) +def login(server, username, password, alias) -> None: data = { "username": username, "password": password, @@ -38,7 +39,11 @@ def login(server, username, password) -> None: data=json.dumps(data).encode("utf-8")) as resp_data: resp = json.load(resp_data) config["servers"][server] = resp["token"] - config["default_server"] = server + if not config["default_server"]: + print(Fore.CYAN, "Setting", server, "as the default server") + config["default_server"] = server + if alias: + config["aliases"][alias] = server save_config() print(Fore.GREEN + "Logged in successfully") except HTTPError as e: diff --git a/maubot/cli/config.py b/maubot/cli/config.py index 5be4ec7..4fd5c2b 100644 --- a/maubot/cli/config.py +++ b/maubot/cli/config.py @@ -21,6 +21,7 @@ from colorama import Fore config: Dict[str, Any] = { "servers": {}, + "aliases": {}, "default_server": None, } configdir = os.environ.get("XDG_CONFIG_HOME", os.path.join(os.environ.get("HOME"), ".config")) @@ -40,9 +41,18 @@ def get_default_server() -> Tuple[Optional[str], Optional[str]]: def get_token(server: str) -> Tuple[Optional[str], Optional[str]]: if not server: return get_default_server() + if server in config["aliases"]: + server = config["aliases"][server] return server, _get_token(server) +def _resolve_alias(alias: str) -> Optional[str]: + try: + return config["aliases"][alias] + except KeyError: + return None + + def _get_token(server: str) -> Optional[str]: try: return config["servers"][server] @@ -61,6 +71,7 @@ def load_config() -> None: with open(f"{configdir}/maubot-cli.json") as file: loaded = json.load(file) config["servers"] = loaded["servers"] + config["aliases"] = loaded["aliases"] config["default_server"] = loaded["default_server"] except FileNotFoundError: pass