This commit is contained in:
parent
bcafbeb3cf
commit
44ee9c7cad
11 changed files with 99 additions and 7 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,6 +1,10 @@
|
|||
# Environment
|
||||
.vscode/
|
||||
*venv/
|
||||
|
||||
# Python stuff
|
||||
__pycache__/
|
||||
*venv/
|
||||
*.egg-info/
|
||||
|
||||
# Build artifacts
|
||||
dist/
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
import importlib.metadata
|
||||
__version__ = importlib.metadata.version(__package__ or "dockge_cli")
|
||||
|
||||
from .dockge_cli import cli
|
|
@ -1 +0,0 @@
|
|||
import argparse
|
31
dockge_cli/components/exec.py
Normal file
31
dockge_cli/components/exec.py
Normal 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
|
||||
|
28
dockge_cli/components/parser.py
Normal file
28
dockge_cli/components/parser.py
Normal 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)
|
|
@ -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
5
dockge_cli/dockge_cli.py
Normal 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)
|
|
@ -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*"]
|
||||
|
|
Loading…
Reference in a new issue