Changeset

13743:0c7e11c11968 13.0

core.configmanager: Remove dependency on 'prosody' global for Credential Minimizing dependencies on global state is nice, as it makes using configmanager outside of Prosody easier.
author Kim Alvefur <zash@zash.se>
date Sat, 22 Feb 2025 00:00:41 +0100
parents 13741:e9edf9b50f32
children 13744:34ac05f6bd10
files core/configmanager.lua util/startup.lua
diffstat 2 files changed, 21 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/core/configmanager.lua	Mon Feb 17 23:06:06 2025 +0000
+++ b/core/configmanager.lua	Sat Feb 22 00:00:41 2025 +0100
@@ -36,6 +36,8 @@
 local config = setmetatable({ ["*"] = { } }, config_mt);
 local delayed_warnings = {};
 local files = {};
+local credentials_directory = nil;
+local credential_fallback_fatal = true;
 
 -- When host not found, use global
 local host_mt = { __index = function(_, k) return config["*"][k] end }
@@ -371,9 +373,9 @@
 		env.FileLine = filereader(config_path, "*l");
 		env.FileLines = linereader(config_path);
 
-		if _G.prosody.paths.credentials then
-			env.Credential = filereader(_G.prosody.paths.credentials, "*a");
-		elseif _G.prosody.process_type == "prosody" then
+		if credentials_directory then
+			env.Credential = filereader(credentials_directory, "*a");
+		elseif credential_fallback_fatal then
 			env.Credential = function() error("Credential() requires the $CREDENTIALS_DIRECTORY environment variable to be set", 2) end
 		else
 			env.Credential = function()
@@ -405,4 +407,12 @@
 
 end
 
+function _M.set_credentials_directory(directory)
+	credentials_directory = directory;
+end
+
+function _M.set_credential_fallback_mode(mode)
+	credential_fallback_fatal = mode == "error";
+end
+
 return _M;
--- a/util/startup.lua	Mon Feb 17 23:06:06 2025 +0000
+++ b/util/startup.lua	Sat Feb 22 00:00:41 2025 +0100
@@ -89,6 +89,14 @@
 		end
 	end
 	prosody.config_file = filename
+	local credentials_directory = os.getenv("CREDENTIALS_DIRECTORY");
+	if credentials_directory then
+		config.set_credentials_directory(credentials_directory);
+	elseif prosody.process_type == "prosody" then
+		config.set_credential_fallback_mode("error");
+	else
+		config.set_credential_fallback_mode("warn");
+	end
 	local ok, level, err = config.load(filename);
 	if not ok then
 		print("\n");
@@ -271,7 +279,6 @@
 		config = CFG_CONFIGDIR or ".";
 		plugins = CFG_PLUGINDIR or "plugins";
 		data = "data";
-		credentials = os.getenv("CREDENTIALS_DIRECTORY");
 	};
 
 	prosody.arg = _G.arg;