2018-01-19 11:32:32 +00:00
|
|
|
local cjson = require 'cjson'
|
|
|
|
|
|
|
|
local function load_json(filename)
|
|
|
|
local f = assert(io.open(filename))
|
|
|
|
local json = cjson.decode(f:read('*a'))
|
|
|
|
f:close()
|
|
|
|
return json
|
|
|
|
end
|
|
|
|
|
|
|
|
local site = load_json(os.getenv('IPKG_INSTROOT') .. '/lib/gluon/site.json')
|
|
|
|
|
|
|
|
|
2018-01-19 02:30:50 +00:00
|
|
|
function in_site(var)
|
2018-01-19 09:12:43 +00:00
|
|
|
return var
|
2018-01-19 02:30:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function in_domain(var)
|
2018-01-19 09:12:43 +00:00
|
|
|
return var
|
2018-01-19 02:30:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function path_to_string(path)
|
|
|
|
return table.concat(path, '/')
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2016-08-03 08:50:24 +00:00
|
|
|
local function array_to_string(array)
|
2018-01-19 09:12:43 +00:00
|
|
|
return '[' .. table.concat(array, ', ') .. ']'
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function var_error(path, val, msg)
|
2018-01-19 09:59:45 +00:00
|
|
|
if type(val) == 'string' then
|
|
|
|
val = string.format('%q', val)
|
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
print(string.format('*** site.conf error: expected %s to %s, but it is %s', path_to_string(path), msg, tostring(val)))
|
|
|
|
os.exit(1)
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function extend(path, c)
|
|
|
|
local p = {unpack(path)}
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
for _, e in ipairs(c) do
|
|
|
|
p[#p+1] = e
|
|
|
|
end
|
|
|
|
return p
|
2015-10-11 17:31:44 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function loadpath(path, base, c, ...)
|
|
|
|
if not c or base == nil then
|
|
|
|
return base
|
|
|
|
end
|
2015-10-11 17:31:44 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if type(base) ~= 'table' then
|
|
|
|
var_error(path, base, 'be a table')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return loadpath(extend(path, {c}), base[c], ...)
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function loadvar(path)
|
|
|
|
return loadpath({}, site, unpack(path))
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function check_type(t)
|
|
|
|
return function(val)
|
|
|
|
return type(val) == t
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function check_one_of(array)
|
|
|
|
return function(val)
|
|
|
|
for _, v in ipairs(array) do
|
|
|
|
if v == val then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need(path, check, required, msg)
|
|
|
|
local val = loadvar(path)
|
|
|
|
if required == false and val == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if not check(val) then
|
|
|
|
var_error(path, val, msg)
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
local function need_type(path, type, required, msg)
|
|
|
|
return need(path, check_type(type), required, msg)
|
|
|
|
end
|
2014-07-16 14:31:31 +00:00
|
|
|
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_alphanumeric_key(path)
|
|
|
|
local val = path[#path]
|
|
|
|
-- We don't use character classes like %w here to be independent of the locale
|
|
|
|
if not val:match('^[0-9a-zA-Z_]+$') then
|
|
|
|
var_error(path, val, 'have a key using only alphanumeric characters and underscores')
|
|
|
|
end
|
2014-07-16 14:31:31 +00:00
|
|
|
end
|
|
|
|
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string(path, required)
|
|
|
|
return need_type(path, 'string', required, 'be a string')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_match(path, pat, required)
|
|
|
|
local val = need_string(path, required)
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if not val:match(pat) then
|
|
|
|
var_error(path, val, "match pattern '" .. pat .. "'")
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_number(path, required)
|
|
|
|
return need_type(path, 'number', required, 'be a number')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_boolean(path, required)
|
|
|
|
return need_type(path, 'boolean', required, 'be a boolean')
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_array(path, subcheck, required)
|
|
|
|
local val = need_type(path, 'table', required, 'be an array')
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if subcheck then
|
|
|
|
for i = 1, #val do
|
|
|
|
subcheck(extend(path, {i}))
|
|
|
|
end
|
|
|
|
end
|
2014-07-05 00:48:09 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_table(path, subcheck, required)
|
|
|
|
local val = need_type(path, 'table', required, 'be a table')
|
|
|
|
if not val then
|
|
|
|
return nil
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
if subcheck then
|
|
|
|
for k, _ in pairs(val) do
|
|
|
|
subcheck(extend(path, {k}))
|
|
|
|
end
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
function need_value(path, value, required)
|
|
|
|
return need(path, function(v)
|
|
|
|
return v == value
|
|
|
|
end, required, 'be ' .. tostring(value))
|
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_one_of(path, array, required)
|
|
|
|
return need(path, check_one_of(array), required, 'be one of the given array ' .. array_to_string(array))
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_array(path, required)
|
|
|
|
return need_array(path, need_string, required)
|
2014-07-05 00:48:09 +00:00
|
|
|
end
|
2016-08-03 08:50:24 +00:00
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_string_array_match(path, pat, required)
|
|
|
|
return need_array(path, function(e) need_string_match(e, pat) end, required)
|
2017-02-21 08:45:33 +00:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:12:43 +00:00
|
|
|
function need_array_of(path, array, required)
|
|
|
|
return need_array(path, function(e) need_one_of(e, array) end, required)
|
2016-08-03 08:50:24 +00:00
|
|
|
end
|
2018-01-19 11:32:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
dofile()
|