63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
# Tool/env_manager.py
|
|
#
|
|
# Lettura e scrittura delle variabili d'ambiente nel file .env del dashboard.
|
|
|
|
import re
|
|
|
|
from .config import REPORTS_DIR, _auth, _conn, ConnType
|
|
|
|
|
|
def _set_env_key(text: str, key: str, value: str) -> str:
|
|
"""Replace or append a KEY=value line in .env content."""
|
|
new, n = re.subn(rf'^#?\s*{key}=.*$', f'{key}={value}', text, flags=re.MULTILINE)
|
|
return new if n else text + f'\n{key}={value}\n'
|
|
|
|
|
|
def update_auth(enabled: bool, user: str | None = None, password: str | None = None) -> None:
|
|
"""Write LOGIN/DASHBOARD_USER/DASHBOARD_PASSWORD to .env and update _auth in memory."""
|
|
env_path = REPORTS_DIR / '.env'
|
|
text = env_path.read_text(encoding='utf-8')
|
|
|
|
text = _set_env_key(text, 'LOGIN', 'true' if enabled else 'false')
|
|
if user is not None:
|
|
text = _set_env_key(text, 'DASHBOARD_USER', user)
|
|
if password is not None:
|
|
text = _set_env_key(text, 'DASHBOARD_PASSWORD', password)
|
|
|
|
env_path.write_text(text, encoding='utf-8')
|
|
|
|
_auth['enabled'] = enabled
|
|
if user is not None:
|
|
_auth['user'] = user
|
|
if password is not None:
|
|
_auth['password'] = password
|
|
|
|
|
|
def update_db_conn(
|
|
conn_type: str,
|
|
pg_host: str = '',
|
|
pg_port: str = '5432',
|
|
pg_db: str = '',
|
|
pg_user: str = '',
|
|
pg_password: str = '',
|
|
) -> None:
|
|
"""Write RPA_CONN_TYPE and PG_* vars to .env and update _conn in memory."""
|
|
env_path = REPORTS_DIR / '.env'
|
|
text = env_path.read_text(encoding='utf-8')
|
|
|
|
text = _set_env_key(text, 'RPA_CONN_TYPE', conn_type)
|
|
text = _set_env_key(text, 'PG_HOST', pg_host or 'localhost')
|
|
text = _set_env_key(text, 'PG_PORT', pg_port or '5432')
|
|
text = _set_env_key(text, 'PG_DB', pg_db)
|
|
text = _set_env_key(text, 'PG_USER', pg_user)
|
|
text = _set_env_key(text, 'PG_PASSWORD', pg_password)
|
|
|
|
env_path.write_text(text, encoding='utf-8')
|
|
|
|
_conn['type'] = ConnType.POSTGRES if conn_type == 'postgres' else ConnType.SQLITE
|
|
_conn['pg_host'] = pg_host or 'localhost'
|
|
_conn['pg_port'] = pg_port or '5432'
|
|
_conn['pg_db'] = pg_db
|
|
_conn['pg_user'] = pg_user
|
|
_conn['pg_password'] = pg_password
|