dockge-cli/dockge_cli/client/commands/functions.py
Firq f5ffe07e77
All checks were successful
/ pylint (push) Successful in 11s
/ mypy (push) Successful in 12s
Renamed communication to connection
2024-07-12 23:25:52 +02:00

138 lines
3.8 KiB
Python

from urllib.parse import urlparse
from getpass import getpass
import re
from ...models import Credentials
from ...service import storage
from ...service.connection import DockgeConnection
from ..utils import stack_formatter, status_formatter, generic_formatter, credential_parser_factory
class FunctionBindings():
"""
Helper class that provides all the static methods in an organized way
This is an abstraction layer of the CLI, as those functions only do little preprocessing before calling the actural DockgeConnection
"""
@staticmethod
def __setup():
"""
Creates a connection and logs into Dockge
"""
con = DockgeConnection()
con.connect_and_login()
return con
@staticmethod
def host(extra_args):
"""
host command binding
"""
if len(extra_args) > 0:
mat = re.search(r"((\w+\.)?\w+\.\w+(\/.+)?)", extra_args[0], re.IGNORECASE)
if mat is None:
raise ValueError("Given host did not match regex")
res = urlparse(f"https://{mat[0]}")
if all([res.scheme, res.netloc]):
storage.put("host", mat[0])
else:
raise ValueError(f"Malformed URL {extra_args[0]}")
print(storage.get("host"))
@staticmethod
def login(extra_args):
"""
login command binding
"""
print(f"WARNING! These credentials will be saved unencrypted in {storage._file.absolute()}")
if len(extra_args) > 0:
credentials = credential_parser_factory().parse_args(extra_args, namespace=Credentials)
storage.put("username", credentials.username, encoded=True)
storage.put("password", credentials.password, encoded=True)
return
storage.put("username", input("Username: "), encoded=True)
storage.put("password", getpass("Password: "), encoded=True)
@staticmethod
def logout(_):
"""
logout command binding
"""
storage.remove("username")
storage.remove("password")
@staticmethod
def exit(_):
"""
exit command binding
"""
storage.clear()
@staticmethod
def list(_):
"""
list command binding
"""
con = FunctionBindings.__setup()
stack_formatter(con.list_stacks())
con.disconnect()
@staticmethod
def status(extra_args):
"""
status command binding
"""
con = FunctionBindings.__setup()
status_formatter(con.list_stack(extra_args[0]))
con.disconnect()
@staticmethod
def restart(extra_args):
"""
restart command binding
"""
con = FunctionBindings.__setup()
generic_formatter(con.restart(extra_args[0]))
con.disconnect()
@staticmethod
def update(extra_args):
"""
update command binding
"""
con = FunctionBindings.__setup()
generic_formatter(con.update(extra_args[0]))
con.disconnect()
@staticmethod
def stop(extra_args):
"""
stop command binding
"""
con = FunctionBindings.__setup()
generic_formatter(con.stop(extra_args[0]))
con.disconnect()
@staticmethod
def start(extra_args):
"""
start command binding
"""
con = FunctionBindings.__setup()
generic_formatter(con.start(extra_args[0]))
con.disconnect()
@staticmethod
def down(extra_args):
"""
down command binding
"""
con = FunctionBindings.__setup()
generic_formatter(con.down(extra_args[0]))
con.disconnect()
@staticmethod
def help():
"""
exit command binding - This should never be invoked
"""
print("WTF")