check_site: introduce custom.json checking

This commit is contained in:
lemoer 2021-01-06 09:08:13 +01:00
parent d571f6054b
commit dd94e84f77
2 changed files with 98 additions and 18 deletions

View File

@ -23,9 +23,12 @@ files["package/**/check_site.lua"] = {
read_globals = { read_globals = {
"alternatives", "alternatives",
"extend", "extend",
"in_site_or_domain", "in_custom",
"in_domain", "in_domain",
"in_domain_or_custom",
"in_site", "in_site",
"in_site_or_domain",
"in_site_or_custom",
"need", "need",
"need_alphanumeric_key", "need_alphanumeric_key",
"need_array", "need_array",

View File

@ -5,6 +5,7 @@ local function config_error(src, ...)
end end
local has_domains = (os.execute('ls -d "$IPKG_INSTROOT"/lib/gluon/domains/ >/dev/null 2>&1') == 0) local has_domains = (os.execute('ls -d "$IPKG_INSTROOT"/lib/gluon/domains/ >/dev/null 2>&1') == 0)
local has_custom = (os.execute('test -e "$IPKG_INSTROOT"/lib/gluon/custom.json') == 0)
local function get_domains() local function get_domains()
@ -23,7 +24,7 @@ local function get_domains()
return domains return domains
end end
local site, domain_code, domain, conf local site, domain_code, domain, custom, conf
local M = setmetatable({}, { __index = _G }) local M = setmetatable({}, { __index = _G })
@ -95,22 +96,49 @@ local function domain_src()
return 'domains/' .. domain_code .. '.conf' return 'domains/' .. domain_code .. '.conf'
end end
local function conf_src(path) local function custom_src()
local src return 'custom.json'
end
if has_domains then local function config_contains(config, path)
if loadpath(nil, domain, unpack(path)) ~= nil then -- A more precise name would be config_exists_and_contains(...), but we want
src = domain_src() -- to keep the name short here.
elseif loadpath(nil, site, unpack(path)) ~= nil then if config == 'custom' then
src = site_src() return has_custom and loadpath(nil, custom, unpack(path)) ~= nil
elseif config == 'domain' then
return has_domains and loadpath(nil, domain, unpack(path)) ~= nil
elseif config == 'site' then
return loadpath(nil, site, unpack(path)) ~= nil
else else
src = site_src() .. ' / ' .. domain_src() assert(false)
end end
end
local function conf_src(path, skip)
local sources = {}
if skip ~= 'custom' and config_contains('custom', path) then
table.insert(sources, custom_src())
elseif skip ~= 'domain' and config_contains('domain', path) then
table.insert(sources, domain_src())
elseif skip ~= 'site' and config_contains('site', path) then
table.insert(sources, site_src())
else else
src = site_src() -- path not found, so show all possible sources
if skip ~= 'site' then
table.insert(sources, site_src())
end end
return src if skip ~= 'domain' and has_domains then
table.insert(sources, domain_src())
end
if skip ~= 'custom' and has_custom then
table.insert(sources, custom_src())
end
end
return table.concat(sources, ' / ')
end end
local function var_error(path, val, msg) local function var_error(path, val, msg)
@ -123,22 +151,58 @@ local function var_error(path, val, msg)
end end
function M.in_site(path) function M.in_site(path)
if has_domains and loadpath(nil, domain, unpack(path)) ~= nil then if config_contains('domain', path) or config_contains('custom', path) then
config_error(domain_src(), '%s is allowed in site configuration only', path_to_string(path)) config_error(conf_src(path, 'site'), '%s is allowed in site configuration only', path_to_string(path))
end end
return path return path
end end
function M.in_domain(path) function M.in_domain(path)
if has_domains and loadpath(nil, site, unpack(path)) ~= nil then -- We allow all domain only variables in site, when there is no domain config,
config_error(site_src(), '%s is allowed in domain configuration only', path_to_string(path)) -- meaning that we only have a single domain inside the site.conf.
local allowed_in_site = not has_domains
if (not allowed_in_site and config_contains('site', path)) or config_contains('custom', path) then
config_error(conf_src(path, 'domain'), '%s is allowed in domain configuration only', path_to_string(path))
end
return path
end
function M.in_custom(path)
if config_contains('site', path) or config_contains('domain', path) then
config_error(conf_src(path, 'custom'), '%s is allowed in custom configuration only', path_to_string(path))
end end
return path return path
end end
function M.in_site_or_domain(path) function M.in_site_or_domain(path)
if config_contains('custom', path) then
config_error(custom_src(), '%s is allowed in site or domain configuration only', path_to_string(path))
end
return path
end
function M.in_site_or_custom(path)
if config_contains('domain', path) then
config_error(domain_src(), '%s is allowed in site or custom configuration only', path_to_string(path))
end
return path
end
function M.in_domain_or_custom(path)
-- We allow all domain only variables in site, when there is no domain config,
-- meaning that we only have a single domain inside the site.conf.
local allowed_in_site = not has_domains
if not allowed_in_site and config_contains('site', path) then
config_error(site_src(), '%s is allowed in domain or custom configuration only', path_to_string(path))
end
return path return path
end end
@ -389,16 +453,29 @@ local check = setfenv(assert(loadfile()), M)
site = assert(json.load((os.getenv('IPKG_INSTROOT') or '') .. '/lib/gluon/site.json')) site = assert(json.load((os.getenv('IPKG_INSTROOT') or '') .. '/lib/gluon/site.json'))
if has_custom then
custom = load_json(os.getenv('IPKG_INSTROOT') .. '/lib/gluon/custom.json')
print('Found ' .. os.getenv('IPKG_INSTROOT') .. '/lib/gluon/custom.json!')
else
print('Info: ' .. os.getenv('IPKG_INSTROOT') .. '/lib/gluon/custom.json is not existing.')
end
local ok, err = pcall(function() local ok, err = pcall(function()
if has_domains then if has_domains then
for k, v in pairs(get_domains()) do for k, v in pairs(get_domains()) do
domain_code = k domain_code = k
domain = v domain = v
conf = merge(site, domain) conf = merge(site, domain)
if has_custom then
conf = merge(conf, custom)
end
check() check()
end end
else else
conf = site conf = site
if has_custom then
conf = merge(conf, custom)
end
check() check()
end end
end) end)