Skip to content

Commit

Permalink
Add doc for enbsubst.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Jun 13, 2024
1 parent a1dac9b commit 08f71bc
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/test-infrastructure/scripts/envsubst.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#!/usr/bin/python3
"""
NAME
envsubst.py - substitutes environment variables in bash format strings
DESCRIPTION
envsubst.py is upgrade of POSIX command `envsubst`
supported syntax:
normal - ${VARIABLE1}
with default - ${VARIABLE1:-somevalue}
"""

import os
import re
import sys


def envsubst(template_str, env=os.environ):
"""Substitute environment variables in the template string, supporting default values."""
pattern = re.compile(r'\$\{([^}:\s]+)(?::-(.*?))?\}')
Expand All @@ -14,6 +27,7 @@ def replace(match):

return pattern.sub(replace, template_str)


def main():
if len(sys.argv) > 2:
print("Usage: python envsubst.py [template_file]")
Expand All @@ -30,5 +44,6 @@ def main():

print(result)


if __name__ == "__main__":
main()

0 comments on commit 08f71bc

Please sign in to comment.