From 84119e8ae9c4cb5fe645202c28a9530ca3dd1e09 Mon Sep 17 00:00:00 2001 From: Julio Faerman <356476+faermanj@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:45:34 +0000 Subject: [PATCH] Loading settings file --- upcli/up/main.py | 2 ++ uplib/poetry.lock | 6 +++--- uplib/uplib/config.py | 24 ++++++++++++++++++++---- uplib/uplib/main.py | 10 +++++++--- uplib/uplib/settings.toml | 0 uplib/uplib/up.yaml | 2 ++ 6 files changed, 34 insertions(+), 10 deletions(-) delete mode 100644 uplib/uplib/settings.toml create mode 100644 uplib/uplib/up.yaml diff --git a/upcli/up/main.py b/upcli/up/main.py index d88ba3c..474064a 100644 --- a/upcli/up/main.py +++ b/upcli/up/main.py @@ -31,5 +31,7 @@ def cli_main(): uplib.up_main(context, prompt) except Exception as e: log.error(e) + # print stack trace + raise e exit_cli("UP_ERROR") diff --git a/uplib/poetry.lock b/uplib/poetry.lock index 56d3584..e9723e0 100644 --- a/uplib/poetry.lock +++ b/uplib/poetry.lock @@ -421,13 +421,13 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] [[package]] name = "rich" -version = "13.6.0" +version = "13.7.0" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" optional = false python-versions = ">=3.7.0" files = [ - {file = "rich-13.6.0-py3-none-any.whl", hash = "sha256:2b38e2fe9ca72c9a00170a1a2d20c63c790d0e10ef1fe35eba76e1e7b1d7d245"}, - {file = "rich-13.6.0.tar.gz", hash = "sha256:5c14d22737e6d5084ef4771b62d5d4363165b403455a30a1c8ca39dc7b644bef"}, + {file = "rich-13.7.0-py3-none-any.whl", hash = "sha256:6da14c108c4866ee9520bbffa71f6fe3962e193b7da68720583850cd4548e235"}, + {file = "rich-13.7.0.tar.gz", hash = "sha256:5cb5123b5cf9ee70584244246816e9114227e0b98ad9176eede6ad54bf5403fa"}, ] [package.dependencies] diff --git a/uplib/uplib/config.py b/uplib/uplib/config.py index ab01584..aaf3447 100644 --- a/uplib/uplib/config.py +++ b/uplib/uplib/config.py @@ -1,13 +1,30 @@ -from dynaconf import Dynaconf +import pkgutil import logging +from dynaconf import Dynaconf +from dynaconf.loaders import yaml_loader +from enum import Enum + +class Config(Enum): + default_image = "default_image" + welcome_message = "welcome_message" + log_level = "log_level" + + def get(self): + key = self.value + return settings().get(key) + _settings = None def settings(): global _settings if not _settings: _settings = Dynaconf( - envvar_prefix="UP", - load_dotenv=True,) + environments=False, + envvar_prefix="UP") + up_yaml = pkgutil.get_data(__name__, "up.yaml") + if up_yaml: + up_yaml = up_yaml.decode("utf-8") + yaml_loader.load(_settings, filename=up_yaml) return _settings def get_log_level(): @@ -15,4 +32,3 @@ def get_log_level(): level = logging.getLevelName(level_name) return level - diff --git a/uplib/uplib/main.py b/uplib/uplib/main.py index 796e36d..750c6b0 100644 --- a/uplib/uplib/main.py +++ b/uplib/uplib/main.py @@ -4,8 +4,11 @@ from .containers import Containers, ContainerRun from .plugins import load_plugins from .logging import log +from .config import Config -def up_main(context: Context, prompt: Prompt): + +def up_main(context: Context, prompt: Prompt): + log.info("*** %s", Config.welcome_message.get() + " ***") containers = Containers() if not prompt: log.error("No prompts found") @@ -21,11 +24,13 @@ def up_main(context: Context, prompt: Prompt): for container in container_runs: containers.run(container) + def default_container(prompt): return ContainerRun( - image="fedora", + image=settings().get("default_image", "fedora"), command=prompt) + def containers_for_prompt(prompt) -> list[ContainerRun]: from_plugins = containers_from_plugins(prompt) from_configs = containers_from_dynaconf(prompt) @@ -43,4 +48,3 @@ def containers_from_plugins(prompt: list[str]) -> list[ContainerRun]: if not result: result = [] return result - diff --git a/uplib/uplib/settings.toml b/uplib/uplib/settings.toml deleted file mode 100644 index e69de29..0000000 diff --git a/uplib/uplib/up.yaml b/uplib/uplib/up.yaml new file mode 100644 index 0000000..a4e1249 --- /dev/null +++ b/uplib/uplib/up.yaml @@ -0,0 +1,2 @@ +default_image: fedora +welcome_message: Thank you for running up! \ No newline at end of file