From c00aa232fc68ebcc3745ed6a268ab0ae95a78d6e Mon Sep 17 00:00:00 2001 From: Firq Date: Sat, 6 Jul 2024 00:28:37 +0200 Subject: [PATCH] Moved files, refactored calls, added credentials warning --- dockge_cli/.gitignore | 1 - dockge_cli/client/commands/functions.py | 13 +---------- dockge_cli/service/.gitignore | 1 + dockge_cli/service/storage.py | 30 ++++++++++++++++--------- pyproject.toml | 2 +- 5 files changed, 23 insertions(+), 24 deletions(-) delete mode 100644 dockge_cli/.gitignore create mode 100644 dockge_cli/service/.gitignore diff --git a/dockge_cli/.gitignore b/dockge_cli/.gitignore deleted file mode 100644 index d36a0d4..0000000 --- a/dockge_cli/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.temp/* \ No newline at end of file diff --git a/dockge_cli/client/commands/functions.py b/dockge_cli/client/commands/functions.py index daa0ed2..f62ed9c 100644 --- a/dockge_cli/client/commands/functions.py +++ b/dockge_cli/client/commands/functions.py @@ -39,6 +39,7 @@ class FunctionBindings(): """ login command binding """ + print(f"WARNING! These credentials will be saved unencrypted in {storage._file.absolute()}") if len(extra_args) > 0: credentials = credential_parser_factory().parse_args(extra_args, namespace=Credentials) storage.put("username", credentials.username, encoded=True) @@ -76,8 +77,6 @@ class FunctionBindings(): """ status command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() status_formatter(con.list_stack(extra_args[0])) con.disconnect() @@ -87,8 +86,6 @@ class FunctionBindings(): """ restart command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() generic_formatter(con.restart(extra_args[0])) con.disconnect() @@ -98,8 +95,6 @@ class FunctionBindings(): """ update command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() generic_formatter(con.update(extra_args[0])) con.disconnect() @@ -109,8 +104,6 @@ class FunctionBindings(): """ stop command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() generic_formatter(con.stop(extra_args[0])) con.disconnect() @@ -120,8 +113,6 @@ class FunctionBindings(): """ start command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() generic_formatter(con.start(extra_args[0])) con.disconnect() @@ -131,8 +122,6 @@ class FunctionBindings(): """ down command binding """ - if extra_args is None: - raise ValueError con = FunctionBindings.__setup() generic_formatter(con.down(extra_args[0])) con.disconnect() diff --git a/dockge_cli/service/.gitignore b/dockge_cli/service/.gitignore new file mode 100644 index 0000000..9e62042 --- /dev/null +++ b/dockge_cli/service/.gitignore @@ -0,0 +1 @@ +.storage/* \ No newline at end of file diff --git a/dockge_cli/service/storage.py b/dockge_cli/service/storage.py index f9230fa..3edf415 100644 --- a/dockge_cli/service/storage.py +++ b/dockge_cli/service/storage.py @@ -3,18 +3,18 @@ import pathlib import base64 import yaml -_storagepath = pathlib.Path(__file__).parents[1] / ".temp" +_storagepath = pathlib.Path(__file__).parent / ".storage" +_storagepath.mkdir(exist_ok=True, parents=True) _file = _storagepath / "storage.yaml" -_storagepath.mkdir(exist_ok=True, parents=True) - -def fileexists(): +def create_file_when_missing(): """ Checks if storage file does exist, creates it when necessary """ - if not _file.exists(): - with open(_file, 'a', encoding="utf-8"): - os.utime(_file, None) + if _file.exists(): + return + with open(_file, 'a', encoding="utf-8"): + os.utime(_file, None) def exists(key: str) -> bool: """ @@ -34,10 +34,13 @@ def put(key: str, value: str, encoded=False): Puts a given value with a given key into the storage file Encodes the data as base64 when encoded is set to true """ - fileexists() - with open(_file, "r+", encoding="utf-8") as file: + if not _file.exists(): + create_file_when_missing() + + with open(_file, "r", encoding="utf-8") as file: content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {} content.update({ key: str(base64.b64encode(value.encode()), "utf-8") if encoded else value }) + with open(_file, "w+", encoding="utf-8") as file: yaml.dump(content, file, Dumper=yaml.SafeDumper) @@ -45,10 +48,13 @@ def remove(key: str): """ Removed a given key from the storage file """ - fileexists() + if not _file.exists(): + create_file_when_missing() + with open(_file, "r", encoding="utf-8") as file: content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {} content.pop(key, None) + with open(_file, "w+", encoding="utf-8") as file: yaml.dump(content, file, Dumper=yaml.SafeDumper) @@ -60,9 +66,11 @@ def get(key: str, encoded=False): value: str | None = None if not _file.exists(): return None + with open(_file, "r", encoding="utf-8") as file: content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) value = content.get(key, None) + if value is None: return None return base64.b64decode(value.encode()).decode() if encoded else value @@ -71,4 +79,6 @@ def clear(): """ Deletes the storage file """ + if not _file.exists(): + return _file.unlink() diff --git a/pyproject.toml b/pyproject.toml index 457eb3e..d1abd6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "dockge_cli" -version = "0.1.0-c.2" +version = "0.1.0-c.3" dependencies = [ "pyyaml~=6.0.1", "pydantic~=2.8.0",