Skip to content

Commit

Permalink
- add env var support evaluated in vars
Browse files Browse the repository at this point in the history
  • Loading branch information
GBDixonAlex committed Jul 12, 2024
1 parent 689e547 commit 4403faa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 14 additions & 4 deletions jsn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4403faa

Please sign in to comment.