mirror of
https://github.com/anotherhadi/jwt-tui.git
synced 2026-06-26 09:12:33 +02:00
f3dce1f4ab
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
37 lines
1022 B
Python
37 lines
1022 B
Python
#!/usr/bin/env python3
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PATTERN = re.compile(r"<!-- exec: (.+?) -->.*?<!-- endexec -->", re.DOTALL)
|
|
|
|
|
|
def replace(match):
|
|
cmd = match.group(1).strip()
|
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
|
output = result.stdout
|
|
if result.returncode != 0:
|
|
print(
|
|
f"[inject-exec] command failed ({result.returncode}): {cmd}",
|
|
file=sys.stderr,
|
|
)
|
|
print(result.stderr, file=sys.stderr)
|
|
sys.exit(1)
|
|
output = re.sub(r"<!-- exec: .+? -->\n?|<!-- endexec -->\n?", "", output)
|
|
if output and not output.endswith("\n"):
|
|
output += "\n"
|
|
return f"<!-- exec: {cmd} -->\n{output}<!-- endexec -->"
|
|
|
|
|
|
def process(path):
|
|
content = Path(path).read_text()
|
|
new_content = PATTERN.sub(replace, content)
|
|
if new_content != content:
|
|
Path(path).write_text(new_content)
|
|
print(f"[inject-exec] updated {path}")
|
|
|
|
|
|
for p in sys.argv[1:]:
|
|
process(p)
|