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.
This commit is contained in:
parent
6e075c88fa
commit
593f2ae1d8
@ -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()
|
||||
|
@ -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"]
|
||||
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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user