Files
compose-files/deploy/envparse.py
T
2026-06-22 22:57:51 -07:00

28 lines
857 B
Python

import sys, re
def parse_env(path):
pairs = []
with open(path) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, _, v = line.partition('=')
k = k.strip(); v = v.strip()
if (v.startswith("'") and v.endswith("'")) or \
(v.startswith('"') and v.endswith('"')):
v = v[1:-1]
pairs.append((k, v))
return pairs
mode = sys.argv[1]
if mode == 'export':
for k, v in parse_env(sys.argv[2]):
print('export {}={}'.format(k, repr(v)))
elif mode == 'vars':
print(' '.join('$' + k for k, v in parse_env(sys.argv[2])))
elif mode == 'strip':
t = sys.stdin.read()
t = re.sub(r'[ \t]*env_file:[ \t]*\n([ \t]+-[^\n]*\n)+', '', t)
sys.stdout.write(t)