dockge-cli/dockge_cli/client/commands/functions.py
Firq c00aa232fc
All checks were successful
/ pylint (push) Successful in 11s
/ mypy (push) Successful in 12s
/ build-artifacts (push) Successful in 7s
/ lint-and-typing (push) Successful in 14s
/ publish-artifacts (push) Successful in 9s
Moved files, refactored calls, added credentials warning
2024-07-06 00:28:37 +02:00

135 lines
3.7 KiB
Python

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, 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:
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):
"""
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")