Moved files, refactored calls, added credentials warning
All checks were successful
/ pylint (push) Successful in 11s
/ mypy (push) Successful in 12s
/ build-artifacts (push) Successful in 7s
/ lint-and-typing (push) Successful in 14s
/ publish-artifacts (push) Successful in 9s

This commit is contained in:
Firq 2024-07-06 00:28:37 +02:00
parent 58aea47921
commit c00aa232fc
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
5 changed files with 23 additions and 24 deletions

View file

@ -1 +0,0 @@
.temp/*

View file

@ -39,6 +39,7 @@ class FunctionBindings():
""" """
login command binding login command binding
""" """
print(f"WARNING! These credentials will be saved unencrypted in {storage._file.absolute()}")
if len(extra_args) > 0: if len(extra_args) > 0:
credentials = credential_parser_factory().parse_args(extra_args, namespace=Credentials) credentials = credential_parser_factory().parse_args(extra_args, namespace=Credentials)
storage.put("username", credentials.username, encoded=True) storage.put("username", credentials.username, encoded=True)
@ -76,8 +77,6 @@ class FunctionBindings():
""" """
status command binding status command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
status_formatter(con.list_stack(extra_args[0])) status_formatter(con.list_stack(extra_args[0]))
con.disconnect() con.disconnect()
@ -87,8 +86,6 @@ class FunctionBindings():
""" """
restart command binding restart command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
generic_formatter(con.restart(extra_args[0])) generic_formatter(con.restart(extra_args[0]))
con.disconnect() con.disconnect()
@ -98,8 +95,6 @@ class FunctionBindings():
""" """
update command binding update command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
generic_formatter(con.update(extra_args[0])) generic_formatter(con.update(extra_args[0]))
con.disconnect() con.disconnect()
@ -109,8 +104,6 @@ class FunctionBindings():
""" """
stop command binding stop command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
generic_formatter(con.stop(extra_args[0])) generic_formatter(con.stop(extra_args[0]))
con.disconnect() con.disconnect()
@ -120,8 +113,6 @@ class FunctionBindings():
""" """
start command binding start command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
generic_formatter(con.start(extra_args[0])) generic_formatter(con.start(extra_args[0]))
con.disconnect() con.disconnect()
@ -131,8 +122,6 @@ class FunctionBindings():
""" """
down command binding down command binding
""" """
if extra_args is None:
raise ValueError
con = FunctionBindings.__setup() con = FunctionBindings.__setup()
generic_formatter(con.down(extra_args[0])) generic_formatter(con.down(extra_args[0]))
con.disconnect() con.disconnect()

1
dockge_cli/service/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.storage/*

View file

@ -3,16 +3,16 @@ import pathlib
import base64 import base64
import yaml 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" _file = _storagepath / "storage.yaml"
_storagepath.mkdir(exist_ok=True, parents=True) def create_file_when_missing():
def fileexists():
""" """
Checks if storage file does exist, creates it when necessary Checks if storage file does exist, creates it when necessary
""" """
if not _file.exists(): if _file.exists():
return
with open(_file, 'a', encoding="utf-8"): with open(_file, 'a', encoding="utf-8"):
os.utime(_file, None) os.utime(_file, None)
@ -34,10 +34,13 @@ def put(key: str, value: str, encoded=False):
Puts a given value with a given key into the storage file Puts a given value with a given key into the storage file
Encodes the data as base64 when encoded is set to true Encodes the data as base64 when encoded is set to true
""" """
fileexists() if not _file.exists():
with open(_file, "r+", encoding="utf-8") as file: 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: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
content.update({ key: str(base64.b64encode(value.encode()), "utf-8") if encoded else value }) content.update({ key: str(base64.b64encode(value.encode()), "utf-8") if encoded else value })
with open(_file, "w+", encoding="utf-8") as file: with open(_file, "w+", encoding="utf-8") as file:
yaml.dump(content, file, Dumper=yaml.SafeDumper) yaml.dump(content, file, Dumper=yaml.SafeDumper)
@ -45,10 +48,13 @@ def remove(key: str):
""" """
Removed a given key from the storage file 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: with open(_file, "r", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {} content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
content.pop(key, None) content.pop(key, None)
with open(_file, "w+", encoding="utf-8") as file: with open(_file, "w+", encoding="utf-8") as file:
yaml.dump(content, file, Dumper=yaml.SafeDumper) yaml.dump(content, file, Dumper=yaml.SafeDumper)
@ -60,9 +66,11 @@ def get(key: str, encoded=False):
value: str | None = None value: str | None = None
if not _file.exists(): if not _file.exists():
return None return None
with open(_file, "r", encoding="utf-8") as file: with open(_file, "r", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader)
value = content.get(key, None) value = content.get(key, None)
if value is None: if value is None:
return None return None
return base64.b64decode(value.encode()).decode() if encoded else value return base64.b64decode(value.encode()).decode() if encoded else value
@ -71,4 +79,6 @@ def clear():
""" """
Deletes the storage file Deletes the storage file
""" """
if not _file.exists():
return
_file.unlink() _file.unlink()

View file

@ -1,6 +1,6 @@
[project] [project]
name = "dockge_cli" name = "dockge_cli"
version = "0.1.0-c.2" version = "0.1.0-c.3"
dependencies = [ dependencies = [
"pyyaml~=6.0.1", "pyyaml~=6.0.1",
"pydantic~=2.8.0", "pydantic~=2.8.0",