dockge-cli/dockge_cli/client/commandprovider/bindings.py

143 lines
3.9 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 import Credentials
from ...service import storage
from ...service.communicate import DockgeConnection
from ..utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser
2024-07-03 18:36:22 +00:00
class ExecutionCommands():
2024-07-05 14:05:35 +00:00
"""
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
"""
2024-07-03 18:36:22 +00:00
@staticmethod
def __setup():
con = DockgeConnection()
con.connect_and_login()
return con
@staticmethod
def host(extra_args):
2024-07-05 14:05:35 +00:00
"""
host command binding
"""
2024-07-03 18:36:22 +00:00
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):
2024-07-05 14:05:35 +00:00
"""
login command binding
"""
2024-07-03 18:36:22 +00:00
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(_):
2024-07-05 14:05:35 +00:00
"""
logout command binding
"""
2024-07-03 18:36:22 +00:00
storage.remove("username")
storage.remove("password")
@staticmethod
def exit(_):
2024-07-05 14:05:35 +00:00
"""
exit command binding
"""
2024-07-03 18:36:22 +00:00
storage.clear()
@staticmethod
def list(_):
2024-07-05 14:05:35 +00:00
"""
list command binding
"""
2024-07-03 18:36:22 +00:00
con = ExecutionCommands.__setup()
stack_formatter(con.list_stacks())
con.disconnect()
@staticmethod
def status(extra_args):
2024-07-05 14:05:35 +00:00
"""
status command binding
"""
2024-07-03 18:36:22 +00:00
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):
2024-07-05 14:05:35 +00:00
"""
restart command binding
"""
2024-07-03 18:36:22 +00:00
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.restart(extra_args[0]))
con.disconnect()
@staticmethod
def update(extra_args):
2024-07-05 14:05:35 +00:00
"""
update command binding
"""
2024-07-03 18:36:22 +00:00
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.update(extra_args[0]))
con.disconnect()
@staticmethod
def stop(extra_args):
2024-07-05 14:05:35 +00:00
"""
stop command binding
"""
2024-07-03 18:36:22 +00:00
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.stop(extra_args[0]))
con.disconnect()
@staticmethod
def start(extra_args):
2024-07-05 14:05:35 +00:00
"""
start command binding
"""
2024-07-03 18:36:22 +00:00
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.start(extra_args[0]))
con.disconnect()
@staticmethod
def down(extra_args):
2024-07-05 14:05:35 +00:00
"""
down command binding
"""
2024-07-03 18:36:22 +00:00
if extra_args is None:
raise ValueError
con = ExecutionCommands.__setup()
generic_formatter(con.down(extra_args[0]))
con.disconnect()
@staticmethod
def help():
2024-07-05 14:05:35 +00:00
"""
exit command binding - This should never be invoked
"""
2024-07-03 18:36:22 +00:00
print("WTF")