parent
bcafbeb3cf
commit
44ee9c7cad
11 changed files with 99 additions and 7 deletions
dockge_cli/components
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue