Comparison

core/configmanager.lua @ 9875:99291e124449

configmanager: Emit warning for duplicated config options
author Matthew Wild <mwild1@gmail.com>
date Wed, 20 Mar 2019 12:20:51 +0000
parent 9874:c9f5ccdcdf80
child 9876:d812031c8716
comparison
equal deleted inserted replaced
9874:c9f5ccdcdf80 9875:99291e124449
14 local envload = require"util.envload".envload; 14 local envload = require"util.envload".envload;
15 local deps = require"util.dependencies"; 15 local deps = require"util.dependencies";
16 local resolve_relative_path = require"util.paths".resolve_relative_path; 16 local resolve_relative_path = require"util.paths".resolve_relative_path;
17 local glob_to_pattern = require"util.paths".glob_to_pattern; 17 local glob_to_pattern = require"util.paths".glob_to_pattern;
18 local path_sep = package.config:sub(1,1); 18 local path_sep = package.config:sub(1,1);
19 local get_traceback_table = require "util.debug".get_traceback_table;
19 20
20 local encodings = deps.softreq"util.encodings"; 21 local encodings = deps.softreq"util.encodings";
21 local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end 22 local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
22 23
23 local _M = {}; 24 local _M = {};
98 end 99 end
99 100
100 -- Built-in Lua parser 101 -- Built-in Lua parser
101 do 102 do
102 local pcall = _G.pcall; 103 local pcall = _G.pcall;
104 local function get_line_number(config_file)
105 local tb = get_traceback_table(nil, 2);
106 for i = 1, #tb do
107 if tb[i].info.short_src == config_file then
108 return tb[i].info.currentline;
109 end
110 end
111 end
103 parser = {}; 112 parser = {};
104 function parser.load(data, config_file, config_table) 113 function parser.load(data, config_file, config_table)
114 local set_options = {}; -- set_options[host.."/"..option_name] = true (when the option has been set already in this file)
105 local warnings = {}; 115 local warnings = {};
106 local env; 116 local env;
107 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below 117 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
108 env = setmetatable({ 118 env = setmetatable({
109 Host = true, host = true, VirtualHost = true, 119 Host = true, host = true, VirtualHost = true,
114 return os.getenv(k:sub(5)); 124 return os.getenv(k:sub(5));
115 end 125 end
116 return rawget(_G, k); 126 return rawget(_G, k);
117 end, 127 end,
118 __newindex = function (_, k, v) 128 __newindex = function (_, k, v)
129 local host = env.__currenthost or "*";
130 local option_path = host.."/"..k;
131 if set_options[option_path] then
132 t_insert(warnings, ("%s:%d: Duplicate option '%s'"):format(config_file, get_line_number(config_file), k));
133 end
134 set_options[option_path] = true;
119 set(config_table, env.__currenthost or "*", k, v); 135 set(config_table, env.__currenthost or "*", k, v);
120 end 136 end
121 }); 137 });
122 138
123 rawset(env, "__currenthost", "*") -- Default is global 139 rawset(env, "__currenthost", "*") -- Default is global