gluon-core: merge site.json and domain.json in site lua module
This commit is contained in:
		
							parent
							
								
									b3c9ac8bce
								
							
						
					
					
						commit
						5999fdb603
					
				@ -2,6 +2,9 @@ need_string(in_site('site_code'))
 | 
			
		||||
need_string(in_site('site_name'))
 | 
			
		||||
need_string_match(in_site('site_seed'), '^' .. ('%x'):rep(64) .. '$')
 | 
			
		||||
 | 
			
		||||
-- TODO: it should be checked for existance!
 | 
			
		||||
need_string(in_site('default_domain_code'))
 | 
			
		||||
 | 
			
		||||
if need_table('opkg', nil, false) then
 | 
			
		||||
	need_string('opkg.lede', false)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,10 @@
 | 
			
		||||
local site = (function()
 | 
			
		||||
	local config = '/lib/gluon/site.json'
 | 
			
		||||
local function read_json(path)
 | 
			
		||||
 | 
			
		||||
	local json = require 'luci.jsonc'
 | 
			
		||||
	local decoder = json.new()
 | 
			
		||||
	local sink = decoder:sink()
 | 
			
		||||
 | 
			
		||||
	local file = assert(io.open(config))
 | 
			
		||||
	local file = assert(io.open(path))
 | 
			
		||||
 | 
			
		||||
	while true do
 | 
			
		||||
		local chunk = file:read(2048)
 | 
			
		||||
@ -16,11 +15,36 @@ local site = (function()
 | 
			
		||||
	file:close()
 | 
			
		||||
 | 
			
		||||
	return assert(decoder:get())
 | 
			
		||||
end)()
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local site = read_json('/lib/gluon/site.json')
 | 
			
		||||
local domain = (function(site)
 | 
			
		||||
	local uci = require('simple-uci').cursor()
 | 
			
		||||
	local fs = require "nixio.fs"
 | 
			
		||||
	local sname = uci:get_first('gluon', 'system')
 | 
			
		||||
 | 
			
		||||
	local dc = uci:get('gluon', sname, 'domain_code') or ''
 | 
			
		||||
 | 
			
		||||
	if fs.stat('/lib/gluon/domains/'..dc..'.json', 'type')~='reg' then
 | 
			
		||||
		dc = site['default_domain_code']
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	return read_json('/lib/gluon/domains/'..dc..'.json')
 | 
			
		||||
end)(site)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
local wrap
 | 
			
		||||
 | 
			
		||||
local function merge(t1, t2)
 | 
			
		||||
	for k, v in pairs(t2) do
 | 
			
		||||
		if (type(v) == "table") and (type(t1[k] or false) == "table") then
 | 
			
		||||
			merge(t1[k], t2[k])
 | 
			
		||||
		else
 | 
			
		||||
			t1[k] = v
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	return t1
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function index(t, k)
 | 
			
		||||
	local v = getmetatable(t).value
 | 
			
		||||
@ -58,4 +82,4 @@ end
 | 
			
		||||
 | 
			
		||||
module 'gluon.site'
 | 
			
		||||
 | 
			
		||||
return wrap(site, _M)
 | 
			
		||||
return wrap(merge(site, domain), _M)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user