Starting with CLI
All checks were successful
/ backend-pylint (push) Successful in 9s

This commit is contained in:
Firq 2024-06-29 17:41:09 +02:00
parent bcafbeb3cf
commit 44ee9c7cad
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
11 changed files with 99 additions and 7 deletions

6
.gitignore vendored
View file

@ -1,6 +1,10 @@
# Environment
.vscode/
*venv/
# Python stuff
__pycache__/
*venv/
*.egg-info/
# Build artifacts
dist/

View file

@ -0,0 +1,4 @@
import importlib.metadata
__version__ = importlib.metadata.version(__package__ or "dockge_cli")
from .dockge_cli import cli

View file

@ -1 +0,0 @@
import argparse

View file

@ -0,0 +1,31 @@
from getpass import getpass
from urllib.parse import urlparse
from . import storage
def exec_command(command, extra_args):
match command:
case "login":
storage.set("username", input("Username: "), encoded=True)
storage.set("password", getpass("Password: "), encoded=True)
return
case "logout":
storage.remove("username")
storage.remove("password")
return
case "host":
if len(extra_args) > 0:
res = urlparse(extra_args[0])
if all([res.scheme, res.netloc]):
storage.set("host", extra_args[0])
else:
raise Exception(f"Malformed URL {extra_args[0]}")
return
print(storage.get("host"))
case "exit":
storage.clear()
return
case _:
print("Not implemented")
return

View file

@ -0,0 +1,28 @@
import argparse
import sys
from .. import __version__
commands = [
"host",
"login",
"logout",
"list",
"restart",
"update",
"exit"
]
class Arguments(argparse.Namespace):
command: str
parser = argparse.ArgumentParser(
prog="dockge_cli",
description="CLI interface for interacting with Dockge",)
parser.add_argument("command", choices=commands, action="store", type=str, default=None)
parser.add_argument("--version", action="version", version=f"dockge_cli {__version__}")
args = Arguments()
args, extra_args = parser.parse_known_args(sys.argv[1:], namespace=args)

View file

@ -1,3 +1,4 @@
import os
import pathlib
import base64
import yaml
@ -7,20 +8,37 @@ _file = _storagepath / "storage.yaml"
_storagepath.mkdir(exist_ok=True, parents=True)
def set(key: str, value: str):
with open(_file, "w+") as file:
def fileexists():
if not _file.exists():
with open(_file, 'a'):
os.utime(_file, None)
def set(key: str, value: str, encoded=False):
fileexists()
with open(_file, "r+") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
content.update({ key: base64.b64encode(value.encode()) })
content.update({ key: base64.b64encode(value.encode()) if encoded else value })
with open(_file, "w+") as file:
yaml.dump(content, file, Dumper=yaml.SafeDumper)
def get(key: str):
def remove(key: str):
fileexists()
with open(_file, "r") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
content.pop(key, None)
with open(_file, "w+") as file:
yaml.dump(content, file, Dumper=yaml.SafeDumper)
def get(key: str, encoded=False):
value: str = None
if not _file.exists():
return None
with open(_file, "r") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader)
value = content.get(key, None)
return base64.b64decode(value).decode() if value is not None else None
if value is None:
return None
return base64.b64decode(value).decode() if encoded else value
def clear():
_file.unlink()

5
dockge_cli/dockge_cli.py Normal file
View file

@ -0,0 +1,5 @@
from .components.parser import args, extra_args
from .components.exec import exec_command
def cli():
exec_command(args.command, extra_args)

View file

@ -16,6 +16,9 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
[project.scripts]
dockge-cli = "dockge_cli:cli"
[tool.setuptools.packages.find]
where = ["."]
include = ["dockge_cli*"]