Refactored code to be more concise
This commit is contained in:
parent
d79a01cfb1
commit
58aea47921
13 changed files with 64 additions and 46 deletions
dockge_cli/client/commands
4
dockge_cli/client/commands/__init__.py
Normal file
4
dockge_cli/client/commands/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from ...models import Command
|
||||
from .mappings import mapping
|
||||
|
||||
commands: dict[str, Command] = { c.cmd: c for c in mapping }
|
145
dockge_cli/client/commands/functions.py
Normal file
145
dockge_cli/client/commands/functions.py
Normal file
|
@ -0,0 +1,145 @@
|
|||
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
|
||||
"""
|
||||
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
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
con = FunctionBindings.__setup()
|
||||
status_formatter(con.list_stack(extra_args[0]))
|
||||
con.disconnect()
|
||||
|
||||
@staticmethod
|
||||
def restart(extra_args):
|
||||
"""
|
||||
restart command binding
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
con = FunctionBindings.__setup()
|
||||
generic_formatter(con.restart(extra_args[0]))
|
||||
con.disconnect()
|
||||
|
||||
@staticmethod
|
||||
def update(extra_args):
|
||||
"""
|
||||
update command binding
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
con = FunctionBindings.__setup()
|
||||
generic_formatter(con.update(extra_args[0]))
|
||||
con.disconnect()
|
||||
|
||||
@staticmethod
|
||||
def stop(extra_args):
|
||||
"""
|
||||
stop command binding
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
con = FunctionBindings.__setup()
|
||||
generic_formatter(con.stop(extra_args[0]))
|
||||
con.disconnect()
|
||||
|
||||
@staticmethod
|
||||
def start(extra_args):
|
||||
"""
|
||||
start command binding
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
con = FunctionBindings.__setup()
|
||||
generic_formatter(con.start(extra_args[0]))
|
||||
con.disconnect()
|
||||
|
||||
@staticmethod
|
||||
def down(extra_args):
|
||||
"""
|
||||
down command binding
|
||||
"""
|
||||
if extra_args is None:
|
||||
raise ValueError
|
||||
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")
|
90
dockge_cli/client/commands/mappings.py
Normal file
90
dockge_cli/client/commands/mappings.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
from typing import List
|
||||
from ...models import Command
|
||||
from .functions import FunctionBindings
|
||||
|
||||
mapping: List[Command] = [
|
||||
Command(
|
||||
cmd="host",
|
||||
description="Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI",
|
||||
args=1,
|
||||
optional=True,
|
||||
func=FunctionBindings.host
|
||||
),
|
||||
Command(
|
||||
cmd="login",
|
||||
description="Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password",
|
||||
args=2,
|
||||
optional=True,
|
||||
func=FunctionBindings.login
|
||||
),
|
||||
Command(
|
||||
cmd="logout",
|
||||
description="Removes the credentials from the local storage.",
|
||||
args=0,
|
||||
optional=False,
|
||||
func=FunctionBindings.logout
|
||||
),
|
||||
Command(
|
||||
cmd="list",
|
||||
description="Lists all available stacks with their status",
|
||||
args=0,
|
||||
optional=False,
|
||||
func=FunctionBindings.list
|
||||
),
|
||||
Command(
|
||||
cmd="status",
|
||||
description="Returns the status of one stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.status
|
||||
),
|
||||
Command(
|
||||
cmd="restart",
|
||||
description="Restarts a given stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.restart
|
||||
),
|
||||
Command(
|
||||
cmd="start",
|
||||
description="Starts a given stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.start
|
||||
),
|
||||
Command(
|
||||
cmd="stop",
|
||||
description="Stops a given stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.stop
|
||||
),
|
||||
Command(
|
||||
cmd="down",
|
||||
description="Stop & Downs a given stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.down
|
||||
),
|
||||
Command(
|
||||
cmd="update",
|
||||
description="Updates a stack",
|
||||
args=1,
|
||||
optional=False,
|
||||
func=FunctionBindings.update
|
||||
),
|
||||
Command(
|
||||
cmd="exit",
|
||||
description="Exits the CLI - this will reset all settings, including credentials and host",
|
||||
args=0,
|
||||
optional=False,
|
||||
func=FunctionBindings.exit
|
||||
),
|
||||
Command(
|
||||
cmd="help",
|
||||
description="Displays helping hints for commands",
|
||||
args=1,
|
||||
optional=True,
|
||||
func=FunctionBindings.help
|
||||
)
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue