You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

42 lines
920 B

local M = {}
local function get_system(uci)
local system
uci:foreach('system', 'system',
function(s)
system = s
return false
end
)
return assert(system, 'unable to find system section')
end
function M.get(uci)
local system = get_system(uci)
return system.pretty_hostname or system.hostname
end
function M.set(uci, pretty_hostname)
local system = get_system(uci)['.name']
local hostname = string.gsub(pretty_hostname, '[^a-zA-Z0-9%-]', '')
hostname = string.gsub(hostname, '%-+', '-')
hostname = string.gsub(hostname, '^%-', '')
hostname = string.sub(hostname, 1, 63)
hostname = string.gsub(hostname, '%-$', '')
if hostname == '' then
hostname = 'localhost'
end
if hostname == pretty_hostname then
uci:delete('system', system, 'pretty_hostname')
else
uci:set('system', system, 'pretty_hostname', pretty_hostname)
end
uci:set('system', system, 'hostname', hostname)
end
return M