Commit b1f9828f authored by Isabella Almeida da Silva's avatar Isabella Almeida da Silva
Browse files

[SOMA-3827][SOMA-4135] Mudar a leitura das configurações do formato atual para...

[SOMA-3827][SOMA-4135] Mudar a leitura das configurações do formato atual para uma tabela com uma lista de definições sem chave
- Adicionada função de cópia de tabelas no módulo util
parent acc39e35
......@@ -3,9 +3,11 @@ local configuration = {}
local safer = require("safer")
local schema = require("schema")
local util = require("sga.util")
local function get(config_data, filename, config)
local config = config or {}
local config = util.table_shallow_copy(config) or {}
config.sgas = nil -- avoid multiple inside multiple
local chunk, err = load(config_data, filename, "t", config)
if not chunk then
return nil, "Failed loading config file "..filename..": "..err
......@@ -26,23 +28,30 @@ local function get_from_multiple(filename, sga_name, config)
if not sga_name then
return nil, string.format(err, filename, "missing sga_name argument")
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)
local sgas_data = config.sgas
if not sgas_data then
return nil, string.format(err, filename, "not a multiple config file")
end
if config.sga_name then
return nil, string.format("Duplicate definition of sga_name: '%s' (from file) & '%s' (from argument). Multiple config file must not define sga_name value.", config.sga_name, sga_name)
-- Indexes all sga configs by sga_name
local sgas = {}
for k,v in ipairs(sgas_data) do
local sga_config, err_msg = {}
sga_config, err_msg = get(v, filename, config)
if not sga_config then
return nil, string.format(err, filename, err_msg)
end
sgas[sga_config.sga_name] = sga_config
end
config.sga_name = sga_name
local config_data = sgas[sga_name]
if not config_data then
return nil, string.format(err, filename, "'"..sga_name.."' not found")
end
return config
print(util.debug_table(sgas))
print("expected: "..sga_name.." got: "..config_data.sga_name)
return config_data
end
function configuration.read(filename, sga_name)
......@@ -54,13 +63,12 @@ function configuration.read(filename, sga_name)
config_fd:close()
local config, err = {}
config.sga = {}
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.sgas and #config.sgas > 0)
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",
......@@ -72,7 +80,7 @@ function configuration.read(filename, sga_name)
return nil, err
end
end
config = safer.readonly(config)
return config
end
......@@ -97,6 +105,7 @@ function configuration.check(config)
driver_config = schema.Optional(schema.Map(schema.String, schema.Any)),
resources = schema.Optional(schema.Collection(schema.String)),
extra_config = schema.Optional(schema.Map(schema.String, schema.Any)),
sgas = schema.Optional(schema.Collection(schema.String)),
}
local err = schema.CheckSchema(config, config_schema)
if err then
......
......@@ -4,6 +4,20 @@ local util = {}
local lfs = require("lfs")
local stat = require("posix.sys.stat")
function util.table_shallow_copy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function util.write_file(name, data)
local fd = io.open(name, "w")
if not fd then
......@@ -87,7 +101,7 @@ local function getPermissions(path)
os.execute(cmd)
local file = io.open(tmpname, "r")
if not file then
local msg = "Falha na abertura do arquivo temporrio. [%s] de sada do comando:%s."
local msg = "Failed to open command output file [%s]: %s."
return nil, string.format(msg, tmpname, cmd)
end
local content = file:read("*a")
......
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