diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2015-06-25 16:47:34 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2015-06-25 16:47:34 +0200 |
| commit | 554836ba3575576e2ef463cdb87a2177f38bf24d (patch) | |
| tree | 4550fb04d8b4541530f1eea0ea4b7201f34255f7 | |
| parent | ec31f30bcc8dfd48c1248f3897fd051bfd12474a (diff) | |
| download | luarocks-554836ba3575576e2ef463cdb87a2177f38bf24d.tar.gz luarocks-554836ba3575576e2ef463cdb87a2177f38bf24d.tar.bz2 luarocks-554836ba3575576e2ef463cdb87a2177f38bf24d.zip | |
refactored the loading of the config files in a more structural manner, removing duplicate code and error handling.
| -rw-r--r-- | src/luarocks/cfg.lua | 84 |
1 files changed, 45 insertions, 39 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index ff18e0a3..6ff2fcf0 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
| @@ -124,8 +124,8 @@ else | |||
| 124 | end | 124 | end |
| 125 | 125 | ||
| 126 | -- Path configuration: | 126 | -- Path configuration: |
| 127 | |||
| 128 | local sys_config_file, home_config_file | 127 | local sys_config_file, home_config_file |
| 128 | local sys_config_file_default, home_config_file_default | ||
| 129 | local sys_config_dir, home_config_dir | 129 | local sys_config_dir, home_config_dir |
| 130 | local sys_config_ok, home_config_ok = false, false | 130 | local sys_config_ok, home_config_ok = false, false |
| 131 | local extra_luarocks_module_dir | 131 | local extra_luarocks_module_dir |
| @@ -173,49 +173,55 @@ local merge_overrides = function(overrides) | |||
| 173 | util.deep_merge(cfg, overrides) | 173 | util.deep_merge(cfg, overrides) |
| 174 | end | 174 | end |
| 175 | 175 | ||
| 176 | sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" | 176 | -- load config file from a list until first succesful one. Info is |
| 177 | do | 177 | -- added to `cfg` module table, returns filepath of succesfully loaded |
| 178 | local err, errcode | 178 | -- file or nil if it failed |
| 179 | sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, env_for_config_file()) | 179 | local load_config_file = function(list) |
| 180 | if (not sys_config_ok) and errcode == "open" then -- file not found, so try alternate file | 180 | for _, filepath in ipairs(list) do |
| 181 | sys_config_file = sys_config_dir.."/config.lua" | 181 | local result, err, errcode = persist.load_into_table(filepath, env_for_config_file()) |
| 182 | sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, env_for_config_file()) | 182 | if (not result) and errcode ~= "open" then |
| 183 | end | 183 | -- errcode is either "load" or "run"; bad config file, so error out |
| 184 | if (not sys_config_ok) and errcode ~= "open" then -- either "load" or "run"; bad config file, bail out with error | 184 | io.stderr:write(err.."\n") |
| 185 | io.stderr:write(err.."\n") | 185 | os.exit(cfg.errorcodes.CONFIGFILE) |
| 186 | os.exit(cfg.errorcodes.CONFIGFILE) | 186 | end |
| 187 | end | 187 | if result then |
| 188 | if sys_config_ok then | 188 | -- succes in loading and running, merge contents and exit |
| 189 | merge_overrides(sys_config_ok) | 189 | merge_overrides(result) |
| 190 | return filepath | ||
| 191 | end | ||
| 190 | end | 192 | end |
| 193 | return nil -- nothing was loaded | ||
| 194 | end | ||
| 195 | |||
| 196 | |||
| 197 | -- Load system configuration file | ||
| 198 | do | ||
| 199 | sys_config_file_default = sys_config_dir.."/config.lua" | ||
| 200 | sys_config_file = load_config_file({ | ||
| 201 | site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua", | ||
| 202 | sys_config_file_default, | ||
| 203 | }) | ||
| 204 | sys_config_ok = (sys_config_file ~= nil) | ||
| 191 | end | 205 | end |
| 192 | 206 | ||
| 207 | -- Load user configuration file (if allowed) | ||
| 193 | if not site_config.LUAROCKS_FORCE_CONFIG then | 208 | if not site_config.LUAROCKS_FORCE_CONFIG then |
| 209 | |||
| 210 | home_config_file_default = home_config_dir.."/config.lua" | ||
| 211 | local list = { | ||
| 212 | os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG"), | ||
| 213 | home_config_dir.."/config-"..cfg.lua_version..".lua", | ||
| 214 | home_config_file_default, | ||
| 215 | } | ||
| 216 | -- first entry might be a silent nil, check and remove if so | ||
| 217 | if not list[1] then table.remove(list, 1) end | ||
| 218 | |||
| 219 | home_config_file = load_config_file(list) | ||
| 220 | home_config_ok = (home_config_file ~= nil) | ||
| 194 | 221 | ||
| 195 | local home_overrides, err, errcode | ||
| 196 | home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG") | ||
| 197 | if home_config_file then | ||
| 198 | home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file()) | ||
| 199 | else | ||
| 200 | home_config_file = home_config_dir.."/config-"..cfg.lua_version..".lua" | ||
| 201 | home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file()) | ||
| 202 | if (not home_overrides) and (not errcode == "run") then | ||
| 203 | home_config_file = home_config_dir.."/config.lua" | ||
| 204 | home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file()) | ||
| 205 | end | ||
| 206 | end | ||
| 207 | if home_overrides then | ||
| 208 | home_config_ok = true | ||
| 209 | merge_overrides(home_overrides) | ||
| 210 | else | ||
| 211 | home_config_ok = home_overrides | ||
| 212 | if errcode ~= "open" then | ||
| 213 | io.stderr:write(err.."\n") | ||
| 214 | os.exit(cfg.errorcodes.CONFIGFILE) | ||
| 215 | end | ||
| 216 | end | ||
| 217 | end | 222 | end |
| 218 | 223 | ||
| 224 | |||
| 219 | if not next(cfg.rocks_trees) then | 225 | if not next(cfg.rocks_trees) then |
| 220 | if cfg.home_tree then | 226 | if cfg.home_tree then |
| 221 | table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) | 227 | table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) |
| @@ -610,11 +616,11 @@ end | |||
| 610 | function cfg.which_config() | 616 | function cfg.which_config() |
| 611 | return { | 617 | return { |
| 612 | system = { | 618 | system = { |
| 613 | file = sys_config_file, | 619 | file = sys_config_file or sys_config_file_default, |
| 614 | ok = sys_config_ok, | 620 | ok = sys_config_ok, |
| 615 | }, | 621 | }, |
| 616 | user = { | 622 | user = { |
| 617 | file = home_config_file, | 623 | file = home_config_file or home_config_file_default, |
| 618 | ok = home_config_ok, | 624 | ok = home_config_ok, |
| 619 | } | 625 | } |
| 620 | } | 626 | } |
