From 4d04b14cf180d6cf1df37e4518cb294f6c242b79 Mon Sep 17 00:00:00 2001
From: Carla Ourofino <ourofino@tecgraf.puc-rio.br>
Date: Mon, 23 Mar 2020 18:18:52 -0300
Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=A3o=20para=20permitir=20arquivo?=
 =?UTF-8?q?=20.cfg=20com=20m=C3=BAlltiplas=20configura=C3=A7=C3=B5es=20de?=
 =?UTF-8?q?=20sga.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

[SOMA-3827][SOMA-4041]
---
 sga/configuration.lua | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/sga/configuration.lua b/sga/configuration.lua
index 2a9e390..a53061e 100644
--- a/sga/configuration.lua
+++ b/sga/configuration.lua
@@ -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
-- 
GitLab