diff --git a/.gitignore b/.gitignore index 2709b2f..460ee2f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Python stuff __pycache__/ *.egg-info/ +*.mypy_cache/ # Build artifacts dist/ diff --git a/dockge_cli/commands/__init__.py b/dockge_cli/client/__init__.py similarity index 100% rename from dockge_cli/commands/__init__.py rename to dockge_cli/client/__init__.py diff --git a/dockge_cli/components/__init__.py b/dockge_cli/client/commandprovider/__init__.py similarity index 100% rename from dockge_cli/components/__init__.py rename to dockge_cli/client/commandprovider/__init__.py diff --git a/dockge_cli/components/bindings.py b/dockge_cli/client/commandprovider/bindings.py similarity index 80% rename from dockge_cli/components/bindings.py rename to dockge_cli/client/commandprovider/bindings.py index aae7865..6e813de 100644 --- a/dockge_cli/components/bindings.py +++ b/dockge_cli/client/commandprovider/bindings.py @@ -1,10 +1,10 @@ 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 +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 @@ -100,18 +100,3 @@ class ExecutionCommands(): @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, -} diff --git a/dockge_cli/commands/descriptors.json b/dockge_cli/client/commandprovider/descriptors.py similarity index 63% rename from dockge_cli/commands/descriptors.json rename to dockge_cli/client/commandprovider/descriptors.py index 90407a9..cd346fd 100644 --- a/dockge_cli/commands/descriptors.json +++ b/dockge_cli/client/commandprovider/descriptors.py @@ -1,74 +1,88 @@ -[ +from .bindings import ExecutionCommands + +command_mappings = [ { "command": "host", "description": "Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI", "args": 1, - "optional": true + "optional": True, + "binding": ExecutionCommands.host }, { "command": "login", "description": "Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password", "args": 2, - "optional": true + "optional": True, + "binding": ExecutionCommands.login }, { "command": "logout", "description": "Removes the credentials from the local storage.", "args": 0, - "optional": false + "optional": False, + "binding": ExecutionCommands.logout }, { "command": "list", "description": "Lists all available stacks with their status", "args": 0, - "optional": false + "optional": False, + "binding": ExecutionCommands.list }, { "command": "status", "description": "Returns the status of one stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.status }, { "command": "restart", "description": "Restarts a given stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.restart }, { "command": "start", "description": "Starts a given stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.start }, { "command": "stop", "description": "Stops a given stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.stop }, { "command": "down", "description": "Stop & Downs a given stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.down }, { "command": "update", "description": "Updates a stack", "args": 1, - "optional": false + "optional": False, + "binding": ExecutionCommands.update }, { "command": "exit", "description": "Exits the CLI - this will reset all settings, including credentials and host", "args": 0, - "optional": false + "optional": False, + "binding": ExecutionCommands.exit }, { "command": "help", "description": "Displays helping hints for commands", "args": 1, - "optional": true + "optional": True, + "binding": ExecutionCommands.help } -] \ No newline at end of file +] diff --git a/dockge_cli/client/commandprovider/factory.py b/dockge_cli/client/commandprovider/factory.py new file mode 100644 index 0000000..4bfe95a --- /dev/null +++ b/dockge_cli/client/commandprovider/factory.py @@ -0,0 +1,17 @@ +from typing import List, Callable +from pydantic import BaseModel +from .descriptors import command_mappings + +class Command(BaseModel): + command: str + description: str + args: int + optional: bool + binding: Callable + +commands: dict[str, Command] = {} +descriptors: List[dict[str, object]] = command_mappings + +for descriptor in descriptors: + c = Command(**descriptor) # type: ignore + commands.update({ c.command: c }) diff --git a/dockge_cli/components/parser.py b/dockge_cli/client/parser.py similarity index 76% rename from dockge_cli/components/parser.py rename to dockge_cli/client/parser.py index 0cc13f6..2efbc30 100644 --- a/dockge_cli/components/parser.py +++ b/dockge_cli/client/parser.py @@ -3,14 +3,14 @@ import sys from .. import __version__ from ..models.parser import Arguments -from .bindings import binds +from .commandprovider.factory import commands def parse_arguments(): parser = argparse.ArgumentParser( prog="dockge_cli", description="CLI interface for interacting with Dockge",) - parser.add_argument("command", choices=list(binds.keys()), action="store", type=str, default=None) + parser.add_argument("command", choices=list(commands.keys()), action="store", type=str, default=None) parser.add_argument("--version", action="version", version=f"dockge_cli {__version__}") args = Arguments() diff --git a/dockge_cli/components/run.py b/dockge_cli/client/run.py similarity index 94% rename from dockge_cli/components/run.py rename to dockge_cli/client/run.py index f4bfe2a..105f380 100644 --- a/dockge_cli/components/run.py +++ b/dockge_cli/client/run.py @@ -1,4 +1,4 @@ -from ..commands.factory import commands +from .commandprovider.factory import commands def display_help(extra_args): if not extra_args: diff --git a/dockge_cli/components/utils.py b/dockge_cli/client/utils.py similarity index 100% rename from dockge_cli/components/utils.py rename to dockge_cli/client/utils.py diff --git a/dockge_cli/commands/factory.py b/dockge_cli/commands/factory.py deleted file mode 100644 index 5fc39e9..0000000 --- a/dockge_cli/commands/factory.py +++ /dev/null @@ -1,30 +0,0 @@ -import pathlib -import json -from typing import List, Callable -from pydantic import BaseModel - -from ..components.bindings import binds - -class Descriptor(BaseModel): - command: str - description: str - args: int - optional: bool - -class Command(Descriptor): - binding: Callable - -_descriptor_file = pathlib.Path(__file__).parent / "descriptors.json" - -commands: dict[str, Command] = {} - -with open(_descriptor_file, "r", encoding="utf-8") as file: - descriptors: List[Descriptor] = json.load(file) - for descriptor in descriptors: - commands.update({ - descriptor["command"]: - Command( - **descriptor, - binding=binds[descriptor["command"]] - ) - }) diff --git a/dockge_cli/dockge_cli.py b/dockge_cli/dockge_cli.py index bbbdc7e..25c437f 100644 --- a/dockge_cli/dockge_cli.py +++ b/dockge_cli/dockge_cli.py @@ -1,5 +1,5 @@ -from .components.parser import parse_arguments -from .components.run import run +from .client.parser import parse_arguments +from .client.run import run def cli(): command, args= parse_arguments() diff --git a/dockge_cli/py.typed b/dockge_cli/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/dockge_cli/service/__init__.py b/dockge_cli/service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dockge_cli/components/communicate.py b/dockge_cli/service/communicate.py similarity index 100% rename from dockge_cli/components/communicate.py rename to dockge_cli/service/communicate.py diff --git a/dockge_cli/components/storage.py b/dockge_cli/service/storage.py similarity index 91% rename from dockge_cli/components/storage.py rename to dockge_cli/service/storage.py index e2d6ace..0a3a0f8 100644 --- a/dockge_cli/components/storage.py +++ b/dockge_cli/service/storage.py @@ -17,7 +17,7 @@ def put(key: str, value: str, encoded=False): fileexists() with open(_file, "r+", encoding="utf-8") as file: content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {} - content.update({ key: base64.b64encode(value.encode()) if encoded else value }) + content.update({ key: str(base64.b64encode(value.encode())) if encoded else value }) with open(_file, "w+", encoding="utf-8") as file: yaml.dump(content, file, Dumper=yaml.SafeDumper) @@ -30,7 +30,7 @@ def remove(key: str): yaml.dump(content, file, Dumper=yaml.SafeDumper) def get(key: str, encoded=False): - value: str = None + value: str | None = None if not _file.exists(): return None with open(_file, "r", encoding="utf-8") as file: diff --git a/pyproject.toml b/pyproject.toml index ef7c2f9..50b898b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "dockge_cli" -version = "0.0.1-a.2" +version = "0.0.1-c.1" dependencies = [ "pyyaml~=6.0.1", "pydantic~=2.8.0", @@ -29,7 +29,7 @@ where = ["."] include = ["dockge_cli*"] [tool.setuptools.package-data] -"*" = ["descriptors.json"] +"*" = ["py.typed"] [tool.pylint."MAIN"] disable = [ "line-too-long", "missing-module-docstring", "missing-function-docstring", "missing-class-docstring" ]