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
3 changed files with 19 additions and 3 deletions
|
@ -27,7 +27,7 @@ import click
|
||||||
from ...loader import PluginMeta
|
from ...loader import PluginMeta
|
||||||
from ..cliq.validators import PathValidator
|
from ..cliq.validators import PathValidator
|
||||||
from ..base import app
|
from ..base import app
|
||||||
from ..config import get_default_server, get_token
|
from ..config import get_token
|
||||||
from .upload import upload_file
|
from .upload import upload_file
|
||||||
|
|
||||||
yaml = YAML()
|
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("-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("-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)
|
@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 = {
|
data = {
|
||||||
"username": username,
|
"username": username,
|
||||||
"password": password,
|
"password": password,
|
||||||
|
@ -38,7 +39,11 @@ def login(server, username, password) -> None:
|
||||||
data=json.dumps(data).encode("utf-8")) as resp_data:
|
data=json.dumps(data).encode("utf-8")) as resp_data:
|
||||||
resp = json.load(resp_data)
|
resp = json.load(resp_data)
|
||||||
config["servers"][server] = resp["token"]
|
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()
|
save_config()
|
||||||
print(Fore.GREEN + "Logged in successfully")
|
print(Fore.GREEN + "Logged in successfully")
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
|
|
|
@ -21,6 +21,7 @@ from colorama import Fore
|
||||||
|
|
||||||
config: Dict[str, Any] = {
|
config: Dict[str, Any] = {
|
||||||
"servers": {},
|
"servers": {},
|
||||||
|
"aliases": {},
|
||||||
"default_server": None,
|
"default_server": None,
|
||||||
}
|
}
|
||||||
configdir = os.environ.get("XDG_CONFIG_HOME", os.path.join(os.environ.get("HOME"), ".config"))
|
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]]:
|
def get_token(server: str) -> Tuple[Optional[str], Optional[str]]:
|
||||||
if not server:
|
if not server:
|
||||||
return get_default_server()
|
return get_default_server()
|
||||||
|
if server in config["aliases"]:
|
||||||
|
server = config["aliases"][server]
|
||||||
return server, _get_token(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]:
|
def _get_token(server: str) -> Optional[str]:
|
||||||
try:
|
try:
|
||||||
return config["servers"][server]
|
return config["servers"][server]
|
||||||
|
@ -61,6 +71,7 @@ def load_config() -> None:
|
||||||
with open(f"{configdir}/maubot-cli.json") as file:
|
with open(f"{configdir}/maubot-cli.json") as file:
|
||||||
loaded = json.load(file)
|
loaded = json.load(file)
|
||||||
config["servers"] = loaded["servers"]
|
config["servers"] = loaded["servers"]
|
||||||
|
config["aliases"] = loaded["aliases"]
|
||||||
config["default_server"] = loaded["default_server"]
|
config["default_server"] = loaded["default_server"]
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue