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
__pycache__/
*.egg-info/
*.mypy_cache/
# Build artifacts
dist/

View file

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

View file

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

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 ..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()

View file

@ -1,4 +1,4 @@
from ..commands.factory import commands
from .commandprovider.factory import commands
def display_help(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 .components.run import run
from .client.parser import parse_arguments
from .client.run import run
def cli():
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()
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:

View file

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