31 lines
861 B
Python
31 lines
861 B
Python
#!/usr/bin/env python3
|
|
"""Merge KEY=value lines from stdin into an env file. Prints key names only."""
|
|
import pathlib
|
|
import re
|
|
import sys
|
|
|
|
path = pathlib.Path(sys.argv[1])
|
|
text = path.read_text() if path.exists() else ""
|
|
|
|
def upsert(text, key, value):
|
|
line = f"{key}={value}"
|
|
pattern = re.compile("^" + re.escape(key) + "=.*$", re.M)
|
|
if pattern.search(text):
|
|
return pattern.sub(line, text, count=1)
|
|
return text.rstrip() + "\n" + line + "\n"
|
|
|
|
keys = []
|
|
for raw in sys.stdin:
|
|
raw = raw.rstrip("\n")
|
|
if not raw or raw.startswith("#") or "=" not in raw:
|
|
continue
|
|
key, value = raw.split("=", 1)
|
|
text = upsert(text, key, value)
|
|
keys.append(key)
|
|
|
|
if text and not text.endswith("\n"):
|
|
text += "\n"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(text)
|
|
print("engine config keys:", ",".join(keys))
|