32 lines
856 B
Python
Executable File
32 lines
856 B
Python
Executable File
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def read_value(path: Path, key: str, default: str) -> str:
|
|
if not path.exists():
|
|
return default
|
|
for raw in path.read_text(encoding="utf-8").splitlines():
|
|
line = raw.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
name, value = line.split("=", 1)
|
|
if name.strip() == key:
|
|
return value.strip().strip('"').strip("'")
|
|
return default
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) < 3:
|
|
print("usage: env-value.py ENV_FILE KEY [DEFAULT]", file=sys.stderr)
|
|
return 2
|
|
default = sys.argv[3] if len(sys.argv) > 3 else ""
|
|
print(read_value(Path(sys.argv[1]), sys.argv[2], default))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|