56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
"""Helper utilities for env-pro."""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Optional, Dict
|
|
|
|
|
|
def find_env_file(project_root: Optional[Path] = None) -> Optional[Path]:
|
|
"""Find the .env file in the project."""
|
|
if project_root is None:
|
|
project_root = Path.cwd()
|
|
|
|
for env_file in [".env", ".env.local", ".env-profiles/default/.env"]:
|
|
path = project_root / env_file
|
|
if path.exists():
|
|
return path
|
|
|
|
return None
|
|
|
|
|
|
def parse_key_value_pair(pair: str) -> tuple:
|
|
"""Parse a KEY=VALUE string into a tuple."""
|
|
if "=" in pair:
|
|
key, value = pair.split("=", 1)
|
|
return key.strip(), value.strip()
|
|
return pair.strip(), ""
|
|
|
|
|
|
def is_encrypted(value: str) -> bool:
|
|
"""Check if a value appears to be encrypted."""
|
|
import base64
|
|
try:
|
|
data = base64.b64decode(value)
|
|
import json
|
|
json.loads(data)
|
|
return True
|
|
except (base64.binascii.Error, json.JSONDecodeError):
|
|
return False
|
|
|
|
|
|
def get_editor() -> str:
|
|
"""Get the editor to use for interactive editing."""
|
|
return os.environ.get("EDITOR", os.environ.get("VISUAL", "vim"))
|
|
|
|
|
|
def shell_expand(path: str) -> str:
|
|
"""Expand shell variables in a path."""
|
|
return os.path.expandvars(os.path.expanduser(path))
|
|
|
|
|
|
def is_valid_env_key(key: str) -> bool:
|
|
"""Check if a string is a valid environment variable name."""
|
|
import re
|
|
return bool(re.match(r"^[A-Za-z_][A-Za-z0-9_]*$", key))
|