diff --git a/sga/configuration.lua b/sga/configuration.lua index 144865dcc63b878566892a640b0775bc1c3cf0eb..fded807f77bd46976b808069831b17d8d21d260d 100644 --- a/sga/configuration.lua +++ b/sga/configuration.lua @@ -4,6 +4,51 @@ local configuration = {} local safer = require("safer") local schema = require("schema") +local function get(config_data, filename, config) + local config = config or {} + local chunk, err = load(config_data, filename, "t", config) + if not chunk then + return nil, "Failed loading config file "..filename..": "..err + end + config.os = { getenv = os.getenv } + config.tonumber = tonumber + + local ok, err = pcall(chunk) + config.os = nil + if not ok then + return nil, "Failed processing config file "..filename..": "..err + end + return config +end + +local function get_from_multiple(filename, sga_name, config) + local err = "Failed getting config from multiple config file %s: %s" + if not sga_name then + return nil, string.format(err, filename, "missing sga_name argument") + end + + if config.sga_name then + -- TODO: Dar erro se config default tiver sga_name + config.sga_name = nil + end + + local config_data = config.sga[sga_name] + if not config_data then + return nil, string.format(err, filename, "'"..sga_name.."' not found") + end + config.sga = nil -- avoid multiple inside multiple + + config, err = get(config_data, filename, config) + if not config then + return nil, string.format("[%s] %s", sga_name, err) + end + + -- TODO: Dar erro se config do sga escolhido tiver sga_name + config.sga_name = sga_name + + return config +end + function configuration.read(filename, sga_name) local config_fd = io.open(filename, "r") if not config_fd then @@ -14,56 +59,28 @@ function configuration.read(filename, sga_name) local config, err = {} config.sga = {} - config, err = configuration.get(config_data, filename, config) + config, err = get(config_data, filename, config) if not config then return nil, err end - local is_multiple = config.sga and next(config.sga) + local is_multiple = (config.sga and next(config.sga)) + if sga_name and not is_multiple then + return nil, string.format( + "Failed getting config '%s': file %s is not a valid multiple config file", + sga_name, filename) + end if is_multiple then - if not sga_name then - return nil, "Failed getting config from file "..filename..": missing sga_name argument" - end - if config.sga_name then - -- TODO: O que fazer nesse caso? Avisar que foi ignorado? - config.sga_name = nil - end - - local config_data = config.sga[sga_name] - if not config_data then - return nil, "SGA configuration "..sga_name.." not found on config file "..filename - end - config.sga = nil - config, err = configuration.get(config_data, filename, config) + config, err = get_from_multiple(filename, sga_name, config) if not config then - return nil, "["..sga_name.."] "..err + return nil, err end - -- TODO: O que fazer se sga_name tiver diferente na configuração? Erro? - config.sga_name = config.sga_name or sga_name end config = safer.readonly(config) return config end -function configuration.get(config_data, filename, config) - local config = config or {} - local chunk, err = load(config_data, filename, "t", config) - if not chunk then - return nil, "Failed loading config file "..filename..": "..err - end - config.os = { getenv = os.getenv } - config.tonumber = tonumber - - local ok, err = pcall(chunk) - config.os = nil - if not ok then - return nil, "Failed processing config file "..filename..": "..err - end - - return config -end - function configuration.check(config) local config_schema = schema.Record { csbase_server = schema.Pattern("https?://.*"), @@ -87,7 +104,11 @@ function configuration.check(config) } local err = schema.CheckSchema(config, config_schema) if err then - io.stderr:write("["..config.sga_name.."] Configuration error: "..tostring(err).."\n") + local prefix = "" + if config.sga_name then + prefix = "["..config.sga_name.."] " + end + io.stderr:write(prefix.."Configuration error: "..tostring(err).."\n") os.exit(1) end end