Software /
code /
prosody
Comparison
core/configmanager.lua @ 13390:905a6009f60d
configmanager: Support for appending to existing config options
...and some other useful operations
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 08 Dec 2023 15:34:48 +0000 |
parent | 13389:47d0d80da208 |
child | 13391:5c783cf58ae7 |
comparison
equal
deleted
inserted
replaced
13389:47d0d80da208 | 13390:905a6009f60d |
---|---|
11 setmetatable, rawget, rawset, io, os, error, dofile, type, pairs, ipairs; | 11 setmetatable, rawget, rawset, io, os, error, dofile, type, pairs, ipairs; |
12 local format, math_max, t_insert = string.format, math.max, table.insert; | 12 local format, math_max, t_insert = string.format, math.max, table.insert; |
13 | 13 |
14 local envload = require"prosody.util.envload".envload; | 14 local envload = require"prosody.util.envload".envload; |
15 local deps = require"prosody.util.dependencies"; | 15 local deps = require"prosody.util.dependencies"; |
16 local it = require"prosody.util.iterators"; | |
16 local resolve_relative_path = require"prosody.util.paths".resolve_relative_path; | 17 local resolve_relative_path = require"prosody.util.paths".resolve_relative_path; |
17 local glob_to_pattern = require"prosody.util.paths".glob_to_pattern; | 18 local glob_to_pattern = require"prosody.util.paths".glob_to_pattern; |
18 local path_sep = package.config:sub(1,1); | 19 local path_sep = package.config:sub(1,1); |
19 local get_traceback_table = require "prosody.util.debug".get_traceback_table; | 20 local get_traceback_table = require "prosody.util.debug".get_traceback_table; |
20 | 21 |
113 if tb[i].info.short_src == config_file then | 114 if tb[i].info.short_src == config_file then |
114 return tb[i].info.currentline; | 115 return tb[i].info.currentline; |
115 end | 116 end |
116 end | 117 end |
117 end | 118 end |
119 | |
120 local config_option_proxy_mt = { | |
121 __index = setmetatable({ | |
122 append = function (self, value) | |
123 local original_option = self:value(); | |
124 if original_option == nil then | |
125 original_option = {}; | |
126 end | |
127 if type(value) ~= "table" then | |
128 error("'append' operation expects a list of values to append to the existing list", 2); | |
129 end | |
130 if value[1] ~= nil then | |
131 for _, v in ipairs(value) do | |
132 t_insert(original_option, v); | |
133 end | |
134 else | |
135 for k, v in pairs(value) do | |
136 original_option[k] = v; | |
137 end | |
138 end | |
139 set(self.config_table, self.host, self.option_name, original_option); | |
140 return self; | |
141 end; | |
142 value = function (self) | |
143 return rawget_option(self.config_table, self.host, self.option_name); | |
144 end; | |
145 values = function (self) | |
146 return it.values(self:value()); | |
147 end; | |
148 }, { | |
149 __index = function (t, k) | |
150 error("Unknown config option operation: '"..k.."'", 2); | |
151 end; | |
152 }); | |
153 | |
154 __call = function (self, v2) | |
155 local v = self:value() or {}; | |
156 if type(v) == "table" and type(v2) == "table" then | |
157 return self:append(v2); | |
158 end | |
159 | |
160 error("Invalid syntax - missing '=' perhaps?", 2); | |
161 end; | |
162 }; | |
163 | |
118 parser = {}; | 164 parser = {}; |
119 function parser.load(data, config_file, config_table) | 165 function parser.load(data, config_file, config_table) |
120 local set_options = {}; -- set_options[host.."/"..option_name] = true (when the option has been set already in this file) | 166 local set_options = {}; -- set_options[host.."/"..option_name] = true (when the option has been set already in this file) |
121 local warnings = {}; | 167 local warnings = {}; |
122 local env; | 168 local env; |
132 if k == "Lua" then | 178 if k == "Lua" then |
133 return _G; | 179 return _G; |
134 end | 180 end |
135 local val = rawget_option(config_table, env.__currenthost or "*", k); | 181 local val = rawget_option(config_table, env.__currenthost or "*", k); |
136 | 182 |
137 if val ~= nil then | 183 local g_val = rawget(_G, k); |
184 | |
185 if val ~= nil or g_val == nil then | |
186 if type(val) == "table" then | |
187 return setmetatable({ | |
188 config_table = config_table; | |
189 host = env.__currenthost or "*"; | |
190 option_name = k; | |
191 }, config_option_proxy_mt); | |
192 end | |
138 return val; | 193 return val; |
139 end | 194 end |
140 | |
141 local g_val = rawget(_G, k); | |
142 | 195 |
143 if g_val ~= nil then | 196 if g_val ~= nil then |
144 t_insert(warnings, ("%s:%d: direct usage of the Lua API is deprecated - replace `%s` with `Lua.%s`"):format(config_file, get_line_number(config_file), k, k)); | 197 t_insert(warnings, ("%s:%d: direct usage of the Lua API is deprecated - replace `%s` with `Lua.%s`"):format(config_file, get_line_number(config_file), k, k)); |
145 end | 198 end |
146 | 199 |