lualoader: config: Pull some messages out into constants

Rather than hardcoding these things. This could lead to some form of loader
localization later, but the main goal at the moment is to get a clear view
of the strings we're outputting and strive to use more string.format() and
less wild concatenation all over the place.
This commit is contained in:
kevans 2018-03-01 02:31:28 +00:00
parent 4704922ee8
commit a92c5ae88e

View File

@ -30,11 +30,24 @@
--
local config = {}
local modules = {}
local carousel_choices = {}
local MSG_FAILEXEC = "Failed to exec '%s'"
local MSG_FAILSETENV = "Failed to '%s' with value: %s"
local MSG_FAILOPENCFG = "Failed to open config: '%s'"
local MSG_FAILREADCFG = "Failed to read config: '%s'"
local MSG_FAILPARSECFG = "Failed to parse config: '%s'"
local MSG_FAILEXBEF = "Failed to execute '%s' before loading '%s'"
local MSG_FAILEXMOD = "Failed to execute '%s'"
local MSG_FAILEXAF = "Failed to execute '%s' after loading '%s'"
local MSG_MALFORMED = "Malformed line (%d):\n\t'%s'"
local MSG_DEFAULTKERNFAIL = "No kernel set, failed to load from module_path"
local MSG_KERNFAIL = "Failed to load kernel '%s'"
local MSG_KERNLOADING = "Loading kernel..."
local MSG_MODLOADING = "Loading configured modules..."
local MSG_MODLOADFAIL = "Could not load one or more modules!"
local pattern_table = {
{
str = "^%s*(#.*)",
@ -97,7 +110,7 @@ local pattern_table = {
str = "^%s*exec%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
process = function(k, _)
if loader.perform(k) ~= 0 then
print("Failed to exec '" .. k .. "'")
print(MSG_FAILEXEC:format(k))
end
end,
},
@ -106,8 +119,7 @@ local pattern_table = {
str = "^%s*([%w%p]+)%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
process = function(k, v)
if config.setenv(k, v) ~= 0 then
print("Failed to set '" .. k ..
"' with value: " .. v .. "")
print(MSG_FAILSETENV:format(k, v))
end
end,
},
@ -116,8 +128,7 @@ local pattern_table = {
str = "^%s*([%w%p]+)%s*=%s*(%d+)%s*(.*)",
process = function(k, v)
if config.setenv(k, v) ~= 0 then
print("Failed to set '" .. k ..
"' with value: " .. v .. "")
print(MSG_FAILSETENV:format(k, tostring(v)))
end
end,
},
@ -127,7 +138,7 @@ local function readFile(name, silent)
local f = io.open(name)
if f == nil then
if not silent then
print("Failed to open config: '" .. name .. "'")
print(MSG_FAILOPENCFG:format(name))
end
return nil
end
@ -138,7 +149,7 @@ local function readFile(name, silent)
if text == nil then
if not silent then
print("Failed to read config: '" .. name .. "'")
print(MSG_FAILREADCFG:format(name))
end
return nil
end
@ -162,8 +173,7 @@ local function checkNextboot()
end
if not config.parse(text) then
print("Failed to parse nextboot configuration: '" ..
nextboot_file .. "'")
print(MSG_FAILPARSECFG:format(nextbootfile))
end
-- Attempt to rewrite the first line and only the first line of the
@ -264,6 +274,7 @@ end
function config.loadmod(mod, silent)
local status = true
local pstatus = true
for k, v in pairs(mod) do
if v.load == "YES" then
local str = "load "
@ -278,23 +289,17 @@ function config.loadmod(mod, silent)
else
str = str .. k
end
if v.before ~= nil then
if loader.perform(v.before) ~= 0 then
if not silent then
print("Failed to execute '" ..
v.before ..
"' before loading '" .. k ..
"'")
end
status = false
pstatus = loader.perform(v.before) == 0
if not pstatus and not silent then
print(MSG_FAILEXBEF:format(v.before, k))
end
status = status and pstatus
end
if loader.perform(str) ~= 0 then
if not silent then
print("Failed to execute '" .. str ..
"'")
print(MSG_FAILEXMOD:format(str))
end
if v.error ~= nil then
loader.perform(v.error)
@ -303,15 +308,11 @@ function config.loadmod(mod, silent)
end
if v.after ~= nil then
if loader.perform(v.after) ~= 0 then
if not silent then
print("Failed to execute '" ..
v.after ..
"' after loading '" .. k ..
"'")
end
status = false
pstatus = loader.perform(v.after) == 0
if not pstatus and not silent then
print(MSG_FAILEXAF:format(v.after, k))
end
status = status and pstatus
end
-- else
@ -357,8 +358,8 @@ function config.parse(text)
if config.isValidComment(c) then
val.process(k, v)
else
print("Malformed line (" .. n ..
"):\n\t'" .. line .. "'")
print(MSG_MALFORMED:format(n,
line))
status = false
end
@ -367,8 +368,7 @@ function config.parse(text)
end
if not found then
print("Malformed line (" .. n .. "):\n\t'" ..
line .. "'")
print(MSG_MALFORMED:format(n, line))
status = false
end
end
@ -417,7 +417,7 @@ function config.loadKernel(other_kernel)
config.kernel_loaded = nil
return true
else
print("No kernel set, failed to load from module_path")
print(MSG_DEFAULTKERNFAIL)
return false
end
else
@ -455,7 +455,7 @@ function config.loadKernel(other_kernel)
config.kernel_loaded = kernel
return true
else
print("Failed to load kernel '" .. kernel .. "'")
print(MSG_KERNFAIL:format(kernel))
return false
end
end
@ -471,7 +471,7 @@ function config.load(file)
end
if not config.processFile(file) then
print("Failed to parse configuration: '" .. file .. "'")
print(MSG_FAILPARSECFG:format(file))
end
local f = loader.getenv("loader_conf_files")
@ -481,8 +481,7 @@ function config.load(file)
-- silent parse so that we complain on parse errors but
-- not for them simply not existing.
if not config.processFile(name, true) then
print("Failed to parse configuration: '" ..
name .. "'")
print(MSG_FAILPARSECFG:format(name))
end
end
end
@ -504,17 +503,16 @@ function config.loadelf()
local kernel = config.kernel_selected or config.kernel_loaded
local loaded
print("Loading kernel...")
print(MSG_KERNLOADING)
loaded = config.loadKernel(kernel)
if not loaded then
print("Failed to load any kernel")
return
end
print("Loading configured modules...")
print(MSG_MODLOADING)
if not config.loadmod(modules) then
print("Could not load one or more modules!")
print(MSG_MODLOADFAIL)
end
end