File size: 1,770 Bytes
dae60e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | from __future__ import annotations
import argparse
from pathlib import Path
import modal
def read_env(path: Path) -> dict[str, str]:
values: dict[str, str] = {}
if not path.exists():
return values
for line in path.read_text().splitlines():
stripped = line.strip()
if not stripped or stripped.startswith("#") or "=" not in stripped:
continue
key, value = stripped.split("=", 1)
values[key.strip()] = value.strip().strip('"').strip("'")
return values
def write_env(path: Path, values: dict[str, str]) -> None:
lines = []
for key in sorted(values):
value = values[key]
lines.append(f'{key}="{value}"')
path.write_text("\n".join(lines) + "\n")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--app", required=True)
parser.add_argument("--function", required=True)
parser.add_argument("--route", default="/extract")
parser.add_argument("--env-var", default="MODAL_RECEIPT_ENDPOINT")
parser.add_argument("--env-file", default=".env")
parser.add_argument("--also", nargs="*", default=[])
args = parser.parse_args()
remote_function = modal.Function.from_name(args.app, args.function)
base_url = remote_function.get_web_url().rstrip("/")
endpoint_url = base_url + args.route
env_path = Path(args.env_file)
values = read_env(env_path)
values[args.env_var] = endpoint_url
for extra_key in args.also:
values[extra_key] = endpoint_url
write_env(env_path, values)
print(f"Wrote {args.env_var}={endpoint_url} to {env_path}")
for extra_key in args.also:
print(f"Wrote {extra_key}={endpoint_url} to {env_path}")
if __name__ == "__main__":
main()
|