Comparison

core/configmanager.lua @ 13746:3d58ea9c5356

Merge 13.0->trunk
author Kim Alvefur <zash@zash.se>
date Sat, 22 Feb 2025 00:26:35 +0100
parent 13745:994ea8d54b72
child 13840:1b4f2d010141
comparison
equal deleted inserted replaced
13742:47e537e340c4 13746:3d58ea9c5356
32 32
33 local parser = nil; 33 local parser = nil;
34 34
35 local config_mt = { __index = function (t, _) return rawget(t, "*"); end}; 35 local config_mt = { __index = function (t, _) return rawget(t, "*"); end};
36 local config = setmetatable({ ["*"] = { } }, config_mt); 36 local config = setmetatable({ ["*"] = { } }, config_mt);
37 local delayed_warnings = {};
38 local files = {}; 37 local files = {};
38 local credentials_directory = nil;
39 local credential_fallback_fatal = true;
39 40
40 -- When host not found, use global 41 -- When host not found, use global
41 local host_mt = { __index = function(_, k) return config["*"][k] end } 42 local host_mt = { __index = function(_, k) return config["*"][k] end }
42 43
43 function _M.getconfig() 44 function _M.getconfig()
44 return config; 45 return config;
45 end 46 end
46 47
47 function _M.get(host, key) 48 function _M.get(host, key)
48 if host and key and delayed_warnings[host.."/"..key] then 49 local v = config[host][key];
49 local warning = delayed_warnings[host.."/"..key]; 50 if v and errors.is_error(v) then
50 log("warn", "%s", warning.text); 51 log("warn", "%s:%d: %s", v.context.filename, v.context.fileline, v.text);
51 end 52 return nil;
52 return config[host][key]; 53 end
54 return v;
53 end 55 end
54 function _M.rawget(host, key) 56 function _M.rawget(host, key)
55 local hostconfig = rawget(config, host); 57 local hostconfig = rawget(config, host);
56 if hostconfig then 58 if hostconfig then
57 return rawget(hostconfig, key); 59 return rawget(hostconfig, key);
248 local option_path = host.."/"..k; 250 local option_path = host.."/"..k;
249 if set_options[option_path] then 251 if set_options[option_path] then
250 t_insert(warnings, ("%s:%d: Duplicate option '%s'"):format(config_file, get_line_number(config_file), k)); 252 t_insert(warnings, ("%s:%d: Duplicate option '%s'"):format(config_file, get_line_number(config_file), k));
251 end 253 end
252 set_options[option_path] = true; 254 set_options[option_path] = true;
253 if errors.is_error(v) then
254 delayed_warnings[option_path] = v;
255 return;
256 end
257 set(config_table, env.__currenthost or "*", k, v); 255 set(config_table, env.__currenthost or "*", k, v);
258 end 256 end
259 }); 257 });
260 258
261 rawset(env, "__currenthost", "*") -- Default is global 259 rawset(env, "__currenthost", "*") -- Default is global
369 367
370 env.FileContents = filereader(config_path, "*a"); 368 env.FileContents = filereader(config_path, "*a");
371 env.FileLine = filereader(config_path, "*l"); 369 env.FileLine = filereader(config_path, "*l");
372 env.FileLines = linereader(config_path); 370 env.FileLines = linereader(config_path);
373 371
374 if _G.prosody.paths.credentials then 372 if credentials_directory then
375 env.Credential = filereader(_G.prosody.paths.credentials, "*a"); 373 env.Credential = filereader(credentials_directory, "*a");
376 elseif _G.prosody.process_type == "prosody" then 374 elseif credential_fallback_fatal then
377 env.Credential = function() error("Credential() requires the $CREDENTIALS_DIRECTORY environment variable to be set", 2) end 375 env.Credential = function() error("Credential() requires the $CREDENTIALS_DIRECTORY environment variable to be set", 2) end
378 else 376 else
379 env.Credential = function() 377 env.Credential = function()
380 return errors.new({ 378 return errors.new({
381 type = "continue", 379 type = "continue";
382 text = ("%s:%d: Credential() requires the $CREDENTIALS_DIRECTORY environment variable to be set") 380 text = "Credential() requires the $CREDENTIALS_DIRECTORY environment variable to be set";
383 :format(config_file, get_line_number(config_file)); 381 }, { filename = config_file; fileline = get_line_number(config_file) });
384 }); 382 end
385 end
386
387 end 383 end
388 384
389 local chunk, err = envload(data, "@"..config_file, env); 385 local chunk, err = envload(data, "@"..config_file, env);
390 386
391 if not chunk then 387 if not chunk then
403 return true, warnings; 399 return true, warnings;
404 end 400 end
405 401
406 end 402 end
407 403
404 function _M.set_credentials_directory(directory)
405 credentials_directory = directory;
406 end
407
408 function _M.set_credential_fallback_mode(mode)
409 credential_fallback_fatal = mode == "error";
410 end
411
408 return _M; 412 return _M;