Improved typing, refactored code, fixed some issues
All checks were successful
/ backend-pylint (push) Successful in 13s
/ build-artifacts (push) Successful in 8s
/ publish-artifacts (push) Successful in 20s

This commit is contained in:
Firq 2024-07-04 22:12:36 +02:00
parent 06cece6242
commit d3e5d4ae56
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
16 changed files with 59 additions and 72 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@
# Python stuff # Python stuff
__pycache__/ __pycache__/
*.egg-info/ *.egg-info/
*.mypy_cache/
# Build artifacts # Build artifacts
dist/ dist/

View file

@ -1,10 +1,10 @@
from urllib.parse import urlparse from urllib.parse import urlparse
from getpass import getpass from getpass import getpass
from ..models.parser import Credentials from ...models.parser import Credentials
from . import storage from ...service import storage
from .utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser from ..utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser
from .communicate import DockgeConnection from ...service.communicate import DockgeConnection
class ExecutionCommands(): class ExecutionCommands():
@staticmethod @staticmethod
@ -100,18 +100,3 @@ class ExecutionCommands():
@staticmethod @staticmethod
def help(): def help():
print("WTF") 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,
}

View file

@ -1,74 +1,88 @@
[ from .bindings import ExecutionCommands
command_mappings = [
{ {
"command": "host", "command": "host",
"description": "Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI", "description": "Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI",
"args": 1, "args": 1,
"optional": true "optional": True,
"binding": ExecutionCommands.host
}, },
{ {
"command": "login", "command": "login",
"description": "Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password", "description": "Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password",
"args": 2, "args": 2,
"optional": true "optional": True,
"binding": ExecutionCommands.login
}, },
{ {
"command": "logout", "command": "logout",
"description": "Removes the credentials from the local storage.", "description": "Removes the credentials from the local storage.",
"args": 0, "args": 0,
"optional": false "optional": False,
"binding": ExecutionCommands.logout
}, },
{ {
"command": "list", "command": "list",
"description": "Lists all available stacks with their status", "description": "Lists all available stacks with their status",
"args": 0, "args": 0,
"optional": false "optional": False,
"binding": ExecutionCommands.list
}, },
{ {
"command": "status", "command": "status",
"description": "Returns the status of one stack", "description": "Returns the status of one stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.status
}, },
{ {
"command": "restart", "command": "restart",
"description": "Restarts a given stack", "description": "Restarts a given stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.restart
}, },
{ {
"command": "start", "command": "start",
"description": "Starts a given stack", "description": "Starts a given stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.start
}, },
{ {
"command": "stop", "command": "stop",
"description": "Stops a given stack", "description": "Stops a given stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.stop
}, },
{ {
"command": "down", "command": "down",
"description": "Stop & Downs a given stack", "description": "Stop & Downs a given stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.down
}, },
{ {
"command": "update", "command": "update",
"description": "Updates a stack", "description": "Updates a stack",
"args": 1, "args": 1,
"optional": false "optional": False,
"binding": ExecutionCommands.update
}, },
{ {
"command": "exit", "command": "exit",
"description": "Exits the CLI - this will reset all settings, including credentials and host", "description": "Exits the CLI - this will reset all settings, including credentials and host",
"args": 0, "args": 0,
"optional": false "optional": False,
"binding": ExecutionCommands.exit
}, },
{ {
"command": "help", "command": "help",
"description": "Displays helping hints for commands", "description": "Displays helping hints for commands",
"args": 1, "args": 1,
"optional": true "optional": True,
"binding": ExecutionCommands.help
} }
] ]

View file

@ -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 })

View file

@ -3,14 +3,14 @@ import sys
from .. import __version__ from .. import __version__
from ..models.parser import Arguments from ..models.parser import Arguments
from .bindings import binds from .commandprovider.factory import commands
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="dockge_cli", prog="dockge_cli",
description="CLI interface for interacting with Dockge",) 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__}") parser.add_argument("--version", action="version", version=f"dockge_cli {__version__}")
args = Arguments() args = Arguments()

View file

@ -1,4 +1,4 @@
from ..commands.factory import commands from .commandprovider.factory import commands
def display_help(extra_args): def display_help(extra_args):
if not extra_args: if not extra_args:

View file

@ -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"]]
)
})

View file

@ -1,5 +1,5 @@
from .components.parser import parse_arguments from .client.parser import parse_arguments
from .components.run import run from .client.run import run
def cli(): def cli():
command, args= parse_arguments() command, args= parse_arguments()

0
dockge_cli/py.typed Normal file
View file

View file

View file

@ -17,7 +17,7 @@ def put(key: str, value: str, encoded=False):
fileexists() fileexists()
with open(_file, "r+", encoding="utf-8") as file: with open(_file, "r+", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {} 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: with open(_file, "w+", encoding="utf-8") as file:
yaml.dump(content, file, Dumper=yaml.SafeDumper) yaml.dump(content, file, Dumper=yaml.SafeDumper)
@ -30,7 +30,7 @@ def remove(key: str):
yaml.dump(content, file, Dumper=yaml.SafeDumper) yaml.dump(content, file, Dumper=yaml.SafeDumper)
def get(key: str, encoded=False): def get(key: str, encoded=False):
value: str = None value: str | None = None
if not _file.exists(): if not _file.exists():
return None return None
with open(_file, "r", encoding="utf-8") as file: with open(_file, "r", encoding="utf-8") as file:

View file

@ -1,6 +1,6 @@
[project] [project]
name = "dockge_cli" name = "dockge_cli"
version = "0.0.1-a.2" version = "0.0.1-c.1"
dependencies = [ dependencies = [
"pyyaml~=6.0.1", "pyyaml~=6.0.1",
"pydantic~=2.8.0", "pydantic~=2.8.0",
@ -29,7 +29,7 @@ where = ["."]
include = ["dockge_cli*"] include = ["dockge_cli*"]
[tool.setuptools.package-data] [tool.setuptools.package-data]
"*" = ["descriptors.json"] "*" = ["py.typed"]
[tool.pylint."MAIN"] [tool.pylint."MAIN"]
disable = [ "line-too-long", "missing-module-docstring", "missing-function-docstring", "missing-class-docstring" ] disable = [ "line-too-long", "missing-module-docstring", "missing-function-docstring", "missing-class-docstring" ]