Commit 4d04b14c authored by Carla Goncalves Ourofino's avatar Carla Goncalves Ourofino
Browse files

Alteração para permitir arquivo .cfg com múlltiplas configurações de sga.

[SOMA-3827][SOMA-4041]
Showing with 34 additions and 4 deletions
+34 -4
......@@ -4,27 +4,57 @@ local configuration = {}
local safer = require("safer")
local schema = require("schema")
function configuration.read(filename)
function configuration.read(filename, sga_name)
local config_fd = io.open(filename, "r")
if not config_fd then
return nil, "Could not read config file "..filename
end
local config_data = config_fd:read("*a")
config_fd:close()
local config, err = {}
config.sga = {}
config, err = configuration.get(config_data, filename, config)
if not config then
return nil, err
end
local is_multiple = config.sga and next(config.sga)
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
config, err = configuration.get(config.sga[sga_name], filename, config)
if not config then
return nil, "["..sga_name.."] "..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
local config = {}
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
config = safer.readonly(config)
return config
end
......@@ -51,7 +81,7 @@ function configuration.check(config)
}
local err = schema.CheckSchema(config, config_schema)
if err then
io.stderr:write("Configuration error: "..tostring(err).."\n")
io.stderr:write("["..config.sga_name.."] Configuration error: "..tostring(err).."\n")
os.exit(1)
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment