dockge-cli/dockge_cli/client/commandprovider/bindings.py
Firq d3e5d4ae56
All checks were successful
/ backend-pylint (push) Successful in 13s
/ build-artifacts (push) Successful in 8s
/ publish-artifacts (push) Successful in 20s
Improved typing, refactored code, fixed some issues
2024-07-04 22:12:36 +02:00

103 lines
3 KiB
Python

from urllib.parse import urlparse
from getpass import getpass
from ...models.parser import Credentials
from ...service import storage
from ..utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser
from ...service.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")