From 4403faa63dfaae84cadd59ec783f9fdf158a313d Mon Sep 17 00:00:00 2001 From: GBDixonAlex Date: Fri, 12 Jul 2024 15:44:19 +0100 Subject: [PATCH] - add env var support evaluated in vars --- jsn.py | 18 ++++++++++++++---- readme.md | 5 ++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/jsn.py b/jsn.py index bc3d7a3..4b3a66f 100644 --- a/jsn.py +++ b/jsn.py @@ -610,8 +610,16 @@ def vars_in_string(string): else: break return variables - - + + +# replaces a ${var} with system environment var if it exists +def get_env_var(var_name): + if var_name in os.environ.keys(): + return os.environ[var_name] + print_error("[jsn error] undefined variable '" + var_name + "'") + sys.exit(1) + + # resolves "${var}" into a typed value or a token pasted string, handle multiple vars within strings or arrays def resolve_vars(value, vars): value_string = str(value) @@ -637,8 +645,10 @@ def resolve_vars(value, vars): else: return vars[var_name] else: - print_error("[jsn error] undefined variable '" + var_name + "'") - sys.exit(1) + ev = get_env_var(var_name) + value = value.replace(v, ev) + if len(vv) == count+1: + return value count += 1 return None diff --git a/readme.md b/readme.md index c352966..c1a6cdd 100644 --- a/readme.md +++ b/readme.md @@ -150,7 +150,10 @@ import test.jsn // you can use special variables: // inject the current script directory into a string // - this is the directory name of the file this variable is used in - script_directory: "${script_dir}" + script_directory: "${script_dir}" + + // env vars can also be injected, if no variable is found in the script jsn will fallback to check if an env var exists + env_vars: ${OS_ENV_VAR} // subobjects can be merged and inherited recursively see ** inheritence(jsn) base: "foo"