mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
feat(config): auto-create missing config.yml on startup
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/0c94e1c8-0f79-4f5f-a72b-e45832906d00 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
79dc039748
commit
fdd9047b37
@@ -178,9 +178,7 @@ class ConfigManager:
|
||||
# Also check if the config file is writable
|
||||
self._config_file_writable = os.access(self.config_file, os.W_OK)
|
||||
except FileNotFoundError:
|
||||
log.critical(
|
||||
"Config file not found! Any changes made to the configuration will not persist after the application restarts."
|
||||
)
|
||||
self._create_missing_config_file()
|
||||
except PermissionError:
|
||||
log.warning(
|
||||
"Config file not writable! Any changes made to the configuration will not persist after the application restarts."
|
||||
@@ -190,6 +188,24 @@ class ConfigManager:
|
||||
self._parse_config()
|
||||
self._validate_config()
|
||||
|
||||
def _create_missing_config_file(self) -> None:
|
||||
log.warning(f"Config file not found, creating an empty config at {hl(self.config_file, BLUE)}")
|
||||
|
||||
try:
|
||||
os.makedirs(os.path.dirname(self.config_file), exist_ok=True)
|
||||
with open(self.config_file, "x"):
|
||||
pass
|
||||
|
||||
self._config_file_mounted = True
|
||||
self._config_file_writable = os.access(self.config_file, os.W_OK)
|
||||
except FileExistsError:
|
||||
self._config_file_mounted = True
|
||||
self._config_file_writable = os.access(self.config_file, os.W_OK)
|
||||
except PermissionError:
|
||||
log.critical(
|
||||
"Config file not found and could not be created! Any changes made to the configuration will not persist after the application restarts."
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_db_engine() -> URL:
|
||||
"""Builds the database connection string using environment variables
|
||||
|
||||
@@ -113,3 +113,14 @@ def test_empty_config_loader():
|
||||
assert loader.config.EJS_CONTROLS == {}
|
||||
assert loader.config.GAMELIST_MEDIA_THUMBNAIL == "box2d"
|
||||
assert loader.config.GAMELIST_MEDIA_IMAGE == "screenshot"
|
||||
|
||||
|
||||
def test_missing_config_file_is_created(tmp_path):
|
||||
config_file = tmp_path / "config" / "config.yml"
|
||||
|
||||
loader = ConfigManager(str(config_file))
|
||||
|
||||
assert config_file.exists()
|
||||
assert config_file.read_text() == ""
|
||||
assert loader.config.CONFIG_FILE_MOUNTED
|
||||
assert loader.config.CONFIG_FILE_WRITABLE
|
||||
|
||||
Reference in New Issue
Block a user