-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from TAServers/102-config-refactor
102 - Rewrite configuration handling
- Loading branch information
Showing
8 changed files
with
165 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
local ConfigLoader = require("src.lua.utils.configLoader") | ||
|
||
return ConfigLoader.new() | ||
:registerProperty("testTimeout", { | ||
type = "number", | ||
default = 5000, | ||
}) | ||
:registerProperty("testMatch", { | ||
type = "table", | ||
default = { ".+%.test%.lua" }, | ||
}) | ||
:load(arg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
local OPTION_PATTERNS = { | ||
"%-%-([%a%-]+)=([^%s]+)", | ||
"%-%-([%a%-]+)", | ||
"%-([%a%-]+)=([^%s]+)", | ||
"%-([%a%-]+)", | ||
} | ||
|
||
local function matchOption(arg) | ||
for _, pattern in ipairs(OPTION_PATTERNS) do | ||
local key, value = string.match(arg, pattern) | ||
if key then | ||
return key, value or "true" | ||
end | ||
end | ||
end | ||
|
||
--- Parses command line options given as an arg array | ||
---@param args string[] | ||
---@return table<string, string> | ||
local function parseCliOptions(args) | ||
local parsed = {} | ||
|
||
for _, arg in ipairs(args) do | ||
local key, value = matchOption(arg) | ||
if key and value then | ||
parsed[key] = value | ||
end | ||
end | ||
|
||
return parsed | ||
end | ||
|
||
return parseCliOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
local parseCliOptions = require("src.lua.utils.cliParser") | ||
|
||
---@class ConfigProperty | ||
---@field default any | ||
---@field type type | ||
---@field cliOnly? boolean | ||
|
||
local CLI_ARGUMENT_CASTS_BY_TYPE = { | ||
number = tonumber, | ||
string = function(arg) | ||
return arg | ||
end, | ||
boolean = function(arg) | ||
return arg == "true" | ||
end, | ||
table = function(arg) | ||
local tbl = {} | ||
for match in arg:gmatch("([^,]+)") do | ||
table.insert(tbl, match) | ||
end | ||
return tbl | ||
end, | ||
} | ||
|
||
--- Returns the appropriate value for a property given a CLI and config file value | ||
---@param property ConfigProperty | ||
---@param cliValue string? | ||
---@param configFileValue any? | ||
---@return any | ||
local function getPropertyValue(property, cliValue, configFileValue) | ||
if cliValue ~= nil then | ||
local cast = CLI_ARGUMENT_CASTS_BY_TYPE[property.type] | ||
or CLI_ARGUMENT_CASTS_BY_TYPE.string | ||
return cast(cliValue) | ||
end | ||
|
||
if configFileValue ~= nil and not property.cliOnly then | ||
return configFileValue | ||
end | ||
|
||
return property.default | ||
end | ||
|
||
---@class ConfigLoader | ||
---@field properties table<string, ConfigProperty> | ||
local ConfigLoader = {} | ||
ConfigLoader.__index = ConfigLoader | ||
|
||
--- Create a new config loader | ||
---@return ConfigLoader | ||
function ConfigLoader.new() | ||
return setmetatable({ properties = {} }, ConfigLoader) | ||
end | ||
|
||
--- Registers a new configuration property | ||
---@param name string | ||
---@param options? ConfigProperty | ||
---@return self | ||
function ConfigLoader:registerProperty(name, options) | ||
self.properties[name] = options | ||
return self | ||
end | ||
|
||
--- Loads a config with the registered properties | ||
---@param cliArgs string[] | ||
---@return table<string, any> | ||
function ConfigLoader:load(cliArgs) | ||
local cliOptions = parseCliOptions(cliArgs) | ||
|
||
local configPath = cliOptions.config or cliOptions.c or "lest.config.lua" | ||
local configLoaded, configFile = pcall(dofile, configPath) | ||
if not configLoaded then | ||
configFile = {} | ||
end | ||
|
||
local mergedConfig = {} | ||
for propertyName, property in pairs(self.properties) do | ||
local propertyValue = getPropertyValue( | ||
property, | ||
cliOptions[propertyName], | ||
configFile[propertyName] | ||
) | ||
|
||
if type(propertyValue) ~= property.type then | ||
print( | ||
string.format( | ||
"Invalid config: Expected %s to be a %s", | ||
propertyName, | ||
property.type | ||
) | ||
) | ||
os.exit(1) | ||
end | ||
|
||
mergedConfig[propertyName] = propertyValue | ||
end | ||
|
||
return mergedConfig | ||
end | ||
|
||
return ConfigLoader |