dockge-cli/dockge_cli/components/bindings.py

118 lines
3.4 KiB
Python
Raw Normal View History

2024-07-03 18:36:22 +00:00
from urllib.parse import urlparse
from getpass import getpass
from ..models.parser import Credentials
from . import storage
from .utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser
from .communicate import DockgeConnection
class ExecutionCommands():
@staticmethod
def __setup():
con = DockgeConnection()
con.connect_and_login()
return con
@staticmethod
def host(extra_args):
if len(extra_args) > 0:
res = urlparse(extra_args[0])
if all([res.scheme, res.netloc]):
host = extra_args[0].rstrip("/").replace("https://", "").replace("wss://", "")
storage.put("host", host)
else:
raise ValueError(f"Malformed URL {extra_args[0]}")
print(storage.get("host"))
@staticmethod
def login(extra_args):
if len(extra_args) > 0:
credentials = get_credential_parser().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(_):
storage.remove("username")
storage.remove("password")
@staticmethod
def exit(_):
storage.clear()
@staticmethod
def list(_):
con = ExecutionCommands.__setup()
stack_formatter(con.list_stacks())
con.disconnect()
@staticmethod
def status(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
status_formatter(con.list_stack(extra_args[0]))
con.disconnect()
@staticmethod
def restart(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.restart(extra_args[0]))
con.disconnect()
@staticmethod
def update(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.update(extra_args[0]))
con.disconnect()
@staticmethod
def stop(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.stop(extra_args[0]))
con.disconnect()
@staticmethod
def start(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.start(extra_args[0]))
con.disconnect()
@staticmethod
def down(extra_args):
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.down(extra_args[0]))
con.disconnect()
@staticmethod
def help():
print("WTF")
binds = {
"host": ExecutionCommands.host,
"login": ExecutionCommands.login,
"logout": ExecutionCommands.logout,
"start": ExecutionCommands.start,
"restart": ExecutionCommands.restart,
"stop": ExecutionCommands.stop,
"down": ExecutionCommands.down,
"exit": ExecutionCommands.exit,
"list": ExecutionCommands.list,
"status": ExecutionCommands.status,
"update": ExecutionCommands.update,
"help": ExecutionCommands.help,
}